parsing break, var decl
This commit is contained in:
parent
1e139194b8
commit
27391bdc43
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue