diff --git a/ast_1.py b/ast_1.py index 330e75d..63978c5 100644 --- a/ast_1.py +++ b/ast_1.py @@ -6,7 +6,23 @@ from typing import Generic, Callable, Self, Type, TypeVar, Union from common import TT, PrimitiveTypes, bcolors, operators -from ast_exprs import AstirExpr, ShuntingYardAlgorithmResults, Identifier, Literal, PrimitiveType, Reference, AstirTuple, SymbolTable, Parameter, Symbol, Parenthesized, Lambda, Assignment, Application, check_is_allowed +from ast_exprs import ( + AstirExpr, + ShuntingYardAlgorithmResults, + Identifier, + Literal, + PrimitiveType, + Reference, + AstirTuple, + SymbolTable, + Parameter, + Symbol, + Parenthesized, + Lambda, + Assignment, + Application, + check_is_allowed, +) class Token: @@ -22,6 +38,7 @@ def __init__( def __repr__(self) -> str: return f"{self.ty} ({self.val})" + def get_op(possible_op: Token | None) -> tuple[str, dict[str, int]] | None: if ( possible_op is None @@ -32,6 +49,7 @@ def get_op(possible_op: Token | None) -> tuple[str, dict[str, int]] | None: op = operators[possible_op.ty.value] return (possible_op.ty.value, op) + def is_valid_ident(c: str) -> bool: return c.isalnum() or c == "_" @@ -196,7 +214,7 @@ def identifier_check(c: str | None, rest: str) -> bool: class Parser(Cursor): def __init__(self, input: list[Token]) -> None: super().__init__(input) - self.results: list['AstirExpr'] = [] + self.results: list["AstirExpr"] = [] global_symbols = SymbolTable(0) # TODO: we are waiting for typedef! global_symbols.insert("int", PrimitiveType(PrimitiveTypes.INT)) @@ -313,7 +331,7 @@ def parse(self) -> AstirExpr | None: # paren = self.parse() # # raise Exception(f"Enum value(?): {c.val} -> {paren}") # result = DataVariantWithInnerValue(Identifier(c.val), paren) - else: + else: self.advance() for_assignment = False if ( @@ -352,7 +370,9 @@ def parse(self) -> AstirExpr | None: elif len(the_between) == 1: # Init Parenthesized with an expression (the_between[0]) # to do exactly what it says... for example (\ :: int ...) - result = Parenthesized(PrimitiveType(PrimitiveTypes.UNIT), the_between[0]) + result = Parenthesized( + PrimitiveType(PrimitiveTypes.UNIT), the_between[0] + ) elif len(the_between) > 1 and has_comma: # Handle tuples result = AstirTuple(the_between) @@ -455,7 +475,8 @@ def parse(self) -> AstirExpr | None: possible_arg = self.parse() if possible_arg is None: raise Exception("Failed to parse") - if possible_arg.ty.ty != type_symbol.val.ty: + + if possible_arg.ty != type_symbol.val.ty: raise Exception( f"{bcolors.FAIL}{bcolors.BOLD}Type mismatch{bcolors.ENDC}" ) diff --git a/ast_exprs.py b/ast_exprs.py index 8b3210b..1e3565c 100644 --- a/ast_exprs.py +++ b/ast_exprs.py @@ -6,6 +6,7 @@ class AstirExpr(ABC): def __init__(self, ty: Union['PrimitiveTypes', 'AstirExpr']): super().__init__() self.ty = ty + def check_is_allowed(AstirExpr: AstirExpr | None) -> bool: allowed = AstirExpr is not None or ( @@ -83,6 +84,9 @@ def __init__( self.parameters=parameters + def __repr__(self): + return f"LambdaDef(Parameters={self.parameters})" + class Lambda(AstirExpr): def __init__(self, parameters: SymbolTable, body: AstirExpr): @@ -91,6 +95,8 @@ def __init__(self, parameters: SymbolTable, body: AstirExpr): self.definition = lambda_def self.body = body + def __repr__(self): + return f"Lambda(Def={self.definition}, Body={self.body})" class Parenthesized(AstirExpr): def __init__(self, ty: AstirExpr | PrimitiveTypes, inner: AstirExpr | None = None) -> None: