Skip to content

Commit

Permalink
handle error in bool
Browse files Browse the repository at this point in the history
  • Loading branch information
tot0p committed Feb 28, 2024
1 parent 6014c18 commit ec9ac80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 10 additions & 2 deletions interpreter/Decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func New(t parser.Literal, env *Env) *Bus {
}
return NewMainBus(str)
case lexer.BOOL:
return NewMainBus(eclaType.NewBool(t.Value))
b, err := eclaType.NewBool(t.Value)
if err != nil {
env.ErrorHandle.HandleError(t.StartLine(), t.StartPos(), err.Error(), errorHandler.LevelFatal)
}
return NewMainBus(b)
case lexer.FLOAT:
return NewMainBus(eclaType.NewFloat(t.Value))
case lexer.CHAR:
Expand Down Expand Up @@ -69,7 +73,11 @@ func RunVariableDecl(tree parser.VariableDecl, env *Env) {
}
env.SetVar(tree.Name, v)
case parser.Bool:
v, err := eclaType.NewVar(tree.Name, tree.Type, eclaType.NewBool("false"))
b, err := eclaType.NewBool("false")
if err != nil {
env.ErrorHandle.HandleError(tree.StartLine(), tree.StartPos(), err.Error(), errorHandler.LevelFatal)
}
v, err := eclaType.NewVar(tree.Name, tree.Type, b)
if err != nil {
env.ErrorHandle.HandleError(tree.StartLine(), tree.StartPos(), err.Error(), errorHandler.LevelFatal)
}
Expand Down
9 changes: 6 additions & 3 deletions interpreter/eclaType/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
)

// NewBool creates a new Bool
func NewBool(value string) Bool {
result, _ := strconv.ParseBool(value)
return Bool(result)
func NewBool(value string) (Bool, error) {
result, err := strconv.ParseBool(value)
if err != nil {
return false, errors.New("cannot convert " + value + " to bool")
}
return Bool(result), nil
}

type Bool bool
Expand Down
5 changes: 4 additions & 1 deletion interpreter/eclaType/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ func TestOrBoolChar(t *testing.T) {
// Bool interacts with String

func TestAddBoolString(t *testing.T) {
t1 := NewBool("true")
t1, err := NewBool("true")
if err != nil {
t.Errorf("Error: %s", err)
}
t2, _ := NewString("Hello")
actual, err := t1.Add(t2)
if err != nil {
Expand Down

0 comments on commit ec9ac80

Please sign in to comment.