Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 1 | // Copyright 2018 The CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cue |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 19 | "strings" |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 20 | |
| 21 | "cuelang.org/go/cue/ast" |
| 22 | "cuelang.org/go/cue/build" |
Marcel van Lohuizen | 6ceb601 | 2019-02-18 18:30:38 +0100 | [diff] [blame] | 23 | "cuelang.org/go/cue/literal" |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 24 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | e9fd214 | 2019-04-12 15:46:31 +0200 | [diff] [blame] | 25 | "cuelang.org/go/internal" |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // insertFile inserts the given file at the root of the instance. |
| 29 | // |
| 30 | // The contents will be merged (unified) with any pre-existing value. In this |
| 31 | // case an error may be reported, but only if the merge failed at the top-level. |
| 32 | // Other errors will be recorded at the respective values in the tree. |
| 33 | // |
| 34 | // There should be no unresolved identifiers in file, meaning the Node field |
| 35 | // of all identifiers should be set to a non-nil value. |
| 36 | func (inst *Instance) insertFile(f *ast.File) error { |
| 37 | // TODO: insert by converting to value first so that the trim command can |
| 38 | // also remove top-level fields. |
| 39 | // First process single file. |
| 40 | v := newVisitor(inst.index, inst.inst, inst.rootStruct, inst.scope) |
| 41 | v.astState.astMap[f] = inst.rootStruct |
| 42 | result := v.walk(f) |
| 43 | if isBottom(result) { |
| 44 | return result.(*bottom) |
| 45 | } |
| 46 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 47 | return nil |
| 48 | } |
| 49 | |
| 50 | type astVisitor struct { |
| 51 | *astState |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 52 | object *structLit |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 53 | |
| 54 | inSelector int |
| 55 | } |
| 56 | |
| 57 | func (v *astVisitor) ctx() *context { |
| 58 | return v.astState.ctx |
| 59 | } |
| 60 | |
| 61 | type astState struct { |
| 62 | ctx *context |
| 63 | *index |
| 64 | inst *build.Instance |
| 65 | |
| 66 | litParser *litParser |
| 67 | resolveRoot *structLit |
| 68 | |
| 69 | // make unique per level to avoid reuse of structs being an issue. |
| 70 | astMap map[ast.Node]scope |
| 71 | } |
| 72 | |
| 73 | func (s *astState) mapScope(n ast.Node) (m scope) { |
| 74 | if m = s.astMap[n]; m == nil { |
| 75 | m = newStruct(newNode(n)) |
| 76 | s.astMap[n] = m |
| 77 | } |
| 78 | return m |
| 79 | } |
| 80 | |
| 81 | func (s *astState) setScope(n ast.Node, v scope) { |
| 82 | if m, ok := s.astMap[n]; ok && m != v { |
| 83 | panic("already defined") |
| 84 | } |
| 85 | s.astMap[n] = v |
| 86 | } |
| 87 | |
| 88 | func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor { |
| 89 | ctx := idx.newContext() |
Marcel van Lohuizen | d4847d9 | 2019-02-18 23:27:34 +0100 | [diff] [blame] | 90 | return newVisitorCtx(ctx, inst, obj, resolveRoot) |
| 91 | } |
| 92 | |
| 93 | func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor { |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 94 | v := &astVisitor{ |
| 95 | object: obj, |
| 96 | } |
| 97 | v.astState = &astState{ |
| 98 | ctx: ctx, |
| 99 | index: ctx.index, |
| 100 | inst: inst, |
| 101 | litParser: &litParser{ctx: ctx}, |
| 102 | resolveRoot: resolveRoot, |
| 103 | astMap: map[ast.Node]scope{}, |
| 104 | } |
| 105 | return v |
| 106 | } |
| 107 | |
| 108 | func (v *astVisitor) error(n ast.Node, args ...interface{}) value { |
| 109 | return v.mkErr(newNode(n), args...) |
| 110 | } |
| 111 | |
| 112 | func (v *astVisitor) resolve(n *ast.Ident) value { |
| 113 | ctx := v.ctx() |
| 114 | label := v.label(n.Name, true) |
| 115 | if r := v.resolveRoot; r != nil { |
Marcel van Lohuizen | 0e2bcd5 | 2019-04-03 22:33:14 +0200 | [diff] [blame] | 116 | for _, a := range r.arcs { |
| 117 | if a.feature == label { |
| 118 | return &selectorExpr{newExpr(n), |
| 119 | &nodeRef{baseValue: newExpr(n), node: r}, label} |
| 120 | } |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 121 | } |
| 122 | if v.inSelector > 0 { |
| 123 | if p := getBuiltinShorthandPkg(ctx, n.Name); p != nil { |
| 124 | return &nodeRef{baseValue: newExpr(n), node: p} |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | func (v *astVisitor) loadImport(imp *ast.ImportSpec) evaluated { |
| 132 | ctx := v.ctx() |
Marcel van Lohuizen | 6ceb601 | 2019-02-18 18:30:38 +0100 | [diff] [blame] | 133 | path, err := literal.Unquote(imp.Path.Value) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 134 | if err != nil { |
| 135 | return ctx.mkErr(newNode(imp), "illformed import spec") |
| 136 | } |
Marcel van Lohuizen | 0e2bcd5 | 2019-04-03 22:33:14 +0200 | [diff] [blame] | 137 | // TODO: allow builtin *and* imported package. The result is a unified |
| 138 | // struct. |
Marcel van Lohuizen | 8bc02e5 | 2019-04-01 13:14:07 +0200 | [diff] [blame] | 139 | if p := getBuiltinPkg(ctx, path); p != nil { |
| 140 | return p |
| 141 | } |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 142 | bimp := v.inst.LookupImport(path) |
| 143 | if bimp == nil { |
| 144 | return ctx.mkErr(newNode(imp), "package %q not found", path) |
| 145 | } |
| 146 | impInst := v.index.loadInstance(bimp) |
| 147 | return impInst.rootValue.evalPartial(ctx) |
| 148 | } |
| 149 | |
| 150 | // We probably don't need to call Walk.s |
| 151 | func (v *astVisitor) walk(astNode ast.Node) (value value) { |
| 152 | switch n := astNode.(type) { |
| 153 | case *ast.File: |
| 154 | obj := v.object |
| 155 | v1 := &astVisitor{ |
| 156 | astState: v.astState, |
| 157 | object: obj, |
| 158 | } |
| 159 | for _, e := range n.Decls { |
| 160 | switch x := e.(type) { |
| 161 | case *ast.EmitDecl: |
| 162 | if v1.object.emit == nil { |
| 163 | v1.object.emit = v1.walk(x.Expr) |
| 164 | } else { |
| 165 | v1.object.emit = mkBin(v.ctx(), token.NoPos, opUnify, v1.object.emit, v1.walk(x.Expr)) |
| 166 | } |
| 167 | default: |
| 168 | v1.walk(e) |
| 169 | } |
| 170 | } |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 171 | value = obj |
| 172 | |
| 173 | case *ast.ImportDecl: |
| 174 | for _, s := range n.Specs { |
| 175 | v.walk(s) |
| 176 | } |
| 177 | |
| 178 | case *ast.ImportSpec: |
| 179 | val := v.loadImport(n) |
| 180 | if !isBottom(val) { |
| 181 | v.setScope(n, val.(*structLit)) |
| 182 | } |
| 183 | |
| 184 | case *ast.StructLit: |
| 185 | obj := v.mapScope(n).(*structLit) |
| 186 | v1 := &astVisitor{ |
| 187 | astState: v.astState, |
| 188 | object: obj, |
| 189 | } |
| 190 | for _, e := range n.Elts { |
| 191 | switch x := e.(type) { |
| 192 | case *ast.EmitDecl: |
| 193 | // Only allowed at top-level. |
| 194 | v1.error(x, "emitting values is only allowed at top level") |
| 195 | case *ast.Field, *ast.Alias: |
| 196 | v1.walk(e) |
| 197 | case *ast.ComprehensionDecl: |
| 198 | v1.walk(x) |
| 199 | } |
| 200 | } |
| 201 | value = obj |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 202 | |
| 203 | case *ast.ComprehensionDecl: |
| 204 | yielder := &yield{baseValue: newExpr(n.Field.Value)} |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 205 | fc := &fieldComprehension{ |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 206 | baseValue: newDecl(n), |
| 207 | clauses: wrapClauses(v, yielder, n.Clauses), |
| 208 | } |
| 209 | field := n.Field |
| 210 | switch x := field.Label.(type) { |
| 211 | case *ast.Interpolation: |
| 212 | yielder.key = v.walk(x) |
| 213 | yielder.value = v.walk(field.Value) |
| 214 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 215 | case *ast.TemplateLabel: |
| 216 | f := v.label(x.Ident.Name, true) |
| 217 | |
| 218 | sig := ¶ms{} |
| 219 | sig.add(f, &basicType{newNode(field.Label), stringKind}) |
| 220 | template := &lambdaExpr{newExpr(field.Value), sig, nil} |
| 221 | |
| 222 | v.setScope(field, template) |
| 223 | template.value = v.walk(field.Value) |
| 224 | yielder.value = template |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 225 | fc.isTemplate = true |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 226 | |
| 227 | case *ast.BasicLit, *ast.Ident: |
| 228 | name, ok := ast.LabelName(x) |
| 229 | if !ok { |
| 230 | return v.error(x, "invalid field name: %v", x) |
| 231 | } |
Marcel van Lohuizen | f23fbff | 2018-12-20 12:23:16 +0100 | [diff] [blame] | 232 | |
| 233 | // TODO: if the clauses do not contain a guard, we know that this |
| 234 | // field will always be added and we can move the comprehension one |
| 235 | // level down. This, in turn, has the advantage that it is more |
| 236 | // likely that the cross-reference limitation for field |
| 237 | // comprehensions is not violated. To ensure compatibility between |
| 238 | // implementations, though, we should relax the spec as well. |
| 239 | // The cross-reference rule is simple and this relaxation seems a |
| 240 | // bit more complex. |
| 241 | |
| 242 | // TODO: for now we can also consider making this an error if |
| 243 | // the list of clauses does not contain if and make a suggestion |
| 244 | // to rewrite it. |
| 245 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 246 | if name != "" { |
| 247 | yielder.key = &stringLit{newNode(x), name} |
| 248 | yielder.value = v.walk(field.Value) |
| 249 | } |
| 250 | |
| 251 | default: |
| 252 | panic("cue: unknown label type") |
| 253 | } |
| 254 | // yielder.key = v.walk(n.Field.Label) |
| 255 | // yielder.value = v.walk(n.Field.Value) |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 256 | v.object.comprehensions = append(v.object.comprehensions, fc) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 257 | |
| 258 | case *ast.Field: |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 259 | opt := n.Optional != token.NoPos |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 260 | switch x := n.Label.(type) { |
| 261 | case *ast.Interpolation: |
| 262 | yielder := &yield{baseValue: newNode(x)} |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 263 | fc := &fieldComprehension{ |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 264 | baseValue: newDecl(n), |
| 265 | clauses: yielder, |
| 266 | } |
| 267 | yielder.key = v.walk(x) |
| 268 | yielder.value = v.walk(n.Value) |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 269 | yielder.opt = opt |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 270 | v.object.comprehensions = append(v.object.comprehensions, fc) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 271 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 272 | case *ast.TemplateLabel: |
| 273 | f := v.label(x.Ident.Name, true) |
| 274 | |
| 275 | sig := ¶ms{} |
| 276 | sig.add(f, &basicType{newNode(n.Label), stringKind}) |
| 277 | template := &lambdaExpr{newExpr(n.Value), sig, nil} |
| 278 | |
| 279 | v.setScope(n, template) |
| 280 | template.value = v.walk(n.Value) |
| 281 | |
| 282 | if v.object.template == nil { |
| 283 | v.object.template = template |
| 284 | } else { |
| 285 | v.object.template = mkBin(v.ctx(), token.NoPos, opUnify, v.object.template, template) |
| 286 | } |
| 287 | |
| 288 | case *ast.BasicLit, *ast.Ident: |
Marcel van Lohuizen | e9fd214 | 2019-04-12 15:46:31 +0200 | [diff] [blame] | 289 | if internal.DropOptional && opt { |
| 290 | break |
| 291 | } |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 292 | attrs, err := createAttrs(v.ctx(), newNode(n), n.Attrs) |
| 293 | if err != nil { |
| 294 | return err |
| 295 | } |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 296 | f, ok := v.nodeLabel(x) |
| 297 | if !ok { |
| 298 | return v.error(n.Label, "invalid field name: %v", n.Label) |
| 299 | } |
| 300 | if f != 0 { |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 301 | v.object.insertValue(v.ctx(), f, opt, v.walk(n.Value), attrs) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | default: |
| 305 | panic("cue: unknown label type") |
| 306 | } |
| 307 | |
| 308 | case *ast.Alias: |
| 309 | // parsed verbatim at reference. |
| 310 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 311 | case *ast.ListComprehension: |
| 312 | yielder := &yield{baseValue: newExpr(n.Expr)} |
| 313 | lc := &listComprehension{ |
| 314 | newExpr(n), |
| 315 | wrapClauses(v, yielder, n.Clauses), |
| 316 | } |
| 317 | // we don't support key for lists (yet?) |
| 318 | yielder.value = v.walk(n.Expr) |
| 319 | return lc |
| 320 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 321 | // Expressions |
| 322 | case *ast.Ident: |
| 323 | if n.Node == nil { |
| 324 | if value = v.resolve(n); value != nil { |
| 325 | break |
| 326 | } |
| 327 | |
| 328 | switch n.Name { |
| 329 | case "_": |
| 330 | return &top{newExpr(n)} |
| 331 | case "string": |
| 332 | return &basicType{newExpr(n), stringKind} |
| 333 | case "bytes": |
| 334 | return &basicType{newExpr(n), bytesKind} |
| 335 | case "bool": |
| 336 | return &basicType{newExpr(n), boolKind} |
| 337 | case "int": |
| 338 | return &basicType{newExpr(n), intKind} |
| 339 | case "float": |
| 340 | return &basicType{newExpr(n), floatKind} |
| 341 | case "number": |
| 342 | return &basicType{newExpr(n), numKind} |
| 343 | case "duration": |
| 344 | return &basicType{newExpr(n), durationKind} |
| 345 | |
| 346 | case "len": |
| 347 | return lenBuiltin |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 348 | case "and": |
| 349 | return andBuiltin |
| 350 | case "or": |
| 351 | return orBuiltin |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 352 | } |
| 353 | if r, ok := predefinedRanges[n.Name]; ok { |
| 354 | return r |
| 355 | } |
| 356 | |
| 357 | value = v.error(n, "reference %q not found", n.Name) |
| 358 | break |
| 359 | } |
| 360 | |
| 361 | if a, ok := n.Node.(*ast.Alias); ok { |
| 362 | value = v.walk(a.Expr) |
| 363 | break |
| 364 | } |
| 365 | |
| 366 | label := v.label(n.Name, true) |
| 367 | if n.Scope != nil { |
| 368 | n2 := v.mapScope(n.Scope) |
| 369 | value = &nodeRef{baseValue: newExpr(n), node: n2} |
| 370 | value = &selectorExpr{newExpr(n), value, label} |
| 371 | } else { |
| 372 | n2 := v.mapScope(n.Node) |
| 373 | value = &nodeRef{baseValue: newExpr(n), node: n2} |
| 374 | } |
| 375 | |
| 376 | case *ast.BottomLit: |
| 377 | value = v.error(n, "from source") |
| 378 | |
| 379 | case *ast.BadDecl: |
| 380 | // nothing to do |
| 381 | |
| 382 | case *ast.BadExpr: |
| 383 | value = v.error(n, "invalid expression") |
| 384 | |
| 385 | case *ast.BasicLit: |
| 386 | value = v.litParser.parse(n) |
| 387 | |
| 388 | case *ast.Interpolation: |
| 389 | if len(n.Elts) == 0 { |
| 390 | return v.error(n, "invalid interpolation") |
| 391 | } |
| 392 | first, ok1 := n.Elts[0].(*ast.BasicLit) |
| 393 | last, ok2 := n.Elts[len(n.Elts)-1].(*ast.BasicLit) |
| 394 | if !ok1 || !ok2 { |
| 395 | return v.error(n, "invalid interpolation") |
| 396 | } |
| 397 | if len(n.Elts) == 1 { |
| 398 | value = v.walk(n.Elts[0]) |
| 399 | break |
| 400 | } |
| 401 | lit := &interpolation{baseValue: newExpr(n), k: stringKind} |
| 402 | value = lit |
Marcel van Lohuizen | 6ceb601 | 2019-02-18 18:30:38 +0100 | [diff] [blame] | 403 | info, prefixLen, _, err := literal.ParseQuotes(first.Value, last.Value) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 404 | if err != nil { |
| 405 | return v.error(n, "invalid interpolation: %v", err) |
| 406 | } |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 407 | prefix := "" |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 408 | for i := 0; i < len(n.Elts); i += 2 { |
| 409 | l, ok := n.Elts[i].(*ast.BasicLit) |
| 410 | if !ok { |
| 411 | return v.error(n, "invalid interpolation") |
| 412 | } |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 413 | s := l.Value |
| 414 | if !strings.HasPrefix(s, prefix) { |
| 415 | return v.error(l, "invalid interpolation: unmatched ')'") |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 416 | } |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 417 | s = l.Value[prefixLen:] |
| 418 | x := parseString(v.ctx(), l, info, s) |
| 419 | lit.parts = append(lit.parts, x) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 420 | if i+1 < len(n.Elts) { |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 421 | lit.parts = append(lit.parts, v.walk(n.Elts[i+1])) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 422 | } |
| 423 | prefix = ")" |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 424 | prefixLen = 1 |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | case *ast.ListLit: |
Marcel van Lohuizen | 9ee652d | 2019-04-25 17:16:01 +0200 | [diff] [blame] | 428 | arcs := []arc{} |
| 429 | for i, e := range n.Elts { |
| 430 | arcs = append(arcs, arc{feature: label(i), v: v.walk(e)}) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 431 | } |
Marcel van Lohuizen | 9ee652d | 2019-04-25 17:16:01 +0200 | [diff] [blame] | 432 | s := &structLit{baseValue: newExpr(n), arcs: arcs} |
| 433 | list := &list{baseValue: newExpr(n), elem: s} |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 434 | list.initLit() |
| 435 | if n.Ellipsis != token.NoPos || n.Type != nil { |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 436 | list.len = newBound(list.baseValue, opGeq, intKind, list.len) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 437 | if n.Type != nil { |
| 438 | list.typ = v.walk(n.Type) |
| 439 | } |
| 440 | } |
| 441 | value = list |
| 442 | |
| 443 | case *ast.ParenExpr: |
| 444 | value = v.walk(n.X) |
| 445 | |
| 446 | case *ast.SelectorExpr: |
| 447 | v.inSelector++ |
| 448 | value = &selectorExpr{ |
| 449 | newExpr(n), |
| 450 | v.walk(n.X), |
| 451 | v.label(n.Sel.Name, true), |
| 452 | } |
| 453 | v.inSelector-- |
| 454 | |
| 455 | case *ast.IndexExpr: |
| 456 | value = &indexExpr{newExpr(n), v.walk(n.X), v.walk(n.Index)} |
| 457 | |
| 458 | case *ast.SliceExpr: |
| 459 | slice := &sliceExpr{baseValue: newExpr(n), x: v.walk(n.X)} |
| 460 | if n.Low != nil { |
| 461 | slice.lo = v.walk(n.Low) |
| 462 | } |
| 463 | if n.High != nil { |
| 464 | slice.hi = v.walk(n.High) |
| 465 | } |
| 466 | value = slice |
| 467 | |
| 468 | case *ast.CallExpr: |
| 469 | call := &callExpr{baseValue: newExpr(n), x: v.walk(n.Fun)} |
| 470 | for _, a := range n.Args { |
| 471 | call.args = append(call.args, v.walk(a)) |
| 472 | } |
| 473 | value = call |
| 474 | |
| 475 | case *ast.UnaryExpr: |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 476 | switch n.Op { |
| 477 | case token.NOT, token.ADD, token.SUB: |
| 478 | value = &unaryExpr{ |
| 479 | newExpr(n), |
| 480 | tokenMap[n.Op], |
| 481 | v.walk(n.X), |
| 482 | } |
Marcel van Lohuizen | 706e69c | 2019-02-11 18:21:14 +0100 | [diff] [blame] | 483 | case token.GEQ, token.GTR, token.LSS, token.LEQ, |
| 484 | token.NEQ, token.MAT, token.NMAT: |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 485 | value = newBound( |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 486 | newExpr(n), |
| 487 | tokenMap[n.Op], |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 488 | topKind|nonGround, |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 489 | v.walk(n.X), |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 490 | ) |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 491 | |
| 492 | case token.MUL: |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 493 | return v.error(n, "preference mark not allowed at this position") |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 494 | default: |
| 495 | return v.error(n, "unsupported unary operator %q", n.Op) |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | case *ast.BinaryExpr: |
| 499 | switch n.Op { |
Marcel van Lohuizen | bedcf0c | 2019-02-22 18:00:00 +0100 | [diff] [blame] | 500 | case token.OR: |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 501 | d := &disjunction{baseValue: newExpr(n)} |
| 502 | v.addDisjunctionElem(d, n.X, false) |
| 503 | v.addDisjunctionElem(d, n.Y, false) |
| 504 | value = d |
Marcel van Lohuizen | 7d0797b | 2019-02-07 18:35:28 +0100 | [diff] [blame] | 505 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 506 | default: |
| 507 | value = &binaryExpr{ |
| 508 | newExpr(n), |
| 509 | tokenMap[n.Op], // op |
| 510 | v.walk(n.X), // left |
| 511 | v.walk(n.Y), // right |
| 512 | } |
| 513 | } |
| 514 | |
Marcel van Lohuizen | 5b49d76 | 2019-05-13 10:59:45 +0200 | [diff] [blame^] | 515 | case *ast.CommentGroup: |
| 516 | // Nothing to do for a free-floating comment group. |
| 517 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 518 | // nothing to do |
| 519 | // case *syntax.EmitDecl: |
| 520 | default: |
| 521 | // TODO: unhandled node. |
| 522 | // value = ctx.mkErr(n, "unknown node type %T", n) |
| 523 | panic(fmt.Sprintf("unimplemented %T", n)) |
| 524 | |
| 525 | } |
| 526 | return value |
| 527 | } |
| 528 | |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 529 | func (v *astVisitor) addDisjunctionElem(d *disjunction, n ast.Node, mark bool) { |
| 530 | switch x := n.(type) { |
| 531 | case *ast.BinaryExpr: |
Marcel van Lohuizen | bedcf0c | 2019-02-22 18:00:00 +0100 | [diff] [blame] | 532 | if x.Op == token.OR { |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 533 | v.addDisjunctionElem(d, x.X, mark) |
| 534 | v.addDisjunctionElem(d, x.Y, mark) |
| 535 | return |
| 536 | } |
| 537 | case *ast.UnaryExpr: |
| 538 | if x.Op == token.MUL { |
| 539 | mark = true |
| 540 | n = x.X |
| 541 | } |
Marcel van Lohuizen | 94d845d | 2019-05-10 00:28:03 +0200 | [diff] [blame] | 542 | d.hasDefaults = true |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 543 | } |
| 544 | d.values = append(d.values, dValue{v.walk(n), mark}) |
| 545 | } |
| 546 | |
Marcel van Lohuizen | 17157ea | 2018-12-11 10:41:10 +0100 | [diff] [blame] | 547 | func wrapClauses(v *astVisitor, y yielder, clauses []ast.Clause) yielder { |
| 548 | for _, c := range clauses { |
| 549 | if n, ok := c.(*ast.ForClause); ok { |
| 550 | params := ¶ms{} |
| 551 | fn := &lambdaExpr{newExpr(n.Source), params, nil} |
| 552 | v.setScope(n, fn) |
| 553 | } |
| 554 | } |
| 555 | for i := len(clauses) - 1; i >= 0; i-- { |
| 556 | switch n := clauses[i].(type) { |
| 557 | case *ast.ForClause: |
| 558 | fn := v.mapScope(n).(*lambdaExpr) |
| 559 | fn.value = y |
| 560 | |
| 561 | key := "_" |
| 562 | if n.Key != nil { |
| 563 | key = n.Key.Name |
| 564 | } |
| 565 | f := v.label(key, true) |
| 566 | fn.add(f, &basicType{newExpr(n.Key), stringKind | intKind}) |
| 567 | |
| 568 | f = v.label(n.Value.Name, true) |
| 569 | fn.add(f, &top{}) |
| 570 | |
| 571 | y = &feed{newExpr(n.Source), v.walk(n.Source), fn} |
| 572 | |
| 573 | case *ast.IfClause: |
| 574 | y = &guard{newExpr(n.Condition), v.walk(n.Condition), y} |
| 575 | } |
| 576 | } |
| 577 | return y |
| 578 | } |