From 790f1ca6bec127b2943b9db15ab87cf7b51dabfc Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 7 Feb 2024 18:11:12 -0500 Subject: [PATCH] Parse loops --- parser/expression.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/parser/expression.go b/parser/expression.go index a4b2431..e2f580a 100644 --- a/parser/expression.go +++ b/parser/expression.go @@ -62,8 +62,8 @@ func (this *Parser) parseExpressionRootIdent () (entity.Expression, error) { switch name { case "true", "false": return this.parseLiteralBoolean () case "nil": return this.parseLiteralNil() - case "if": // TODO - case "loop": // TODO + case "if": return this.parseIfElse() + case "loop": return this.parseLoop() default: return this.parseVariableOrDeclaration() } panic(this.bug()) @@ -111,3 +111,23 @@ func (this *Parser) parseVariableOrDeclaration () (entity.Expression, error) { return variable, nil } } + +func (this *Parser) parseIfElse () (*entity.IfElse, error) { + // TODO + panic(this.bug()) +} + +func (this *Parser) parseLoop () (*entity.Loop, error) { + err := this.expectDesc("Loop", lexer.Ident) + if err != nil { return nil, err } + pos := this.pos() + + this.next() + body, err := this.parseExpression() + if err != nil { return nil, err } + + return &entity.Loop { + Position: pos, + Body: body, + }, nil +}