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

View File

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