Fixed AST inheritance issues and got back to a working state

This commit is contained in:
Mattia Giambirtone 2022-04-28 11:50:10 +02:00
parent f26541d4ae
commit 0e4eb82554
2 changed files with 13 additions and 15 deletions

View File

@ -97,18 +97,16 @@ type
# This is not shown when the node is printed, but makes it a heck of a lot easier to report # This is not shown when the node is printed, but makes it a heck of a lot easier to report
# errors accurately even deep in the compilation pipeline # errors accurately even deep in the compilation pipeline
token*: Token token*: Token
# This weird inheritance chain is just a map of # This weird inheritance chain is needed for the parser to
# our grammar's precedence rules (expressions first, # work properly
# then statements and declarations last) Declaration* = ref object of ASTNode
Expression* = ref object of ASTNode
## An expression
Statement* = ref object of Expression
## A statement
Declaration* = ref object of Statement
## A declaration ## A declaration
closedOver*: bool closedOver*: bool
pragmas*: seq[Token] pragmas*: seq[Token]
Statement* = ref object of Declaration
## A statement
Expression* = ref object of Statement
## An expression
LiteralExpr* = ref object of Expression LiteralExpr* = ref object of Expression
# Using a string for literals makes it much easier to handle numeric types, as # Using a string for literals makes it much easier to handle numeric types, as
# there is no overflow nor underflow or float precision issues during parsing. # there is no overflow nor underflow or float precision issues during parsing.
@ -375,7 +373,7 @@ proc newGroupingExpr*(expression: Expression, token: Token): GroupingExpr =
result.token = token result.token = token
proc newLambdaExpr*(arguments: seq[tuple[name: IdentExpr, valueType: IdentExpr]], defaults: seq[Expression], body: Declaration, proc newLambdaExpr*(arguments: seq[tuple[name: IdentExpr, valueType: IdentExpr]], defaults: seq[Expression], body: Statement,
isGenerator: bool, isAsync: bool, token: Token): LambdaExpr = isGenerator: bool, isAsync: bool, token: Token): LambdaExpr =
result = LambdaExpr(kind: lambdaExpr) result = LambdaExpr(kind: lambdaExpr)
result.body = body result.body = body

View File

@ -320,13 +320,13 @@ proc primary(self: Parser): Expression =
result = newInfExpr(self.step()) result = newInfExpr(self.step())
of Function: of Function:
discard self.step() discard self.step()
result = self.funDecl(isLambda=true) result = Expression(self.funDecl(isLambda=true))
of Coroutine: of Coroutine:
discard self.step() discard self.step()
result = self.funDecl(isAsync=true, isLambda=true) result = Expression(self.funDecl(isAsync=true, isLambda=true))
of Generator: of Generator:
discard self.step() discard self.step()
result = self.funDecl(isGenerator=true, isLambda=true) result = Expression(self.funDecl(isGenerator=true, isLambda=true))
else: else:
self.error("invalid syntax") self.error("invalid syntax")
@ -773,7 +773,7 @@ proc forStmt(self: Parser): Statement =
if increment != nil: if increment != nil:
# The increment runs after each iteration, so we # The increment runs after each iteration, so we
# inject it into the block as the last statement # inject it into the block as the last statement
body = newBlockStmt(@[Declaration(body), Declaration(Expression(newExprStmt(increment, increment.token)))], tok) body = newBlockStmt(@[Declaration(body), newExprStmt(increment, increment.token)], tok)
if condition == nil: if condition == nil:
## An empty condition is functionally ## An empty condition is functionally
## equivalent to "true" ## equivalent to "true"
@ -894,7 +894,7 @@ proc funDecl(self: Parser, isAsync: bool = false, isGenerator: bool = false, isL
self.currentFunction = enclosingFunction self.currentFunction = enclosingFunction
return result return result
elif isLambda: elif isLambda:
self.currentFunction = Declaration(Expression(newLambdaExpr(arguments, defaults, Declaration(Statement(newBlockStmt(@[], Token()))), isGenerator=isGenerator, isAsync=isAsync, token=tok))) self.currentFunction = newLambdaExpr(arguments, defaults, newBlockStmt(@[], Token()), isGenerator=isGenerator, isAsync=isAsync, token=tok)
elif not isOperator: elif not isOperator:
self.error("funDecl: invalid state") self.error("funDecl: invalid state")
if self.match(Colon): if self.match(Colon):