Add parsing for defers

This commit is contained in:
germax26 2024-08-11 12:59:19 +10:00
parent 659c79373d
commit 2cee127d8f
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
4 changed files with 24 additions and 14 deletions

View File

@ -70,7 +70,7 @@ class Statements(Statement):
statements: List[Statement] statements: List[Statement]
### Enums + Struct ### ### Enums + Struct ###
@dataclass @dataclass
class EnumEntry: class EnumEntry:
name: str name: str
@ -129,9 +129,9 @@ class Variable(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.name return self.name
def precedence(self) -> int: return 13 def precedence(self) -> int: return 13
@dataclass @dataclass
class ArrayAccess(Expression): class ArrayAccess(Expression):
array: Expression array: Expression
@ -326,7 +326,7 @@ class NotEqual(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" != "+self.wrap(self.rhs) return self.wrap(self.lhs)+" != "+self.wrap(self.rhs)
def precedence(self) -> int: return 7 def precedence(self) -> int: return 7
@dataclass @dataclass
@ -397,7 +397,7 @@ class Addition(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" + "+self.wrap(self.rhs) return self.wrap(self.lhs)+" + "+self.wrap(self.rhs)
def precedence(self) -> int: return 10 def precedence(self) -> int: return 10
@dataclass @dataclass
@ -407,7 +407,7 @@ class Subtract(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" - "+self.wrap(self.rhs) return self.wrap(self.lhs)+" - "+self.wrap(self.rhs)
def precedence(self) -> int: return 10 def precedence(self) -> int: return 10
@dataclass @dataclass
@ -417,7 +417,7 @@ class Multiplication(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" * "+self.wrap(self.rhs) return self.wrap(self.lhs)+" * "+self.wrap(self.rhs)
def precedence(self) -> int: return 11 def precedence(self) -> int: return 11
@dataclass @dataclass
@ -427,7 +427,7 @@ class Division(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" / "+self.wrap(self.rhs) return self.wrap(self.lhs)+" / "+self.wrap(self.rhs)
def precedence(self) -> int: return 11 def precedence(self) -> int: return 11
@dataclass @dataclass
@ -437,7 +437,7 @@ class Modulo(Expression):
def represent(self) -> str: def represent(self) -> str:
return self.wrap(self.lhs)+" % "+self.wrap(self.rhs) return self.wrap(self.lhs)+" % "+self.wrap(self.rhs)
def precedence(self) -> int: return 11 def precedence(self) -> int: return 11
@dataclass @dataclass
@ -446,7 +446,7 @@ class Bnot(Expression):
def represent(self) -> str: def represent(self) -> str:
return "~"+self.wrap(self.expression) return "~"+self.wrap(self.expression)
def precedence(self) -> int: return 12 def precedence(self) -> int: return 12
@dataclass @dataclass
@ -455,7 +455,7 @@ class Not(Expression):
def represent(self) -> str: def represent(self) -> str:
return "!"+self.wrap(self.expression) return "!"+self.wrap(self.expression)
def precedence(self) -> int: return 12 def precedence(self) -> int: return 12
@dataclass @dataclass
@ -464,7 +464,7 @@ class UnaryPlus(Expression):
def represent(self) -> str: def represent(self) -> str:
return "+"+self.wrap(self.expression) return "+"+self.wrap(self.expression)
def precedence(self) -> int: return 12 def precedence(self) -> int: return 12
@dataclass @dataclass
@ -473,7 +473,7 @@ class UnaryMinus(Expression):
def represent(self) -> str: def represent(self) -> str:
return "-"+self.wrap(self.expression) return "-"+self.wrap(self.expression)
def precedence(self) -> int: return 12 def precedence(self) -> int: return 12
@dataclass @dataclass
@ -542,4 +542,8 @@ class Import(Statement):
@dataclass @dataclass
class TypeDefinition(Statement): class TypeDefinition(Statement):
name: str name: str
expression: TypeExpression expression: TypeExpression
@dataclass
class DeferStatement(Statement):
statement: Statement

View File

@ -656,6 +656,8 @@ def interpret_statements(statements: List_[Statement], program: ProgramState) ->
program.modules[file.str] = module program.modules[file.str] = module
case TypeDefinition(name, expression_): case TypeDefinition(name, expression_):
program.declare_and_assign_variable(name, TypeObject(calculate_type_expression(expression_, program))) program.declare_and_assign_variable(name, TypeObject(calculate_type_expression(expression_, program)))
case DeferStatement(statement=statement):
assert False, "TODO: Defers are not implemented"
case _: case _:
assert False, ("Unimplemented", statement) assert False, ("Unimplemented", statement)
return NothingResult() return NothingResult()

View File

@ -358,6 +358,9 @@ def parse_statement(lexer: Lexer) -> Statement:
type_expression = parse_type(lexer) type_expression = parse_type(lexer)
lexer.assert_token(SymbolToken(Symbol.Semicolon)) lexer.assert_token(SymbolToken(Symbol.Semicolon))
return TypeDefinition(name, type_expression) return TypeDefinition(name, type_expression)
elif lexer.take_token(KeywordToken(Keyword.Defer)):
statement = parse_statement(lexer)
return DeferStatement(statement)
elif lexer.check_tokenkind(KeywordToken) and not lexer.check_tokens(KeywordToken(Keyword.Return), KeywordToken(Keyword.Lambda)): elif lexer.check_tokenkind(KeywordToken) and not lexer.check_tokens(KeywordToken(Keyword.Return), KeywordToken(Keyword.Lambda)):
assert False, ("Unimplemented", lexer.next_token(), lexer.next_token(), lexer.next_token()) assert False, ("Unimplemented", lexer.next_token(), lexer.next_token(), lexer.next_token())
elif lexer.take_token(SymbolToken(Symbol.OpenCurly)): elif lexer.take_token(SymbolToken(Symbol.OpenCurly)):

View File

@ -23,6 +23,7 @@ class Keyword(Enum):
Lambda = 'lambda' Lambda = 'lambda'
Import = 'import' Import = 'import'
Type = 'type' Type = 'type'
Defer = 'defer'
class Symbol(Enum): class Symbol(Enum):
Open = '(' Open = '('