Skip to content

Commit

Permalink
defined more runtime errs
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jul 31, 2024
1 parent 7e53c9d commit e90754c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions interpreter/interpreter_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
35 changes: 35 additions & 0 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit e90754c

Please sign in to comment.