-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_dict.go
66 lines (60 loc) · 1.66 KB
/
expr_dict.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
package rel
import (
"bytes"
"fmt"
"github.com/arr-ai/wbnf/parser"
)
// DictExpr represents an expression that yields a dict.
type DictExpr struct {
ExprScanner
entryExprs []DictEntryTupleExpr
allowDupKeys bool
}
// NewDictExpr returns a new DictExpr from pairs.
func NewDictExpr(
scanner parser.Scanner,
allowDupKeys bool, dictExprAlways bool, entryExprs ...DictEntryTupleExpr,
) Expr {
entries := make([]DictEntryTuple, 0, len(entryExprs))
for _, expr := range entryExprs {
if !dictExprAlways {
if at, is := exprIsValue(expr.at); is {
if value, is := exprIsValue(expr.value); is {
entries = append(entries, NewDictEntryTuple(at, value))
continue
}
}
}
return DictExpr{ExprScanner: ExprScanner{Src: scanner}, entryExprs: entryExprs, allowDupKeys: allowDupKeys}
}
return NewLiteralExpr(scanner, NewDict(allowDupKeys, entries...))
}
// String returns a string representation of the expression.
func (e DictExpr) String() string {
var b bytes.Buffer
b.WriteByte('{')
for i, expr := range e.entryExprs {
if i > 0 {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%v: %v", expr.at.String(), expr.value.String())
}
b.WriteByte('}')
return b.String()
}
// Eval returns the subject
func (e DictExpr) Eval(local Scope) (Value, error) {
entryExprs := make([]DictEntryTuple, 0, len(e.entryExprs))
for _, expr := range e.entryExprs {
at, err := expr.at.Eval(local)
if err != nil {
return nil, wrapContext(err, e, local)
}
value, err := expr.value.Eval(local)
if err != nil {
return nil, wrapContext(err, e, local)
}
entryExprs = append(entryExprs, NewDictEntryTuple(at, value))
}
return NewDict(e.allowDupKeys, entryExprs...), nil
}