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

Fix type call evaluation

parent d3d93dac
clang-format==15.0.7
pytype~=2023.4.11
dataclasses~=0.6
python-dotenv~=1.0.0
\ No newline at end of file
python-dotenv~=1.0.0
# coding: utf-8
import argparse
from os import system, environ
from pathlib import Path
......@@ -10,7 +11,13 @@ from dotenv import load_dotenv
load_dotenv()
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--compile", action="store_true")
args = parser.parse_args()
def run_tests():
for path in Path('tests').glob('*.py'):
print(path.name)
if path.name.startswith('_'):
......@@ -22,9 +29,12 @@ def run_tests():
name_cpp = path.with_suffix('.cpp')
with open(name_cpp, "w", encoding="utf-8") as fcpp:
fcpp.write(res)
print(".cpp generated")
if args.compile:
continue
name_bin = path.with_suffix('').as_posix()
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"):
commands.append(alt.format(name_bin=name_bin, name_cpp_posix=name_cpp.as_posix()))
......@@ -34,7 +44,6 @@ def run_tests():
if system(cmd) != 0:
print(f"Error running command: {cmd}")
break
#exit()
if __name__ == "__main__":
......
# coding: utf-8
def fib(upto):
a = 0
b = 1
while b < upto:
yield a
a, b = b, a + b
if __name__ == "__main__":
f = fib(50)
for i in range(15):
# coding: utf-8
def fib(upto):
a = 0
b = 1
while b < upto:
yield a
a, b = b, a + b
if __name__ == "__main__":
f = fib(50)
for i in range(15):
print(next(f, None))
\ No newline at end of file
from typon import fork, sync
def fibo(n: int) -> int:
if n < 2:
return n
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
sync()
return a.get() + b.get()
"""
def fibo(n: int) -> int:
if n < 2:
return n
with sync(): # {
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
# }
return a + b
"""
"""
Task<int> fibo(int n) {
if (n < 2) {
return n;
}
Forked<int> a;
Forked<int> b;
{
a = fork(fibo(n - 1));
// cvcvc
b = fork(fibo(n - 2));
co_await sync();
}
co_return a.get() + b.get();
"""
"""
Task<int> fibo(int n) {
int a, b;
co_return []() -> Join<int> {
if (n < 2) {
return n;
}
co_await fork(fibo(n - 1), a);
co_await fork(fibo(n - 2), b);
co_await Sync();
co_return a + b;
}();
}
"""
if __name__ == "__main__":
from typon import fork, sync
def fibo(n: int) -> int:
if n < 2:
return n
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
sync()
return a.get() + b.get()
"""
def fibo(n: int) -> int:
if n < 2:
return n
with sync(): # {
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
# }
return a + b
"""
"""
Task<int> fibo(int n) {
if (n < 2) {
return n;
}
Forked<int> a;
Forked<int> b;
{
a = fork(fibo(n - 1));
// cvcvc
b = fork(fibo(n - 2));
co_await sync();
}
co_return a.get() + b.get();
"""
"""
Task<int> fibo(int n) {
int a, b;
co_return []() -> Join<int> {
if (n < 2) {
return n;
}
co_await fork(fibo(n - 1), a);
co_await fork(fibo(n - 2), b);
co_await Sync();
co_return a + b;
}();
}
"""
if __name__ == "__main__":
print(fibo(20)) # should display 832040
\ No newline at end of file
import ast
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.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({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
......@@ -18,6 +19,7 @@ PRELUDE.vars.update({
# "list": VarDecl(VarKind.LOCAL, TY_TYPE, PyList),
"int": VarDecl(VarKind.LOCAL, TypeType(TY_INT)),
"str": VarDecl(VarKind.LOCAL, TypeType(TY_STR)),
"bytes": VarDecl(VarKind.LOCAL, TypeType(TY_BYTES)),
"bool": VarDecl(VarKind.LOCAL, TypeType(TY_BOOL)),
"complex": VarDecl(VarKind.LOCAL, TypeType(TY_COMPLEX)),
"None": VarDecl(VarKind.LOCAL, TypeType(TY_NONE)),
......@@ -29,27 +31,39 @@ PRELUDE.vars.update({
"Task": VarDecl(VarKind.LOCAL, TypeType(Task)),
"Future": VarDecl(VarKind.LOCAL, TypeType(Future)),
"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"
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):
for child in path.iterdir():
for child in sorted(path.iterdir()):
if child.is_dir():
mod_scope = PRELUDE.child(ScopeKind.GLOBAL)
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":
StdlibVisitor(scope).visit(ast.parse(child.read_text()))
print(f"Visited {child}")
elif child.suffix == ".py":
mod_scope = PRELUDE.child(ScopeKind.GLOBAL)
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)
print("Stdlib visited!")
#exit()
\ No newline at end of file
......@@ -109,7 +109,8 @@ class ScoperExprVisitor(ScoperVisitor):
def visit_function_call(self, ftype: BaseType, arguments: List[BaseType]):
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)
if not isinstance(ftype, FunctionType):
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