-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
207 lines (178 loc) · 4.03 KB
/
ast.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package dLola
import (
// "errors"
"fmt"
// "log"
// "strconv"
// "strings"
)
type StreamType int
const (
NumT StreamType = iota
BoolT
StringT
Unknown // we use this in the parser for unknow type values (offset expressions) that will be resolved later
LastType = Unknown
)
type StreamName string
func (s StreamName) Sprint() string {
return string(s)
}
func (t StreamType) Sprint() string {
type_names := []string{"num", "bool", "string"}
if t >= LastType {
return ""
}
return fmt.Sprintf("%s", type_names[t])
}
type Position struct {
Line, Col, Offset int
}
type ConstDecl struct { // const int one_sec := 1s
Name StreamName
Type StreamType
Val Expr
Pos Position
}
type InputDecl struct { // input int bar
Name StreamName
Type StreamType
Eval bool
Pos Position
}
type OutputDecl struct { // output int foo /* this is just a decl, later a tick and a def will be given */
Name StreamName
Type StreamType
Pos Position
}
type MonitorDecl struct {
Nid int
Decls []interface{}
}
func NewMonitorDecl(n, d interface{}) MonitorDecl {
return MonitorDecl{n.(IntLiteralExpr).Num, ToSlice(d)}
}
type TopoMonitorDecls struct {
Topo string
Nmons int
MonitorDecls []MonitorDecl
}
func NewTopoMonitorDecls(t, m interface{}) TopoMonitorDecls {
monitorDecls := make([]MonitorDecl, 0)
nmons := 0
for _, m := range ToSlice(m) {
monitorDecls = append(monitorDecls, m.(MonitorDecl))
nmons++
}
return TopoMonitorDecls{t.(Identifier).Val, nmons, monitorDecls}
}
/*lm: not needed type TicksDecl struct {
Name StreamName
Ticks TickingExpr
}*/
type OutputDefinition struct {
Output bool
Name StreamName
Type StreamType
Eval bool
Expr Expr // chango to ValueExpr?
Pos Position
}
func NewPosition(p interface{}) Position {
po := p.(position) //this type is defined in parser.go as part of the PIGEON library
return Position{po.line, po.col, po.offset}
}
func NewConstDecl(n, t, e, p interface{}) ConstDecl {
name := getStreamName(n)
return ConstDecl{name, t.(StreamType), e.(Expr), NewPosition(p)}
}
func NewInputDecl(n, t, le, p interface{}) InputDecl {
name := getStreamName(n)
return InputDecl{name, t.(StreamType), le.(bool), NewPosition(p)}
}
func NewOutputDecl(n, t, p interface{}) OutputDecl {
name := getStreamName(n)
return OutputDecl{name, t.(StreamType), NewPosition(p)}
}
/*: not needed func NewTicksDecl(n, t interface{}) TicksDecl {
name := getStreamName(n)
expr := t.(TickingExpr)
return TicksDecl{name, expr}
}*/
func NewOutputDefinition(o, n, t, le, e, p interface{}) OutputDefinition {
name := getStreamName(n)
expr := e.(Expr)
eval := getEval(le)
return OutputDefinition{o.(bool), name, t.(StreamType), eval, expr, NewPosition(p)}
}
func getStreamName(a interface{}) StreamName {
return StreamName(a.(Identifier).Val)
}
func getEval(le interface{}) bool {
eval := true
v, ok := le.(bool)
if ok {
eval = v
} //if le was not a bool, it will be considered eval
return eval
}
//
// DEPRECATED (MOVED ELSEWHERE)
//
// type Event struct {
// Payload string // changeme
// // Stamp []Tag
// }
// //
// // eval(Event e) bool
// //
// func (p AndPredicate) Eval(e Event) bool {
// return p.Left.Eval(e) && p.Right.Eval(e)
// }
// func (p OrPredicate) Eval(e Event) bool {
// return p.Left.Eval(e) || p.Right.Eval(e)
// }
// func (p NotPredicate) Eval(e Event) bool {
// return !p.Inner.Eval(e)
// }
// func (p TruePredicate) Eval(e Event) bool {
// return true
// }
// func (p FalsePredicate) Eval(e Event) bool {
// return false
// }
//type Monitor Filters
type Tag struct {
// Tag dt.Channel
Tag string
}
type Identifier struct {
Val string
}
type PathName struct {
Val string
}
type QuotedString struct {
Val string
}
type Alphanum struct {
Val string
}
type Keyword struct {
Val string
}
func NewIdentifier(s string) Identifier {
return Identifier{s}
}
func NewPathName(s string) PathName {
return PathName{s}
}
func NewQuotedString(s string) QuotedString {
return QuotedString{s}
}
func ToSlice(v interface{}) []interface{} {
if v == nil {
return nil
}
return v.([]interface{})
}