-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.go
More file actions
73 lines (67 loc) · 3 KB
/
regex.go
File metadata and controls
73 lines (67 loc) · 3 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
63
64
65
66
67
68
69
70
71
72
73
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
"github.com/Eclalang/regex"
)
type Regex struct {
functionMap map[string]interface{}
}
func NewRegex() *Regex {
return &Regex{
functionMap: map[string]interface{}{
"find": nil,
"findAll": nil,
"findAllIndex": nil,
"findIndex": nil,
"match": nil,
"replace": nil,
"replaceAll": nil,
},
}
}
func (r *Regex) 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 := r.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package regex", name))
}
switch name {
case "find":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(regex.Find(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "findAll":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(regex.FindAll(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "findAllIndex":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(regex.FindAllIndex(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "findIndex":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(regex.FindIndex(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "match":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(regex.Match(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "replace":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.String && reflect.TypeOf(newArgs[3]).Kind() == reflect.Int && len(newArgs) == 4 {
return []eclaType.Type{utils.GoToEclaType(regex.Replace(newArgs[0].(string), newArgs[1].(string), newArgs[2].(string), newArgs[3].(int)))}, nil
}
case "replaceAll":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.String && len(newArgs) == 3 {
return []eclaType.Type{utils.GoToEclaType(regex.ReplaceAll(newArgs[0].(string), newArgs[1].(string), newArgs[2].(string)))}, nil
}
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package regex", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}