-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from NuruProgramming/fh/add-type-casting
Fh/add type casting
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package evaluator | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/NuruProgramming/Nuru/object" | ||
) | ||
|
||
func convertToInteger(obj object.Object) object.Object { | ||
switch obj := obj.(type) { | ||
case *object.Integer: | ||
return obj | ||
case *object.Float: | ||
return &object.Integer{Value: int64(obj.Value)} | ||
case *object.String: | ||
i, err := strconv.ParseInt(obj.Value, 10, 64) | ||
if err != nil { | ||
return newError("Haiwezi kubadilisha '%s' kuwa NAMBA", obj.Value) | ||
} | ||
return &object.Integer{Value: i} | ||
case *object.Boolean: | ||
if obj.Value { | ||
return &object.Integer{Value: 1} | ||
} | ||
return &object.Integer{Value: 0} | ||
default: | ||
return newError("Haiwezi kubadilisha %s kuwa NAMBA", obj.Type()) | ||
} | ||
} | ||
|
||
func convertToFloat(obj object.Object) object.Object { | ||
switch obj := obj.(type) { | ||
case *object.Float: | ||
return obj | ||
case *object.Integer: | ||
return &object.Float{Value: float64(obj.Value)} | ||
case *object.String: | ||
f, err := strconv.ParseFloat(obj.Value, 64) | ||
if err != nil { | ||
return newError("Haiwezi kubadilisha '%s' kuwa DESIMALI", obj.Value) | ||
} | ||
return &object.Float{Value: f} | ||
case *object.Boolean: | ||
if obj.Value { | ||
return &object.Float{Value: 1.0} | ||
} | ||
return &object.Float{Value: 0.0} | ||
default: | ||
return newError("Haiwezi kubadilisha %s kuwa DESIMALI", obj.Type()) | ||
} | ||
} | ||
|
||
func convertToString(obj object.Object) object.Object { | ||
return &object.String{Value: obj.Inspect()} | ||
} | ||
|
||
func convertToBoolean(obj object.Object) object.Object { | ||
switch obj := obj.(type) { | ||
case *object.Boolean: | ||
return obj | ||
case *object.Integer: | ||
return &object.Boolean{Value: obj.Value != 0} | ||
case *object.Float: | ||
return &object.Boolean{Value: obj.Value != 0} | ||
case *object.String: | ||
return &object.Boolean{Value: len(obj.Value) > 0} | ||
case *object.Null: | ||
return &object.Boolean{Value: false} | ||
default: | ||
return &object.Boolean{Value: true} | ||
} | ||
} |