umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs

commit 65428752a41df1171c0ba4735b8f6f7c4766dc88
parent b438a9f973e08ee8d875e56c9d51e65d322bd6b3
Author: bsandro <email@bsandro.tech>
Date:   Fri, 17 Jun 2022 02:42:22 +0300

Parentheses grouping support

Diffstat:
Mparser/parser.go | 11+++++++++++
Mparser/parser_test.go | 3+++
2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/parser/parser.go b/parser/parser.go @@ -70,6 +70,8 @@ func New(l *lexer.Lexer) *Parser { p.registerPrefix(token.TRUE, p.parseBoolean) p.registerPrefix(token.FALSE, p.parseBoolean) + p.registerPrefix(token.LPAREN, p.parseGroupedExpression) + return p } @@ -253,3 +255,12 @@ func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression { func (p *Parser) parseBoolean() ast.Expression { return &ast.Boolean{Token: p.curToken, Value: p.curTokenIs(token.TRUE)} } + +func (p *Parser) parseGroupedExpression() ast.Expression { + p.nextToken() + expr := p.parseExpression(LOWEST) + if !p.expectPeek(token.RPAREN) { + return nil + } + return expr +} diff --git a/parser/parser_test.go b/parser/parser_test.go @@ -347,6 +347,9 @@ func TestOperatorPrecedenceParsing(t *testing.T) { {"true", "true"}, {"false", "false"}, {"3<5==true==!false", "{{{3 < 5} == true} == {!false}}"}, + {"!(true == true)", "{!{true == true}}"}, + {"2/(5+5)", "{2 / {5 + 5}}"}, + {"-(5+5)", "{-{5 + 5}}"}, } for _, tt := range tests {