-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
248 lines (217 loc) · 8.03 KB
/
types.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (c) 2023, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package analysis
import (
"fmt"
"github.com/dburkart/fossil/pkg/common/parse"
"github.com/dburkart/fossil/pkg/database"
"github.com/dburkart/fossil/pkg/query/ast"
"github.com/dburkart/fossil/pkg/query/scanner"
"github.com/dburkart/fossil/pkg/query/types"
"github.com/dburkart/fossil/pkg/schema"
"strings"
)
type TypeChecker struct {
Errors []parse.SyntaxError
initialType schema.Object
symbols map[string]schema.Object
typeLookup map[ast.ASTNode]schema.Object
locations map[ast.ASTNode]parse.Location
nodes []ast.ASTNode
db *database.Database
}
func MakeTypeChecker(db *database.Database) *TypeChecker {
return &TypeChecker{
symbols: make(map[string]schema.Object),
typeLookup: make(map[ast.ASTNode]schema.Object),
locations: make(map[ast.ASTNode]parse.Location),
db: db,
}
}
// FIXME: Factor out stack into it's own thing
func (t *TypeChecker) push(node ast.ASTNode) {
t.nodes = append(t.nodes, node)
}
func (t *TypeChecker) pop() ast.ASTNode {
if len(t.nodes) == 0 {
return nil
}
node := t.nodes[len(t.nodes)-1]
t.nodes = t.nodes[:len(t.nodes)-1]
return node
}
func (t *TypeChecker) typeForNode(node ast.ASTNode) schema.Object {
nt, ok := t.typeLookup[node]
if !ok {
return schema.Unknown{}
}
return nt
}
func (t *TypeChecker) Visit(node ast.ASTNode) ast.Visitor {
if node == nil {
node = t.pop()
if node == nil {
return nil
}
switch n := node.(type) {
case *ast.NumberNode:
t.typeLookup[n] = &schema.Type{Name: "int64"}
t.locations[n] = n.Token.Location
case *ast.StringNode:
t.typeLookup[n] = &schema.Type{Name: "string"}
t.locations[n] = n.Token.Location
case *ast.IdentifierNode:
s, ok := t.symbols[n.Value()]
if !ok {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Token, fmt.Sprintf("Unable to infer type of identifier '%s'", n.Value())))
return nil
}
t.typeLookup[n] = s
t.locations[n] = n.Token.Location
case *ast.TupleElementNode:
var array *schema.Array
s, ok := t.symbols[n.Identifier.Value()]
if !ok {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Identifier.Token, fmt.Sprintf("Unable to infer type of identifier '%s'", n.Identifier.Value())))
return nil
}
if array, ok = s.(*schema.Array); !ok {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Identifier.Token, fmt.Sprintf("Type of '%s' is not a tuple, subscripting not allowed", n.Identifier.Value())))
return nil
}
if types.IntVal(n.Subscript.Val) > int64(array.Length-1) {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Subscript.Token, fmt.Sprintf("Tuple index out of bounds, '%s' has a schema of '%s'", n.Identifier.Value(), array.ToSchema())))
}
t.typeLookup[n] = &s.(*schema.Array).Type
t.locations[n] = n.Identifier.Token.Location
case *ast.TimeWhenceNode, *ast.TimespanNode:
t.typeLookup[n] = &schema.Type{Name: "int64"}
case *ast.BinaryOpNode:
if !t.typeForNode(n.Left).IsNumeric() || !t.typeForNode(n.Right).IsNumeric() {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Op, "Both operands must be numeric"))
return nil
}
switch n.Op.Type {
case scanner.TOK_MINUS, scanner.TOK_PLUS, scanner.TOK_STAR:
if strings.HasPrefix(t.typeForNode(n.Left).ToSchema(), "float") ||
strings.HasPrefix(t.typeForNode(n.Right).ToSchema(), "float") {
t.typeLookup[n] = &schema.Type{Name: "float64"}
} else {
t.typeLookup[n] = &schema.Type{Name: "int64"}
}
case scanner.TOK_SLASH:
t.typeLookup[n] = &schema.Type{Name: "float64"}
case scanner.TOK_LESS, scanner.TOK_LESS_EQ, scanner.TOK_EQ_EQ, scanner.TOK_NOT_EQ, scanner.TOK_GREATER, scanner.TOK_GREATER_EQ:
t.typeLookup[n] = &schema.Type{Name: "boolean"}
}
t.locations[n] = parse.Location{Start: t.locations[n.Left].Start, End: t.locations[n.Right].End}
case *ast.UnaryOpNode:
if !t.typeForNode(n.Operand).IsNumeric() {
err := fmt.Sprintf("Operator '%s' expects a numeric operand, got %s instead", n.Operator.Lexeme, t.typeForNode(n.Operand).ToSchema())
t.Errors = append(t.Errors, parse.NewSyntaxError(parse.Token{Location: t.locations[n.Operand]}, err))
}
// FIXME: This is not quite correct, we should be up-casting to int if operand is uint and the sign is -
t.typeLookup[n] = t.typeForNode(n.Operand)
t.locations[n] = parse.Location{Start: n.Operator.Location.Start, End: t.locations[n.Operand].End}
case *ast.TupleNode:
var innerType schema.Object
// Each item must have a compatible type
for _, item := range n.Elements {
if innerType == nil {
innerType = t.typeForNode(item)
continue
}
if (t.typeForNode(item).IsNumeric() && !innerType.IsNumeric()) ||
(!t.typeForNode(item).IsNumeric() && innerType.IsNumeric()) {
t.Errors = append(t.Errors, parse.NewSyntaxError(parse.Token{Location: t.locations[item]}, "Incompatible type found"))
}
if strings.HasPrefix(t.typeForNode(item).ToSchema(), "float") {
innerType = t.typeForNode(item)
}
// FIXME: Up-sample to largest numeric
}
t.typeLookup[n] = &schema.Array{Type: *innerType.(*schema.Type), Length: len(n.Elements)}
t.locations[n] = parse.Location{Start: t.locations[n.Elements[0]].Start, End: t.locations[n.Elements[len(n.Elements)-1]].End}
case *ast.DataFunctionNode:
t.typeLookup[n] = t.typeForNode(n.Expression)
// Reduce must have 2 arguments
if n.Name.Lexeme == "reduce" && len(n.Arguments) != 2 {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Name, fmt.Sprintf("The reduce function expects 2 arguments, %d provided", len(n.Arguments))))
}
// Populate symbols for the next stage in our pipeline
if n.Next != nil {
// Ensure we have the same number of return values as the next stage's
// arguments
nextNumArgs := len(n.Next.Arguments)
var argType schema.Object
// Filter operations don't mutate the input, and simply pass it along
if n.Name.Lexeme == "filter" {
argType = t.symbols[n.Arguments[0].Value()]
} else {
if array, ok := t.typeForNode(n.Expression).(schema.Array); ok {
if nextNumArgs == 1 {
argType = array
} else if nextNumArgs == array.Length {
argType = array.Type
} else {
txt := fmt.Sprintf("Argument mismatch: %s stage expected %d arguments, but got %d", n.Next.Value(), nextNumArgs, array.Length)
t.Errors = append(t.Errors, parse.NewSyntaxError(parse.Token{Location: t.locations[n.Expression]}, txt))
}
} else {
argType = t.typeForNode(n.Expression)
}
}
for _, arg := range n.Next.Arguments {
t.symbols[arg.Value()] = argType
}
}
case *ast.BuiltinFunctionNode:
builtin, ok := types.LookupBuiltinFunction(n.Name.Lexeme)
if !ok {
t.Errors = append(t.Errors, parse.NewSyntaxError(n.Name, fmt.Sprintf("Unknown builtin function: '%s'", n.Name.Lexeme)))
return nil
}
argType := t.typeForNode(n.Expression)
retType, err := builtin.Validate(argType)
if err != nil {
t.Errors = append(t.Errors, parse.NewSyntaxError(parse.Token{Location: t.locations[n.Expression]}, err.Error()))
return nil
}
t.typeLookup[n] = retType
}
return nil
}
switch n := node.(type) {
case *ast.QueryNode:
if n.DataPipeline != nil {
var s schema.Object
if n.Topic == nil {
s = &schema.Type{Name: "string"}
} else {
topic := n.Topic.(*ast.TopicSelectorNode).Topic
s = t.db.SchemaForTopic(topic.Lexeme)
if s == nil {
t.Errors = append(t.Errors, parse.NewSyntaxError(topic, "Unknown topic specified."))
return nil
}
}
t.initialType = s
return t
}
return nil
case *ast.DataPipelineNode:
first := n.Stages[0].(*ast.DataFunctionNode)
for _, arg := range first.Arguments {
t.symbols[arg.Value()] = t.initialType
}
return t
case *ast.NumberNode, *ast.StringNode, *ast.IdentifierNode, *ast.BinaryOpNode, *ast.UnaryOpNode, *ast.TupleNode,
*ast.DataFunctionNode, *ast.TupleElementNode, *ast.BuiltinFunctionNode, *ast.TimespanNode, *ast.TimeWhenceNode:
t.push(n)
return t
}
return nil
}