Removed the need for parentheses around control flow and looping statements, enforced braces instead. Got rid of the old C-style for loop

This commit is contained in:
Mattia Giambirtone 2022-07-16 13:21:40 +02:00
parent b40275b52f
commit da2cfefe75
2 changed files with 18 additions and 19 deletions

View File

@ -641,16 +641,14 @@ proc raiseStmt(self: Parser): Statement =
proc forEachStmt(self: Parser): Statement =
## Parses C#-like foreach loops
let tok = self.peek(-1)
var enclosingLoop = self.currentLoop
let enclosingLoop = self.currentLoop
self.currentLoop = Loop
self.expect(LeftParen, "expecting '(' after 'foreach'")
self.expect(Identifier)
var identifier = newIdentExpr(self.peek(-1), self.scopeDepth)
let identifier = newIdentExpr(self.peek(-1), self.scopeDepth)
self.expect(":")
var expression = self.expression()
self.expect(RightParen)
var body = self.statement()
result = newForEachStmt(identifier, expression, body, tok)
let expression = self.expression()
self.expect(LeftBrace)
result = newForEachStmt(identifier, expression, self.blockStmt(), tok)
self.currentLoop = enclosingLoop
@ -700,16 +698,15 @@ proc whileStmt(self: Parser): Statement =
## Parses a C-style while loop statement
let tok = self.peek(-1)
self.beginScope()
var enclosingLoop = self.currentLoop
let enclosingLoop = self.currentLoop
let condition = self.expression()
self.expect(LeftBrace)
self.currentLoop = Loop
self.expect(LeftParen, "expecting '(' before while loop condition")
var condition = self.expression()
self.expect(RightParen, "unterminated while loop condition")
result = newWhileStmt(condition, self.statement(), tok)
result = newWhileStmt(condition, self.blockStmt(), tok)
self.currentLoop = enclosingLoop
self.endScope()
#[
proc forStmt(self: Parser): Statement =
## Parses a C-style for loop
self.beginScope()
@ -766,18 +763,18 @@ proc forStmt(self: Parser): Statement =
result = body
self.currentLoop = enclosingLoop
self.endScope()
]#
proc ifStmt(self: Parser): Statement =
## Parses if statements
let tok = self.peek(-1)
self.expect(LeftParen, "expecting '(' before if condition")
var condition = self.expression()
self.expect(RightParen, "expecting ')' after if condition")
var thenBranch = self.statement()
let condition = self.expression()
self.expect(LeftBrace)
let thenBranch = self.blockStmt()
var elseBranch: Statement
if self.match(Else):
elseBranch = self.statement()
elseBranch = self.blockStmt()
result = newIfStmt(condition, thenBranch, elseBranch, tok)
@ -1088,9 +1085,11 @@ proc statement(self: Parser): Statement =
of While:
discard self.step()
result = self.whileStmt()
#[
of For:
discard self.step()
result = self.forStmt()
]#
of Foreach:
discard self.step()
result = self.forEachStmt()

View File

@ -194,7 +194,7 @@ proc runFile(f: string, interactive: bool = false, fromString: bool = false) =
tokenizer = newLexer()
parser = newParser()
compiler = newCompiler()
debugger = newDebugger()
debugger {.used.} = newDebugger()
serializer = newSerializer()
vm = newPeonVM()
input: string