Skip to content

Commit

Permalink
simple type parsing works
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed Jul 23, 2024
1 parent b04d3d5 commit b2ff67e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions luisa_lang/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ def retrieve_ast_and_filename(f: object) -> Tuple[ast.AST, str]:
source_lines, lineno = sourceinspect.getsourcelines(f)
src, indent = dedent_and_retrieve_indentation(source_lines)
tree = increment_lineno_and_col_offset(ast.parse(src), lineno - 1, indent + 1)
for child in ast.walk(tree):
setattr(child, "source_file", source_file)
return tree, source_file
9 changes: 6 additions & 3 deletions luisa_lang/hir.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __eq__(self, value: object) -> bool:
and value.element == self.element
and value.count == self.count
)

def __repr__(self) -> str:
return f"ArrayType({self.element}, {self.count})"

Expand Down Expand Up @@ -344,8 +344,11 @@ def from_ast(ast: ast.AST) -> Optional["Span"]:
return None
if not hasattr(ast, "end_col_offset") or ast.end_col_offset is None:
return None
file = None
if hasattr(ast, "source_file"):
file = getattr(ast, "source_file")
return Span(
file=None,
file=file,
start=(getattr(ast, "lineno", 0), getattr(ast, "col_offset", 0)),
end=(getattr(ast, "end_lineno", 0), getattr(ast, "end_col_offset", 0)),
)
Expand Down Expand Up @@ -554,7 +557,7 @@ def get() -> "GlobalContext":

def __init__(self) -> None:
assert _global_context is None, "GlobalContext should be a singleton"
self.types = {}
self.types = {type(None): UnitType()}
self.functions = {}


Expand Down
4 changes: 3 additions & 1 deletion luisa_lang/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def __init__(self, func: ast.FunctionDef, p_ctx: ParsingContext) -> None:
self.arg_types = []
self.return_type = None
self._init_signature()
print(self.arg_types, "->", self.return_type)
print(self.arg_types, '->', self.return_type)

def _init_signature(
self,
Expand All @@ -288,6 +288,8 @@ def _init_signature(
raise RuntimeError("TODO: infer type")
if (arg_ty := p_ctx.parse_type(arg.annotation)) is not None:
self.arg_types.append(arg_ty)
else:
report_error(arg.annotation, f"invalid type for argument")
if func.returns is None:
self.return_type = hir.UnitType()
else:
Expand Down
5 changes: 1 addition & 4 deletions scripts/gen_math_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ class {ty}:
name = names[bits]
gen_vector_type(f"{name}{size}", f"i{bits}", "int", size)
gen_vector_type(f"u{name}{size}", f"u{bits}", "int", size)
with open("luisa_lang/_math_type_exports.py", "w") as f:
__builtins__.print(
f'from luisa_lang.math_types import ({", ".join(exports)})', file=f
)
print("__all__ = " + str(exports))


if __name__ == "__main__":
Expand Down

0 comments on commit b2ff67e

Please sign in to comment.