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
/
Copy pathexpressions.go
133 lines (121 loc) · 3.54 KB
/
expressions.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
package yoo
type Block []Any
func NewBlock(ba *ByteArray, pool *[]string) (*Block, error) {
length, err := ba.ReadInt16()
if err != nil { return nil, err }
block := make(Block, length, length)
for i := int16(0); i < length; i++ {
expr, err := expression(ba, pool)
if err != nil { return nil, err }
block[i] = expr
}
return &block, nil
}
type Identifier struct {
text string
}
func NewIdentifier(ba *ByteArray, pool *[]string) (*Identifier, error) {
id, err := ba.ReadInt16()
if err != nil { return nil, err }
return &Identifier{ text: (*pool)[int(id)] }, nil
}
type Call struct {
callee Any
args *[]Any
}
func NewCall(ba *ByteArray, pool *[]string) (*Call, error) {
callee, err := expression(ba, pool)
if err != nil { return nil, err }
length, err := ba.ReadInt16()
if err != nil { return nil, err }
args := make([]Any, length, length)
for i := int16(0); i < length; i++ {
arg, err := expression(ba, pool)
if err != nil { return nil, err }
args[i] = &arg
}
return &Call{ callee: callee, args: &args }, nil
}
type Member struct {
left Any
right Any
}
func NewMember(ba *ByteArray, pool *[]string) (*Member, error) {
left, err := expression(ba, pool)
if err != nil { return nil, err }
right, err := expression(ba, pool)
if err != nil { return nil, err }
return &Member{ left: left, right: right }, nil
}
type VariableElement struct {
name *Identifier
value Any
}
type Variable []*VariableElement
func NewVariable(ba *ByteArray, pool *[]string) (*Variable, error) {
length, err := ba.ReadInt16()
if err != nil { return nil, err }
vars := make(Variable, length, length)
for i := int16(0); i < length; i++ {
left, err := expression(ba, pool)
if err != nil { return nil, err }
right, err := expression(ba, pool)
if err != nil { return nil, err }
vars[i] = &VariableElement{ name: left.(*Identifier), value: right }
}
return &vars, nil
}
type Imported struct {
name *Identifier
prop *Identifier
}
type Import struct {
path string
importeds *[]*Imported
}
func NewImport(ba *ByteArray, pool *[]string) (*Import, error) {
path, err := expression(ba, pool)
if err != nil { return nil, err }
length, err := ba.ReadInt16()
if err != nil { return nil, err }
imports := make([]*Imported, length, length)
for i := int16(0); i < length; i++ {
name, err := expression(ba, pool)
if err != nil { return nil, err }
prop, err := expression(ba, pool)
if err != nil { return nil, err }
imports[i] = &Imported{ name: name.(*Identifier), prop: prop.(*Identifier) }
}
return &Import{ path: path.(string), importeds: &imports }, nil
}
type ArrowFunction struct {
async bool
args *Variable
body *Block
}
func NewArrowFunction(ba *ByteArray, pool *[]string) (*ArrowFunction, error) {
async, err := expression(ba, pool)
if err != nil { return nil, err }
args, err := NewVariable(ba, pool)
if err != nil { return nil, err }
block, err := NewBlock(ba, pool)
if err != nil { return nil, err }
return &ArrowFunction{ async: async.(bool), args: args, body: block }, nil
}
type MakeClass struct {
class Any
args *[]Any
}
func NewMakeClass(ba *ByteArray, pool *[]string) (*MakeClass, error) {
callee, err := expression(ba, pool)
if err != nil { return nil, err }
length, err := ba.ReadInt16()
if err != nil { return nil, err }
args := make([]Any, length, length)
for i := int16(0); i < length; i++ {
arg, err := expression(ba, pool)
if err != nil { return nil, err }
args[i] = &arg
}
return &MakeClass{ class: callee, args: &args }, nil
}