Commit 9a209460 authored by Tom Niget's avatar Tom Niget

Make inference run in multiple passes

parent 06f91a66
......@@ -57,8 +57,8 @@ path = Path(args.input[0])
with open(path, "r", encoding="utf-8") as f:
code = f.read()
from .transpiler import transpile
from .transpiler.format import format_code
from transpiler import transpile
from transpiler.format import format_code
raw_cpp = transpile(code, path.name, path)
formatted = format_code(raw_cpp)
......
import sys
import math
x = [6]
x = 5
u = (math.abcd) # abcd
a = 5 if True else 3
def c(x: int):
return x
for v in 6:
g = 6
h = 7
i = 8
pass
if __name__ == "__main__":
pass
\ No newline at end of file
......@@ -29,7 +29,7 @@ def read_file(path):
fd.close()
return content
def handle_connection(connfd: socket, filepath):
def handle_connection(connfd, filepath):
buf = connfd.recv(1024).decode("utf-8")
length = buf.find("\r\n\r\n")
content = read_file(filepath)
......@@ -37,7 +37,7 @@ def handle_connection(connfd: socket, filepath):
connfd.send(response.encode("utf-8"))
connfd.close()
def server_loop(sockfd: socket, filepath):
def server_loop(sockfd, filepath):
while True:
connfd, _ = sockfd.accept()
......
......@@ -25,8 +25,8 @@ class BlockVisitor(NodeVisitor):
def visit_Pass(self, node: ast.Pass) -> Iterable[str]:
yield ";"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield from self.visit_free_func(node)
# def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
# yield from self.visit_free_func(node)
def visit_free_func(self, node: ast.FunctionDef, emission: FunctionEmissionKind) -> Iterable[str]:
if getattr(node, "is_main", False):
......
......@@ -12,5 +12,6 @@ class IfMainVisitor(ast.NodeVisitor):
new_node = ast.parse("def main(): pass").body[0]
new_node.body = stmt.body
new_node.is_main = True
node.main_if = new_node
node.body[i] = new_node
return
\ No newline at end of file
......@@ -28,12 +28,29 @@ class ScoperVisitor(NodeVisitorSeq):
self.fdecls = []
for b in block:
self.visit(b)
for node, rtype in self.fdecls:
for b in node.body:
decls = {}
visitor = ScoperBlockVisitor(node.inner_scope, decls)
visitor.visit(b)
b.decls = decls
if not node.inner_scope.has_return:
rtype.unify(TY_NONE) # todo: properly indicate missing return
if self.fdecls:
old_list = self.fdecls
exc = None
while True:
new_list = []
for node, rtype in old_list:
from transpiler.exceptions import CompileError
try:
for b in node.body:
decls = {}
visitor = ScoperBlockVisitor(node.inner_scope, decls)
visitor.visit(b)
b.decls = decls
if not node.inner_scope.has_return:
rtype.unify(TY_NONE) # todo: properly indicate missing return
except CompileError as e:
new_list.append((node, rtype))
if not exc:
exc = e
if len(new_list) == len(old_list):
raise exc
if not new_list:
break
old_list = new_list
exc = None
......@@ -34,7 +34,7 @@ DUNDER = {
class ScoperExprVisitor(ScoperVisitor):
def visit(self, node) -> BaseType:
if existing := getattr(node, "type", None):
return existing
return existing.resolve()
res = super().visit(node)
if not res:
raise NotImplementedError(f"`{ast.unparse(node)}` {type(node)}")
......
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