Skip to content

Commit

Permalink
Merge branch 'master' into patch_phases
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv authored Jun 17, 2024
2 parents dd8014d + b24c7f3 commit a80cf48
Show file tree
Hide file tree
Showing 42 changed files with 1,913 additions and 1,222 deletions.
32 changes: 26 additions & 6 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package ast
import (
"reflect"

"github.com/expr-lang/expr/checker/nature"
"github.com/expr-lang/expr/file"
)

var (
anyType = reflect.TypeOf(new(any)).Elem()
)

// Node represents items of abstract syntax tree.
type Node interface {
Location() file.Location
SetLocation(file.Location)
Nature() nature.Nature
SetNature(nature.Nature)
Type() reflect.Type
SetType(reflect.Type)
String() string
Expand All @@ -25,8 +32,8 @@ func Patch(node *Node, newNode Node) {

// base is a base struct for all nodes.
type base struct {
loc file.Location
nodeType reflect.Type
loc file.Location
nature nature.Nature
}

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

// Nature returns the nature of the node.
func (n *base) Nature() nature.Nature {
return n.nature
}

// SetNature sets the nature of the node.
func (n *base) SetNature(nature nature.Nature) {
n.nature = nature
}

// Type returns the type of the node.
func (n *base) Type() reflect.Type {
return n.nodeType
if n.nature.Type == nil {
return anyType
}
return n.nature.Type
}

// SetType sets the type of the node.
func (n *base) SetType(t reflect.Type) {
n.nodeType = t
n.nature.Type = t
}

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

// ClosureNode represents a predicate.
// PredicateNode represents a predicate.
// Example:
//
// filter(foo, .bar == 1)
//
// The predicate is ".bar == 1".
type ClosureNode struct {
type PredicateNode struct {
base
Node Node // Node of the predicate body.
}
Expand Down
2 changes: 1 addition & 1 deletion ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (n *BuiltinNode) String() string {
return fmt.Sprintf("%s(%s)", n.Name, strings.Join(arguments, ", "))
}

func (n *ClosureNode) String() string {
func (n *PredicateNode) String() string {
return n.Node.String()
}

Expand Down
2 changes: 1 addition & 1 deletion ast/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Walk(node *Node, v Visitor) {
for i := range n.Arguments {
Walk(&n.Arguments[i], v)
}
case *ClosureNode:
case *PredicateNode:
Walk(&n.Node, v)
case *PointerNode:
case *VariableDeclaratorNode:
Expand Down
2 changes: 1 addition & 1 deletion builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestBuiltin_errors(t *testing.T) {
{`bitushr(-5, -2)`, "invalid operation: negative shift count -2 (type int) (1:1)"},
{`now(nil)`, "invalid number of arguments (expected 0, got 1)"},
{`date(nil)`, "interface {} is nil, not string (1:1)"},
{`timezone(nil)`, "interface {} is nil, not string (1:1)"},
{`timezone(nil)`, "cannot use nil as argument (type string) to call timezone (1:10)"},
}
for _, test := range errorTests {
t.Run(test.input, func(t *testing.T) {
Expand Down
Loading

0 comments on commit a80cf48

Please sign in to comment.