fix if/then errors

This commit is contained in:
Sky Johnson 2025-06-10 09:46:17 -05:00
parent 8a8801f1c0
commit 29044dc74c
2 changed files with 22 additions and 2 deletions

View File

@ -157,7 +157,18 @@ func (p *Parser) parseIfStatement() *IfStatement {
return nil
}
p.nextToken() // move past condition
// Optional 'then' keyword
if p.peekTokenIs(THEN) {
p.nextToken()
}
p.nextToken() // move past condition (and optional 'then')
// Check if we immediately hit END (missing body)
if p.curTokenIs(END) {
p.addError("expected 'end' to close if statement")
return nil
}
// Parse if body
stmt.Body = p.parseBlockStatements(ELSEIF, ELSE, END)
@ -174,7 +185,12 @@ func (p *Parser) parseIfStatement() *IfStatement {
return nil
}
p.nextToken() // move past condition
// Optional 'then' keyword
if p.peekTokenIs(THEN) {
p.nextToken()
}
p.nextToken() // move past condition (and optional 'then')
elseif.Body = p.parseBlockStatements(ELSEIF, ELSE, END)
stmt.ElseIfs = append(stmt.ElseIfs, elseif)
@ -563,6 +579,8 @@ func tokenTypeString(t TokenType) string {
return "var"
case IF:
return "if"
case THEN:
return "then"
case ELSEIF:
return "elseif"
case ELSE:

View File

@ -29,6 +29,7 @@ const (
// Keywords
VAR
IF
THEN
ELSEIF
ELSE
END
@ -74,6 +75,7 @@ func lookupIdent(ident string) TokenType {
"false": FALSE,
"nil": NIL,
"if": IF,
"then": THEN,
"elseif": ELSEIF,
"else": ELSE,
"end": END,