Skip to content

Commit a80cf48

Browse files
authored
Merge branch 'master' into patch_phases
2 parents dd8014d + b24c7f3 commit a80cf48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1913
-1222
lines changed

ast/node.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ package ast
33
import (
44
"reflect"
55

6+
"github.com/expr-lang/expr/checker/nature"
67
"github.com/expr-lang/expr/file"
78
)
89

10+
var (
11+
anyType = reflect.TypeOf(new(any)).Elem()
12+
)
13+
914
// Node represents items of abstract syntax tree.
1015
type Node interface {
1116
Location() file.Location
1217
SetLocation(file.Location)
18+
Nature() nature.Nature
19+
SetNature(nature.Nature)
1320
Type() reflect.Type
1421
SetType(reflect.Type)
1522
String() string
@@ -25,8 +32,8 @@ func Patch(node *Node, newNode Node) {
2532

2633
// base is a base struct for all nodes.
2734
type base struct {
28-
loc file.Location
29-
nodeType reflect.Type
35+
loc file.Location
36+
nature nature.Nature
3037
}
3138

3239
// Location returns the location of the node in the source code.
@@ -39,14 +46,27 @@ func (n *base) SetLocation(loc file.Location) {
3946
n.loc = loc
4047
}
4148

49+
// Nature returns the nature of the node.
50+
func (n *base) Nature() nature.Nature {
51+
return n.nature
52+
}
53+
54+
// SetNature sets the nature of the node.
55+
func (n *base) SetNature(nature nature.Nature) {
56+
n.nature = nature
57+
}
58+
4259
// Type returns the type of the node.
4360
func (n *base) Type() reflect.Type {
44-
return n.nodeType
61+
if n.nature.Type == nil {
62+
return anyType
63+
}
64+
return n.nature.Type
4565
}
4666

4767
// SetType sets the type of the node.
4868
func (n *base) SetType(t reflect.Type) {
49-
n.nodeType = t
69+
n.nature.Type = t
5070
}
5171

5272
// NilNode represents nil.
@@ -163,13 +183,13 @@ type BuiltinNode struct {
163183
Map Node // Used by optimizer to fold filter() and map() builtins.
164184
}
165185

166-
// ClosureNode represents a predicate.
186+
// PredicateNode represents a predicate.
167187
// Example:
168188
//
169189
// filter(foo, .bar == 1)
170190
//
171191
// The predicate is ".bar == 1".
172-
type ClosureNode struct {
192+
type PredicateNode struct {
173193
base
174194
Node Node // Node of the predicate body.
175195
}

ast/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (n *BuiltinNode) String() string {
162162
return fmt.Sprintf("%s(%s)", n.Name, strings.Join(arguments, ", "))
163163
}
164164

165-
func (n *ClosureNode) String() string {
165+
func (n *PredicateNode) String() string {
166166
return n.Node.String()
167167
}
168168

ast/visitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Walk(node *Node, v Visitor) {
4545
for i := range n.Arguments {
4646
Walk(&n.Arguments[i], v)
4747
}
48-
case *ClosureNode:
48+
case *PredicateNode:
4949
Walk(&n.Node, v)
5050
case *PointerNode:
5151
case *VariableDeclaratorNode:

builtin/builtin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func TestBuiltin_errors(t *testing.T) {
235235
{`bitushr(-5, -2)`, "invalid operation: negative shift count -2 (type int) (1:1)"},
236236
{`now(nil)`, "invalid number of arguments (expected 0, got 1)"},
237237
{`date(nil)`, "interface {} is nil, not string (1:1)"},
238-
{`timezone(nil)`, "interface {} is nil, not string (1:1)"},
238+
{`timezone(nil)`, "cannot use nil as argument (type string) to call timezone (1:10)"},
239239
}
240240
for _, test := range errorTests {
241241
t.Run(test.input, func(t *testing.T) {

0 commit comments

Comments
 (0)