-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
75 lines (70 loc) · 2.44 KB
/
encoding.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
enc "github.com/Eclalang/encoding"
)
type Encoding struct {
functionMap map[string]interface{}
}
func NewEncoding() *Encoding {
return &Encoding{
functionMap: map[string]interface{}{
"asciiToString": nil,
"decodeBase64": nil,
"decodeGob": nil,
"decodeHex": nil,
"encodeBase64": nil,
"encodeGob": nil,
"encodeHex": nil,
"stringToAscii": nil,
},
}
}
func (e *Encoding) 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 := e.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package encoding", name))
}
switch name {
case "asciiToString":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.AsciiToString(newArgs[0].([]int)))}, nil
}
case "decodeBase64":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.DecodeBase64(newArgs[0].(string)))}, nil
}
case "decodeGob":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.DecodeGob(newArgs[0].([]int)))}, nil
}
case "decodeHex":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.DecodeHex(newArgs[0].(string)))}, nil
}
case "encodeBase64":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.EncodeBase64(newArgs[0].([]int)))}, nil
}
case "encodeGob":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.EncodeGob(newArgs[0].(string)))}, nil
}
case "encodeHex":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(enc.EncodeHex(newArgs[0].([]int)))}, nil
}
case "stringToAscii":
return []eclaType.Type{utils.GoToEclaType(enc.StringToAscii(newArgs[0].(string)))}, nil
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package encoding", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}