parsing break, var decl

This commit is contained in:
prod2 2022-12-03 09:39:26 +01:00
parent 1e139194b8
commit 27391bdc43
1 changed files with 19 additions and 3 deletions

View File

@ -109,17 +109,18 @@ proc peek(parser: Parser): Token =
proc peekMatch(parser: Parser, tokenType: TokenType): bool =
parser.peek().tokenType == tokenType
proc isAtEnd(parser: Parser): bool =
parser.current.tokenType == tkEof
proc synchronize(parser: Parser) =
parser.panicMode = false
while parser.current.tokenType != tkEof:
while not parser.isAtEnd():
if parser.previous.get().tokenType in {tkSemicolon}:
return
if parser.current.tokenType in {tkProc, tkVar, tkFor, tkIf, tkWhile, tkRightBrace}:
return
parser.advance()
proc isAtEnd(parser: Parser): bool =
parser.current.tokenType == tkEof
# EXPRESSIONS
proc expression(parser: Parser): Node
@ -464,6 +465,21 @@ proc statement(parser: Parser, inBlock: bool = false): Node =
let funct = parser.parseProcDeclaration()
result = Node(kind: nkVarDecl, name: varname, value: funct)
discard parser.consume(tkSemicolon, "';' expected after procedure declaration.")
elif parser.match(tkBreak):
if parser.match(tkLabel):
result = Node(kind: nkBreak, label: parser.previous.get().text[1..^1])
else:
result = Node(kind: nkBreak, label: "")
discard parser.consume(tkSemicolon, "';' expected after break statement.")
elif parser.match(tkVar):
discard parser.consume(tkIdentifier, "Identifier expected after 'var'.")
let name = parser.previous.get().text
if parser.match(tkEqual):
let val = parser.expression()
result = Node(kind: nkVarDecl, name: name, value: val)
else:
result = Node(kind: nkVarDecl, name: name, value: nil)
discard parser.consume(tkSemicolon, "';' expected after variable declaration.")
else:
result = parser.exprStatement(inBlock)