This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute.go
155 lines (143 loc) · 3.72 KB
/
execute.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package yoo
import (
"fmt"
"errors"
"io/ioutil"
)
var modules = *GetBindings()
var global = GetGlobal()
func getObject(o Any, variables *Variables) Any {
if i, ok := o.(*Identifier); ok {
return (*variables)[i.text]
}
return o
}
func Execute(object *Block, variables *Variables) (Any, error) {
block := *object
i := 0
stack := make([]Block, 99999999)
indexes := make([]int, 99999999)
maxes := make([]int, 99999999)
stack[0] = block
maxes[0] = len(block)
indexes[0] = 0
for indexes[i] < maxes[i] {
switch o := stack[i][indexes[i]].(type) {
case *Call:
callee, err := Execute(o.callee, variables)
if err != nil { return nil, err }
argsA := *o.args
length := len(argsA)
args := make([]Any, length, length)
for k, v := range argsA {
arg, err := Execute(v, variables, variables2)
if err != nil { return nil, err }
args[k] = arg
}
if fn, ok := callee.(*TSFunction); ok {
a := (*fn)(&args)
return a, nil
}
return nil, errors.New(fmt.Sprintf("%+v is not a function.", callee))
case *Member:
left, err := Execute(o.left, variables, variables2)
if err != nil { return nil, err }
rightA, ok := o.right.(*Identifier)
var right string
if ok {
right = rightA.text
} else {
ret, err := Execute(o.right, variables, variables2)
if err != nil { return nil, err }
right = ret.(string)
}
return GetValue(left, right), nil
case *Variable:
vars := *variables
for _, elem := range (*o) {
v, err := Execute(elem.value, variables, variables2)
if err != nil { return nil, err }
vars[elem.name.text] = v
}
case *Import:
name := o.path
module := modules[name]
if module == nil { return nil, errors.New("No such module: " + name) }
vars := *variables
for _, elem := range (*o.importeds) {
vars[elem.name.text] = GetValue(module, elem.prop.text)
}
case *ArrowFunction:
pars := *o.args
var fn TSFunction = func (args *[]Any) Any {
vars := copyMap(variables)
varsV := *vars
argsV := *args
length := len(argsV) - 1
for i, v := range pars {
var value Any
if i <= length {
value = argsV[i]
}
if value == nil {
value = v.value
}
varsV[v.name.text] = value
}
for _, v := range (*o.body) {
Execute(v, vars, variables)
}
return nil
}
return &fn, nil
case *TSClass:
cls := CreateObjectByLength(o.length)
(*o.constructor)(&[]Any { cls })
return cls, nil
}
}
return nil, nil
}
func ExecuteFile(file string) error {
f, err := ioutil.ReadFile("a.yoo")
if err != nil { return err }
return ExecuteBytes(f)
}
func ExecuteBytes(buff []byte) error {
ba := CreateByteArray(buff)
head, err := ba.ReadString(3)
if err != nil || head != "YOO" {
return errors.New("Header error")
}
version, _ := ba.ReadInt8()
if version != VERSION {
return errors.New("Version error")
}
length, err := ba.ReadInt16()
if err != nil {
return errors.New("Strings pool error")
}
pool := make([]string, length, length)
for i := int16(0); i < length; i++ {
len, err := ba.ReadInt16()
if err != nil {
return err
}
str, err := ba.ReadString(int(len))
if err != nil {
return err
}
pool[i] = str
}
poolPtr := &pool
vars := copyMap(global)
length--
for length > 0 {
length--
obj, err := expression(ba, poolPtr)
if err != nil { return err }
_, err = Execute(obj, vars, nil)
if err != nil { return err }
}
return nil
}