-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast.go
More file actions
62 lines (57 loc) · 1.89 KB
/
cast.go
File metadata and controls
62 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
"github.com/Eclalang/cast"
)
type Cast struct {
functionMap map[string]interface{}
}
func NewCast() *Cast {
return &Cast{
functionMap: map[string]interface{}{
"atoi": nil,
"floatToInt": nil,
"intToFloat": nil,
"parseBool": nil,
"parseFloat": nil,
},
}
}
func (c *Cast) Call(name string, args []eclaType.Type) ([]eclaType.Type, error) {
newArgs := make([]any, len(args))
for k, arg := range args {
newArgs[k] = utils.EclaTypeToGo(arg)
}
if _, ok := c.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package cast", name))
}
switch name {
case "atoi":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(cast.Atoi(newArgs[0].(string)))}, nil
}
case "floatToInt":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Float64 && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(cast.FloatToInt(newArgs[0].(float64)))}, nil
}
case "intToFloat":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Int && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(cast.IntToFloat(newArgs[0].(int)))}, nil
}
case "parseBool":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(cast.ParseBool(newArgs[0].(string)))}, nil
}
case "parseFloat":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.Int && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(cast.ParseFloat(newArgs[0].(string), newArgs[1].(int)))}, nil
}
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package cast", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}