Commit 420d71ea authored by Tom Niget's avatar Tom Niget

Add bindings for methods and fields

parent 1e3fcd78
...@@ -6,7 +6,7 @@ from dataclasses import dataclass, field ...@@ -6,7 +6,7 @@ from dataclasses import dataclass, field
from transpiler.phases.typing import FunctionType from transpiler.phases.typing import FunctionType
from transpiler.phases.typing.scope import Scope from transpiler.phases.typing.scope import Scope
from transpiler.phases.emit_cpp import CoroutineMode, FunctionEmissionKind, NodeVisitor from transpiler.phases.emit_cpp import CoroutineMode, FunctionEmissionKind, NodeVisitor, join
from transpiler.phases.emit_cpp.block import BlockVisitor from transpiler.phases.emit_cpp.block import BlockVisitor
from transpiler.phases.emit_cpp.class_ import ClassVisitor from transpiler.phases.emit_cpp.class_ import ClassVisitor
from transpiler.phases.emit_cpp.function import FunctionVisitor from transpiler.phases.emit_cpp.function import FunctionVisitor
...@@ -127,13 +127,26 @@ class ModuleVisitorExt(NodeVisitor): ...@@ -127,13 +127,26 @@ class ModuleVisitorExt(NodeVisitor):
init = node.type.fields["__init__"].type.resolve().remove_self() init = node.type.fields["__init__"].type.resolve().remove_self()
init_params = init.parameters init_params = init.parameters
yield ".def(py::init<" yield ".def(py::init<"
for i, argty in enumerate(init_params): yield from join(", ", map(self.visit, init_params))
if i != 0:
yield ", "
yield from self.visit(argty)
yield ">())" yield ">())"
yield f'.def("__repr__", [](const PROGRAMNS::{node.name}_s::py_type& self)' yield f'.def("__repr__", [](const PROGRAMNS::{node.name}_s::py_type& self)'
yield "{ return repr(self); })" yield "{ return repr(self); })"
for f, v in node.type.fields.items():
if f == "__init__":
continue
if isinstance(v.type, FunctionType):
meth = v.type.remove_self()
yield f'.def("{f}", [](const PROGRAMNS::{node.name}_s::py_type& a'
if meth.parameters:
yield ","
vis = BlockVisitor(node.scope)
yield from vis.visit_func_params(((f"arg{i}", ty, None) for i, ty in enumerate(meth.parameters)), FunctionEmissionKind.LAMBDA)
yield f') {{ return dotp(&a, {f})('
if meth.parameters:
yield from join(", ", (f"arg{i}" for i, _ in enumerate(meth.parameters)))
yield ').call(); })'
else:
yield f'.def_readwrite("{f}", &PROGRAMNS::{node.name}_s::py_type::{f})'
yield ";" yield ";"
pass pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment