Commit 5f91e8b6 authored by Tom Niget's avatar Tom Niget

Fix type call evaluation

parent d3d93dac
# coding: utf-8 # coding: utf-8
import argparse
from os import system, environ from os import system, environ
from pathlib import Path from pathlib import Path
...@@ -10,7 +11,13 @@ from dotenv import load_dotenv ...@@ -10,7 +11,13 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--compile", action="store_true")
args = parser.parse_args()
def run_tests(): def run_tests():
for path in Path('tests').glob('*.py'): for path in Path('tests').glob('*.py'):
print(path.name) print(path.name)
if path.name.startswith('_'): if path.name.startswith('_'):
...@@ -22,9 +29,12 @@ def run_tests(): ...@@ -22,9 +29,12 @@ def run_tests():
name_cpp = path.with_suffix('.cpp') name_cpp = path.with_suffix('.cpp')
with open(name_cpp, "w", encoding="utf-8") as fcpp: with open(name_cpp, "w", encoding="utf-8") as fcpp:
fcpp.write(res) fcpp.write(res)
print(".cpp generated")
if args.compile:
continue
name_bin = path.with_suffix('').as_posix() name_bin = path.with_suffix('').as_posix()
commands = [ commands = [
f"bash -c 'PYTHONPATH=stdlib python3 ./{path.as_posix()}'", # f"bash -c 'PYTHONPATH=stdlib python3 ./{path.as_posix()}'",
] ]
if alt := environ.get("ALT_RUNNER"): if alt := environ.get("ALT_RUNNER"):
commands.append(alt.format(name_bin=name_bin, name_cpp_posix=name_cpp.as_posix())) commands.append(alt.format(name_bin=name_bin, name_cpp_posix=name_cpp.as_posix()))
...@@ -34,7 +44,6 @@ def run_tests(): ...@@ -34,7 +44,6 @@ def run_tests():
if system(cmd) != 0: if system(cmd) != 0:
print(f"Error running command: {cmd}") print(f"Error running command: {cmd}")
break break
#exit()
if __name__ == "__main__": if __name__ == "__main__":
......
import ast import ast
from pathlib import Path from pathlib import Path
from transpiler.phases.typing.scope import VarKind, VarDecl, ScopeKind from transpiler.phases.typing.scope import VarKind, VarDecl, ScopeKind, Scope
from transpiler.phases.typing.stdlib import PRELUDE, StdlibVisitor from transpiler.phases.typing.stdlib import PRELUDE, StdlibVisitor
from transpiler.phases.typing.types import TY_TYPE, TY_INT, TY_STR, TY_BOOL, TY_COMPLEX, TY_NONE, FunctionType, \ from transpiler.phases.typing.types import TY_TYPE, TY_INT, TY_STR, TY_BOOL, TY_COMPLEX, TY_NONE, FunctionType, \
TypeVariable, TY_MODULE, CppType, PyList, TypeType, Forked, Task, Future, PyIterator, TupleType TypeVariable, CppType, PyList, TypeType, Forked, Task, Future, PyIterator, TupleType, TypeOperator, BaseType, \
ModuleType, TY_BYTES
PRELUDE.vars.update({ PRELUDE.vars.update({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT), # "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
...@@ -18,6 +19,7 @@ PRELUDE.vars.update({ ...@@ -18,6 +19,7 @@ PRELUDE.vars.update({
# "list": VarDecl(VarKind.LOCAL, TY_TYPE, PyList), # "list": VarDecl(VarKind.LOCAL, TY_TYPE, PyList),
"int": VarDecl(VarKind.LOCAL, TypeType(TY_INT)), "int": VarDecl(VarKind.LOCAL, TypeType(TY_INT)),
"str": VarDecl(VarKind.LOCAL, TypeType(TY_STR)), "str": VarDecl(VarKind.LOCAL, TypeType(TY_STR)),
"bytes": VarDecl(VarKind.LOCAL, TypeType(TY_BYTES)),
"bool": VarDecl(VarKind.LOCAL, TypeType(TY_BOOL)), "bool": VarDecl(VarKind.LOCAL, TypeType(TY_BOOL)),
"complex": VarDecl(VarKind.LOCAL, TypeType(TY_COMPLEX)), "complex": VarDecl(VarKind.LOCAL, TypeType(TY_COMPLEX)),
"None": VarDecl(VarKind.LOCAL, TypeType(TY_NONE)), "None": VarDecl(VarKind.LOCAL, TypeType(TY_NONE)),
...@@ -29,27 +31,39 @@ PRELUDE.vars.update({ ...@@ -29,27 +31,39 @@ PRELUDE.vars.update({
"Task": VarDecl(VarKind.LOCAL, TypeType(Task)), "Task": VarDecl(VarKind.LOCAL, TypeType(Task)),
"Future": VarDecl(VarKind.LOCAL, TypeType(Future)), "Future": VarDecl(VarKind.LOCAL, TypeType(Future)),
"Iterator": VarDecl(VarKind.LOCAL, TypeType(PyIterator)), "Iterator": VarDecl(VarKind.LOCAL, TypeType(PyIterator)),
"Tuple": VarDecl(VarKind.LOCAL, TypeType(TupleType)), "tuple": VarDecl(VarKind.LOCAL, TypeType(TupleType)),
}) })
typon_std = Path(__file__).parent.parent.parent.parent / "stdlib" typon_std = Path(__file__).parent.parent.parent.parent / "stdlib"
def make_module(name: str, scope: Scope) -> BaseType:
ty = ModuleType([], f"module${name}")
for n, v in scope.vars.items():
ty.members[n] = v.type
return ty
def discover_module(path: Path, scope): def discover_module(path: Path, scope):
for child in path.iterdir(): for child in sorted(path.iterdir()):
if child.is_dir(): if child.is_dir():
mod_scope = PRELUDE.child(ScopeKind.GLOBAL) mod_scope = PRELUDE.child(ScopeKind.GLOBAL)
discover_module(child, mod_scope) discover_module(child, mod_scope)
scope.vars[child.name] = VarDecl(VarKind.LOCAL, TY_MODULE, {k: v.type for k, v in mod_scope.vars.items()}) scope.vars[child.name] = make_mod_decl(child, mod_scope)
elif child.name == "__init__.py": elif child.name == "__init__.py":
StdlibVisitor(scope).visit(ast.parse(child.read_text())) StdlibVisitor(scope).visit(ast.parse(child.read_text()))
print(f"Visited {child}") print(f"Visited {child}")
elif child.suffix == ".py": elif child.suffix == ".py":
mod_scope = PRELUDE.child(ScopeKind.GLOBAL) mod_scope = PRELUDE.child(ScopeKind.GLOBAL)
StdlibVisitor(mod_scope).visit(ast.parse(child.read_text())) StdlibVisitor(mod_scope).visit(ast.parse(child.read_text()))
scope.vars[child.stem] = VarDecl(VarKind.LOCAL, TY_MODULE, {k: v.type for k, v in mod_scope.vars.items()}) scope.vars[child.stem] = make_mod_decl(child, mod_scope)
print(f"Visited {child}")
def make_mod_decl(child, mod_scope):
return VarDecl(VarKind.MODULE, make_module(child.name, mod_scope), {k: v.type for k, v in mod_scope.vars.items()})
discover_module(typon_std, PRELUDE) discover_module(typon_std, PRELUDE)
print("Stdlib visited!") print("Stdlib visited!")
#exit()
\ No newline at end of file
...@@ -109,7 +109,8 @@ class ScoperExprVisitor(ScoperVisitor): ...@@ -109,7 +109,8 @@ class ScoperExprVisitor(ScoperVisitor):
def visit_function_call(self, ftype: BaseType, arguments: List[BaseType]): def visit_function_call(self, ftype: BaseType, arguments: List[BaseType]):
if isinstance(ftype, TypeType):# and isinstance(ftype.type_object, UserType): if isinstance(ftype, TypeType):# and isinstance(ftype.type_object, UserType):
init: FunctionType = self.visit_getattr(ftype.type_object, "__init__") init: FunctionType = self.visit_getattr(ftype, "__init__").remove_self()
init.return_type = ftype.type_object
return self.visit_function_call(init, arguments) return self.visit_function_call(init, arguments)
if not isinstance(ftype, FunctionType): if not isinstance(ftype, FunctionType):
raise IncompatibleTypesError(f"Cannot call {ftype}") raise IncompatibleTypesError(f"Cannot call {ftype}")
......
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