Skip to content

Commit

Permalink
add parenthesis evaluation (for ints)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopedraza committed Feb 27, 2024
1 parent 5404bb8 commit f8b6b97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions interpret.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func (i *Interpreter) VisitInteger(ctx *parsing.IntegerContext) int {
func (i *Interpreter) VisitSum(ctx *parsing.SumContext) interface{} {
return i.VisitInteger(ctx.Exp(0).(*parsing.IntegerContext)) + i.VisitInteger(ctx.Exp(1).(*parsing.IntegerContext))
}

func (i *Interpreter) VisitParens(ctx *parsing.ParensContext) interface{} {
return i.VisitInteger(ctx.Exp().(*parsing.IntegerContext))
}
11 changes: 11 additions & 0 deletions interpret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ func TestIntEval(t *testing.T) {
assert.Equal(t, 1, interp.VisitInteger(tree), "'1' should eval to 1")
}

func TestParens(t *testing.T) {
input := antlr.NewInputStream("(1)")
lexer := parsing.NewlangLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parsing.NewlangParser(stream)
tree := p.Exp().(*parsing.ParensContext)
interp := &Interpreter{}

assert.Equal(t, 1, interp.VisitParens(tree), "'(1)' should eval to 1")
}

func TestIntSumEval(t *testing.T) {
input := antlr.NewInputStream("1+1")
lexer := parsing.NewlangLexer(input)
Expand Down

0 comments on commit f8b6b97

Please # to comment.