From 3b08932403f27e5091e1abf74eac3d292b4ffaf3 Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Tue, 16 Nov 2021 13:32:12 +0100 Subject: [PATCH] Improved error handling for declarations inside functions --- src/backend/parser.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/backend/parser.nim b/src/backend/parser.nim index ff68bb2..80f8eec 100644 --- a/src/backend/parser.nim +++ b/src/backend/parser.nim @@ -735,8 +735,8 @@ proc forStmt(self: Parser): ASTNode = self.expect(RightParen, "unterminated for loop condition") var body = self.statement() if increment != nil: - # The increment runs at each iteration, so we - # inject it into the block as the first statement + # The increment runs after each iteration, so we + # inject it into the block as the last statement body = newBlockStmt(@[body, increment], tok) if condition == nil: ## An empty condition is functionally @@ -778,6 +778,10 @@ proc varDecl(self: Parser, isStatic: bool = true, isPrivate: bool = true): ASTNo keyword = "constant" else: keyword = "variable" + if not isStatic and self.currentFunction != nil: + self.error("dynamic declarations are illegal inside functions") + if not isPrivate and self.currentFunction != nil: + self.error("public declarations are illegal inside functions") self.expect(Identifier, &"expecting {keyword} name after '{varKind.lexeme}'") var name = newIdentExpr(self.peek(-1)) if self.match(Equal):