diff --git a/interpret.go b/interpret.go index 105e48f..1ae2f93 100644 --- a/interpret.go +++ b/interpret.go @@ -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)) +} diff --git a/interpret_test.go b/interpret_test.go index b4ce94b..2434c18 100644 --- a/interpret_test.go +++ b/interpret_test.go @@ -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)