-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.go
More file actions
66 lines (60 loc) · 2.22 KB
/
time.go
File metadata and controls
66 lines (60 loc) · 2.22 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
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
"github.com/Eclalang/time"
)
type Time struct {
functionMap map[string]interface{}
}
func NewTime() *Time {
return &Time{
functionMap: map[string]interface{}{
"convertRoman": nil,
"date": nil,
"now": nil,
"sleep": nil,
"strftime": nil,
"timer": nil,
},
}
}
func (t *Time) 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 := t.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package time", name))
}
switch name {
case "convertRoman":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(time.ConvertRoman(newArgs[0].(string)))}, nil
}
case "date":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Int && reflect.TypeOf(newArgs[1]).Kind() == reflect.Int && reflect.TypeOf(newArgs[2]).Kind() == reflect.Int && reflect.TypeOf(newArgs[3]).Kind() == reflect.Int && reflect.TypeOf(newArgs[4]).Kind() == reflect.Int && reflect.TypeOf(newArgs[5]).Kind() == reflect.Int && len(newArgs) == 6 {
return []eclaType.Type{utils.GoToEclaType(time.Date(newArgs[0].(int), newArgs[1].(int), newArgs[2].(int), newArgs[3].(int), newArgs[4].(int), newArgs[5].(int)))}, nil
}
case "now":
return []eclaType.Type{utils.GoToEclaType(time.Now())}, nil
case "sleep":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Int && len(newArgs) == 1 {
time.Sleep(newArgs[0].(int))
}
case "strftime":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(time.Strftime(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "timer":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Int && len(newArgs) == 1 {
time.Timer(newArgs[0].(int))
}
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package time", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}