Add parsing for defers
This commit is contained in:
parent
659c79373d
commit
2cee127d8f
32
ppp_ast.py
32
ppp_ast.py
@ -70,7 +70,7 @@ class Statements(Statement):
|
||||
statements: List[Statement]
|
||||
|
||||
### Enums + Struct ###
|
||||
|
||||
|
||||
@dataclass
|
||||
class EnumEntry:
|
||||
name: str
|
||||
@ -129,9 +129,9 @@ class Variable(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
def precedence(self) -> int: return 13
|
||||
|
||||
|
||||
@dataclass
|
||||
class ArrayAccess(Expression):
|
||||
array: Expression
|
||||
@ -326,7 +326,7 @@ class NotEqual(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" != "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 7
|
||||
|
||||
@dataclass
|
||||
@ -397,7 +397,7 @@ class Addition(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" + "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 10
|
||||
|
||||
@dataclass
|
||||
@ -407,7 +407,7 @@ class Subtract(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" - "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 10
|
||||
|
||||
@dataclass
|
||||
@ -417,7 +417,7 @@ class Multiplication(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" * "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 11
|
||||
|
||||
@dataclass
|
||||
@ -427,7 +427,7 @@ class Division(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" / "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 11
|
||||
|
||||
@dataclass
|
||||
@ -437,7 +437,7 @@ class Modulo(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return self.wrap(self.lhs)+" % "+self.wrap(self.rhs)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 11
|
||||
|
||||
@dataclass
|
||||
@ -446,7 +446,7 @@ class Bnot(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return "~"+self.wrap(self.expression)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 12
|
||||
|
||||
@dataclass
|
||||
@ -455,7 +455,7 @@ class Not(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return "!"+self.wrap(self.expression)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 12
|
||||
|
||||
@dataclass
|
||||
@ -464,7 +464,7 @@ class UnaryPlus(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return "+"+self.wrap(self.expression)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 12
|
||||
|
||||
@dataclass
|
||||
@ -473,7 +473,7 @@ class UnaryMinus(Expression):
|
||||
|
||||
def represent(self) -> str:
|
||||
return "-"+self.wrap(self.expression)
|
||||
|
||||
|
||||
def precedence(self) -> int: return 12
|
||||
|
||||
@dataclass
|
||||
@ -542,4 +542,8 @@ class Import(Statement):
|
||||
@dataclass
|
||||
class TypeDefinition(Statement):
|
||||
name: str
|
||||
expression: TypeExpression
|
||||
expression: TypeExpression
|
||||
|
||||
@dataclass
|
||||
class DeferStatement(Statement):
|
||||
statement: Statement
|
||||
|
@ -656,6 +656,8 @@ def interpret_statements(statements: List_[Statement], program: ProgramState) ->
|
||||
program.modules[file.str] = module
|
||||
case TypeDefinition(name, expression_):
|
||||
program.declare_and_assign_variable(name, TypeObject(calculate_type_expression(expression_, program)))
|
||||
case DeferStatement(statement=statement):
|
||||
assert False, "TODO: Defers are not implemented"
|
||||
case _:
|
||||
assert False, ("Unimplemented", statement)
|
||||
return NothingResult()
|
||||
|
@ -358,6 +358,9 @@ def parse_statement(lexer: Lexer) -> Statement:
|
||||
type_expression = parse_type(lexer)
|
||||
lexer.assert_token(SymbolToken(Symbol.Semicolon))
|
||||
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)):
|
||||
assert False, ("Unimplemented", lexer.next_token(), lexer.next_token(), lexer.next_token())
|
||||
elif lexer.take_token(SymbolToken(Symbol.OpenCurly)):
|
||||
|
@ -23,6 +23,7 @@ class Keyword(Enum):
|
||||
Lambda = 'lambda'
|
||||
Import = 'import'
|
||||
Type = 'type'
|
||||
Defer = 'defer'
|
||||
|
||||
class Symbol(Enum):
|
||||
Open = '('
|
||||
|
Loading…
Reference in New Issue
Block a user