-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_tuple.go
152 lines (139 loc) · 3.7 KB
/
expr_tuple.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
package rel
import (
"bytes"
"github.com/arr-ai/wbnf/parser"
"github.com/go-errors/errors"
)
// AttrExpr represents a single name:expr in a TupleExpr.
type AttrExpr struct {
ExprScanner
name string
expr Expr
}
// NewAttrExpr constructs a new AttrExpr from the given arguments.
func NewAttrExpr(scanner parser.Scanner, name string, expr Expr) (AttrExpr, error) {
isWildcard := false
if dot, ok := expr.(*DotExpr); ok {
if dot.Attr() == "*" {
isWildcard = true
expr = dot.Subject()
}
}
if isWildcard != (name == "*") {
return AttrExpr{}, errors.Errorf("Wildcard attr cannot have a name")
}
return AttrExpr{ExprScanner{scanner}, name, expr}, nil
}
func MustNewAttrExpr(scanner parser.Scanner, name string, expr Expr) AttrExpr {
attrExpr, err := NewAttrExpr(scanner, name, expr)
if err != nil {
panic(err)
}
return attrExpr
}
// NewWildcardExpr constructs a new wildcard AttrExpr.
func NewWildcardExpr(scanner parser.Scanner, lhs Expr) AttrExpr {
return AttrExpr{ExprScanner{scanner}, "*", lhs}
}
// IsWildcard returns true iff the AttrExpr is a wildcard expression.
func (e *AttrExpr) IsWildcard() bool {
return e.name == "*"
}
// Apply applies the AttrExpr to the Tuple.
func (e *AttrExpr) Apply(
local Scope, tuple Tuple,
) (Tuple, error) {
value, err := e.expr.Eval(local)
if err != nil {
return nil, err
}
if e.IsWildcard() {
if t, ok := value.(Tuple); ok {
for e := t.Enumerator(); e.MoveNext(); {
tuple = tuple.With(e.Current())
}
return tuple, nil
}
return nil, errors.Errorf(
"LHS of wildcard must be tuple, not %T", value)
}
tuple = tuple.With(e.name, value)
return tuple, nil
}
// TupleExpr returns a set from a slice of Exprs.
type TupleExpr struct {
ExprScanner
attrs []AttrExpr
attrMap map[string]Expr
}
// NewTupleExpr returns a new TupleExpr.
func NewTupleExpr(scanner parser.Scanner, attrs ...AttrExpr) Expr {
attrValues := make([]Attr, len(attrs))
for i, attr := range attrs {
if value, is := exprIsValue(attr.expr); is {
attrValues[i] = Attr{attr.name, value}
} else {
attrMap := make(map[string]Expr, len(attrs))
for _, attr := range attrs {
attrMap[attr.name] = attr.expr
}
return &TupleExpr{ExprScanner{scanner}, attrs, attrMap}
}
}
return NewLiteralExpr(scanner, NewTuple(attrValues...))
}
// NewTupleExprFromMap returns a new TupleExpr from a map[string]Expr.
func NewTupleExprFromMap(scanner parser.Scanner, attrMap map[string]Expr) Expr {
attrValues := make([]Attr, len(attrMap))
i := 0
for name, expr := range attrMap {
if value, ok := expr.(Value); ok {
attrValues[i] = Attr{name, value}
i++
} else {
attrs := make([]AttrExpr, len(attrMap))
i := 0
for name, expr := range attrMap {
attrs[i] = AttrExpr{ExprScanner{scanner}, name, expr}
i++
}
return &TupleExpr{ExprScanner{scanner}, attrs, attrMap}
}
}
return NewTuple(attrValues...)
}
// String returns a string representation of the expression.
func (e *TupleExpr) String() string { //nolint:dupl
var b bytes.Buffer
b.WriteByte('(')
for i, attr := range e.attrs {
if i > 0 {
b.WriteString(", ")
}
if attr.IsWildcard() {
if ident, is := attr.expr.(IdentExpr); !is || ident.Ident() != "." {
b.WriteString(attr.expr.String())
}
b.WriteString(".*")
} else {
b.WriteString(attr.name)
b.WriteString(": ")
b.WriteString(attr.expr.String())
}
}
b.WriteByte(')')
return b.String()
}
// Eval returns the subject
func (e *TupleExpr) Eval(local Scope) (Value, error) {
tuple := EmptyTuple
var err error
for _, attr := range e.attrs {
tuple, err = attr.Apply(local, tuple)
if err != nil {
return nil, WrapContext(err, e, local)
}
}
// TODO: Construct new tuple directly
return tuple.(*GenericTuple).Canonical(), nil
}