Commit 23be3256 authored by Tom Niget's avatar Tom Niget

Migrate class and method emission to new referencemodel

parent 316e4d8b
......@@ -20,7 +20,7 @@ public:
return sync_wrapper(std::forward<Args>(args)...);
}
};
/*
struct method {};
template <typename Func, typename Self>
......@@ -52,5 +52,8 @@ auto dot_bind(Obj, Attr attr) {
#define dotp(OBJ, NAME) [](auto && obj) -> auto { return dot_bind(obj, obj->NAME); }(OBJ)
#define dots(OBJ, NAME) [](auto && obj) -> auto { return std::remove_reference<decltype(obj)>::type::py_type::NAME; }(OBJ)
*/
#include "referencemodel.hpp"
#endif // TYPON_BASEDEF_HPP
......@@ -25,17 +25,17 @@
#define _Args(...) __VA_ARGS__
#define COMMA() ,
#define METHOD(ret, name, args, ...) \
static constexpr struct name##_s : method { \
static constexpr struct name##_s : referencemodel::method { \
template <typename Self> ret operator() args const __VA_ARGS__ \
} name{};
#define METHOD_GEN(gen, ret, name, args, ...) \
static constexpr struct name##_s : method { \
static constexpr struct name##_s : referencemodel::method { \
template <typename Self, _Args gen> ret operator() args const __VA_ARGS__ \
} name{};
#define FUNCTION(ret, name, args, ...) \
struct { \
struct : referencemodel::staticmethod { \
ret operator() args const __VA_ARGS__ \
} static constexpr name{};
......
This diff is collapsed.
# coding: utf-8
# norun
# https://lab.nexedi.com/xavier_thompson/typon-snippets/tree/master/references
class Person:
name: str
......@@ -12,14 +12,15 @@ class Person:
def afficher(self):
print(self.name, self.age)
def creer():
return Person("jean", 123)
# def creer():
# return Person("jean", 123)
if __name__ == "__main__":
y = Person
x = creer()
#x = creer()
x = Person("jean", 123)
print(x.name)
print(x.age)
x.afficher()
y.afficher(x)
#y.afficher(x)
......@@ -67,11 +67,12 @@ class NodeVisitor(UniversalVisitor):
elif node is TY_STR:
yield "PyStr"
elif isinstance(node, UserType):
if node.is_reference:
yield "PyObj<"
yield f"decltype({node.name})"
if node.is_reference:
yield "::py_type>"
# if node.is_reference:
# yield "PyObj<"
#yield "auto"
yield f"referencemodel::Rc<decltype(__main__::{node.name})::Obj>"
# if node.is_reference:
# yield "::py_type>"
elif isinstance(node, TypeType):
yield "auto" # TODO
elif isinstance(node, FunctionType):
......
......@@ -84,7 +84,7 @@ class ClassInnerVisitor(NodeVisitor):
# from transpiler.phases.emit_cpp.block import BlockVisitor
# yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD, True)
# yield f"}} {node.name} {{ this }};"
yield f"struct {node.name}_m_s : method {{"
yield f"struct {node.name}_m_s : referencemodel::method {{"
from transpiler.phases.emit_cpp.block import BlockVisitor
yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD)
yield f"}} static constexpr {node.name} {{}};"
......@@ -109,3 +109,29 @@ class ClassOuterVisitor(NodeVisitor):
# from transpiler.phases.emit_cpp.block import BlockVisitor
# yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD)
# yield f"}} static constexpr {node.name} {{}};"
@dataclass
class ClassInnerVisitor2(NodeVisitor):
scope: Scope
def visit_AnnAssign(self, node: ast.AnnAssign) -> Iterable[str]:
yield ""
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield "struct : referencemodel::method {"
from transpiler.phases.emit_cpp.block import BlockVisitor
yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD)
yield f"}} static constexpr {node.name} {{}};"
@dataclass
class ClassInnerVisitor4(NodeVisitor):
scope: Scope
def visit_AnnAssign(self, node: ast.AnnAssign) -> Iterable[str]:
member = self.scope.obj_type.fields[node.target.id]
yield from self.visit(member.type)
yield node.target.id
yield ";"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield ""
\ No newline at end of file
......@@ -227,6 +227,8 @@ class ExpressionVisitor(NodeVisitor):
use_dot = "dotp"
else:
use_dot = "dot"
if use_dot:
use_dot = "dot"
if use_dot:
yield use_dot
yield "(("
......
......@@ -4,7 +4,8 @@ from dataclasses import dataclass
from typing import Iterable
from transpiler.phases.emit_cpp.block import BlockVisitor
from transpiler.phases.emit_cpp.module import ModuleVisitor, ModuleVisitor2, ModuleVisitorExt
from transpiler.phases.emit_cpp.module import ModuleVisitor, ModuleVisitor2, ModuleVisitorExt, ModuleVisitor3, \
ModuleVisitor4
# noinspection PyPep8Naming
......@@ -22,10 +23,26 @@ class FileVisitor(BlockVisitor):
code = [line for stmt in node.body for line in visitor.visit(stmt)]
yield from visitor.includes
yield "namespace PROGRAMNS {"
yield "struct __main__ : referencemodel::moduletype<__main__> {"
yield from code
visitor = ModuleVisitor2(self.scope)
# visitor = ModuleVisitor2(self.scope)
# code = [line for stmt in node.body for line in visitor.visit(stmt)]
# yield from code
visitor = ModuleVisitor4(self.scope)
code = [line for stmt in node.body for line in visitor.visit(stmt)]
yield from code
visitor = ModuleVisitor3(self.scope)
code = [line for stmt in node.body for line in visitor.visit(stmt)]
yield from code
yield "auto operator -> () const { return this; }"
yield "} __main__;"
yield "}"
yield "#ifdef TYPON_EXTENSION"
yield f"PYBIND11_MODULE({self.module_name}, m) {{"
......@@ -37,6 +54,6 @@ class FileVisitor(BlockVisitor):
yield "#else"
yield "int main(int argc, char* argv[]) {"
yield "py_sys::all.argv = typon::PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));"
yield "PROGRAMNS::root().call();"
yield "PROGRAMNS::__main__.root().call();"
yield "}"
yield "#endif"
......@@ -8,7 +8,7 @@ from transpiler.phases.typing import FunctionType
from transpiler.phases.typing.scope import Scope
from transpiler.phases.emit_cpp import CoroutineMode, FunctionEmissionKind, NodeVisitor, join
from transpiler.phases.emit_cpp.block import BlockVisitor
from transpiler.phases.emit_cpp.class_ import ClassVisitor
from transpiler.phases.emit_cpp.class_ import ClassVisitor, ClassInnerVisitor, ClassInnerVisitor2, ClassInnerVisitor4
from transpiler.phases.emit_cpp.function import FunctionVisitor
from transpiler.utils import compare_ast, highlight
......@@ -100,7 +100,8 @@ class ModuleVisitor(BlockVisitor):
raise NotImplementedError(node)
def visit_ClassDef(self, node: ast.ClassDef) -> Iterable[str]:
yield from ClassVisitor().visit(node)
#yield from ClassVisitor().visit(node)
yield from ()
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield from super().visit_free_func(node, FunctionEmissionKind.DECLARATION)
......@@ -116,6 +117,68 @@ class ModuleVisitor2(NodeVisitor):
yield ""
pass
@dataclass
class ModuleVisitor3(NodeVisitor):
scope: Scope
def visit_ClassDef(self, node: ast.ClassDef) -> Iterable[str]:
yield from ()
return
if gen_instances := getattr(node, "gen_instances", None):
for args, inst in gen_instances.items():
yield from self.visit_ClassDef(inst)
return
yield f"static constexpr _detail_<__main__>::{node.name}<> {node.name} {{}};"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield from BlockVisitor(self.scope).visit_free_func(node, FunctionEmissionKind.DEFINITION)
@dataclass
class ModuleVisitor4(NodeVisitor):
scope: Scope
def visit_ClassDef(self, node: ast.ClassDef) -> Iterable[str]:
if gen_instances := getattr(node, "gen_instances", None):
for args, inst in gen_instances.items():
yield from self.visit_ClassDef(inst)
return
yield f"struct {node.name} : referencemodel::classtype<{node.name}> {{"
yield f"template <typename _Base0 = referencemodel::object>"
yield f"struct body : referencemodel::classbodytype<_Base0, body<>> {{"
yield f"static constexpr std::string_view name = \"{node.name}\";"
inner = ClassInnerVisitor2(node.inner_scope)
for stmt in node.body:
yield from inner.visit(stmt)
yield "};"
yield "static constexpr body _body{};"
yield "static_assert(sizeof _body == 1);"
yield f"struct Obj : referencemodel::instance<{node.name}, Obj> {{"
inner = ClassInnerVisitor4(node.inner_scope)
for stmt in node.body:
yield from inner.visit(stmt)
yield "template <typename... U>"
yield "Obj(U&&... args) {"
yield "dot(this, __init__)(this, std::forward<U>(args)...);"
yield "}"
yield "};"
yield "template <typename _Unused = void, typename... T>"
yield "auto operator() (T&&... args) const {"
yield "return referencemodel::rc(Obj{std::forward<T>(args)...});"
yield "}"
yield f"}} static constexpr {node.name} {{}};"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield from ()
@dataclass
class ModuleVisitorExt(NodeVisitor):
scope: Scope
......
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