Skip to content

Commit

Permalink
Parsing gotos and labels is very confusing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaenalos committed Dec 20, 2023
1 parent 47f95be commit 5596af4
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/prometheus/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,17 @@ function Parser:statement(scope, currentLoop)

-- Labels
if consume(self, TokenKind.Symbol, "::") then
local ident = expect(self, TokenKind.Ident)
local label = expect(self, TokenKind.Ident).value;
if expect(self, TokenKind.Symbol, "::") then
local label = ident.value;
if currentLoop then
scope = Scope:new(scope)
end
if scope:hasVariable(label) then
local labelScope, labelID = scope:resolve(label);
return Ast.LabelStatement(labelID, labelScope), currentLoop
return Ast.LabelStatement(labelID, labelScope)
else
local id = scope:addVariable(label, ident);
return Ast.LabelStatement(id, scope), currentLoop;
return Ast.LabelStatement(id, scope)
end
end
end
Expand All @@ -206,12 +208,12 @@ function Parser:statement(scope, currentLoop)
local label = ident.value;
if scope:hasVariable(label) then
local labelScope, labelID = scope:resolve(label);
return Ast.GotoStatement(labelID, labelScope), currentLoop;
return Ast.GotoStatement(labelID, labelScope)
else
local id = scope:addVariable(label, ident);
return Ast.GotoStatement(id, scope), currentLoop;
return Ast.GotoStatement(id, scope)
end
end
end

-- Break Statement - only valid inside of Loops
if(consume(self, TokenKind.Keyword, "break")) then
Expand Down

0 comments on commit 5596af4

Please sign in to comment.