diff --git a/interpreter/interpreter.go b/interpreter/interpreter.go index 9aaf7f0..7bc62e8 100644 --- a/interpreter/interpreter.go +++ b/interpreter/interpreter.go @@ -111,7 +111,7 @@ func (s *programState) parseVars(varDeclrs []parser.VarDeclaration, rawVars map[ if varsDecl.Origin == nil { raw, ok := rawVars[varsDecl.Name.Name] if !ok { - panic("TODO handle var not found: " + varsDecl.Name.Name) + return MissingVariableErr{Name: varsDecl.Name.Name} } parsed, err := parseVar(varsDecl.Type.Name, raw) if err != nil { @@ -200,7 +200,7 @@ func (st *programState) evaluateLit(literal parser.Literal) (Value, error) { case *parser.VariableLiteral: value, ok := st.Vars[literal.Name] if !ok { - panic("TODO err for unbound variable") + return nil, UnboundVariableErr{Name: literal.Name} } return value, nil default: diff --git a/interpreter/interpreter_error.go b/interpreter/interpreter_error.go index a8a9c4c..e7cb793 100644 --- a/interpreter/interpreter_error.go +++ b/interpreter/interpreter_error.go @@ -22,3 +22,19 @@ type TypeError struct { func (e TypeError) Error() string { return fmt.Sprintf("Invalid value received. Expecting value of type %s (got %s instead)", e.Expected, e.Value.String()) } + +type UnboundVariableErr struct { + Name string +} + +func (e UnboundVariableErr) Error() string { + return fmt.Sprintf("Unbound variable: %s", e.Name) +} + +type MissingVariableErr struct { + Name string +} + +func (e MissingVariableErr) Error() string { + return fmt.Sprintf("Variable is missing in json: %s", e.Name) +} diff --git a/interpreter/interpreter_test.go b/interpreter/interpreter_test.go index a3016e9..80ac03d 100644 --- a/interpreter/interpreter_test.go +++ b/interpreter/interpreter_test.go @@ -1383,4 +1383,39 @@ func TestErrors(t *testing.T) { test(t, tc) }) + t.Run("unbound variable", func(t *testing.T) { + tc := NewTestCase() + tc.compile(t, ` + send $unbound_var ( + source = @a + destination = @b + )`) + + tc.expected = CaseResult{ + Error: machine.UnboundVariableErr{ + Name: "unbound_var", + }, + } + test(t, tc) + }) + + t.Run("missing variable from json", func(t *testing.T) { + tc := NewTestCase() + tc.compile(t, ` + vars { + monetary $x + } + + send $x ( + source = @a + destination = @b + )`) + + tc.expected = CaseResult{ + Error: machine.MissingVariableErr{ + Name: "x", + }, + } + test(t, tc) + }) }