@@ -3,13 +3,21 @@ package ast
|
3 | 3 | import (
|
4 | 4 | "reflect"
|
5 | 5 |
|
| 6 | +".com/expr-lang/expr/checker/nature" |
6 | 7 | ".com/expr-lang/expr/file"
|
7 | 8 | )
|
8 | 9 |
|
| 10 | +var ( |
| 11 | +anyType = reflect.TypeOf(new(any)).Elem() |
| 12 | +) |
| 13 | + |
9 | 14 | // Node represents items of abstract syntax tree.
|
10 | 15 | type Node interface {
|
11 | 16 | Location() file.Location
|
12 | 17 | SetLocation(file.Location)
|
| 18 | +Nature() nature.Nature |
| 19 | +SetNature(nature.Nature) |
| 20 | +Kind() reflect.Kind |
13 | 21 | Type() reflect.Type
|
14 | 22 | SetType(reflect.Type)
|
15 | 23 | String() string
|
@@ -25,8 +33,8 @@ func (node *Node, newNode Node) {
|
25 | 33 |
|
26 | 34 | // base is a base struct for all nodes.
|
27 | 35 | type base struct {
|
28 |
| -loc file.Location |
29 |
| -nodeType reflect.Type |
| 36 | +loc file.Location |
| 37 | +nature nature.Nature |
30 | 38 | }
|
31 | 39 |
|
32 | 40 | // Location returns the location of the node in the source code.
|
@@ -39,14 +47,36 @@ func (n *base) SetLocation(loc file.Location) {
|
39 | 47 | n.loc = loc
|
40 | 48 | }
|
41 | 49 |
|
| 50 | +// Nature returns the nature of the node. |
| 51 | +func (n *base) Nature() nature.Nature { |
| 52 | +return n.nature |
| 53 | +} |
| 54 | + |
| 55 | +// SetNature sets the nature of the node. |
| 56 | +func (n *base) SetNature(nature nature.Nature) { |
| 57 | +n.nature = nature |
| 58 | +} |
| 59 | + |
| 60 | +// Kind returns the kind of the node. |
| 61 | +// If the type is nil (meaning unknown) then it returns reflect.Interface. |
| 62 | +func (n *base) Kind() reflect.Kind { |
| 63 | +if n.nature.Type == nil { |
| 64 | +return reflect.Interface |
| 65 | +} |
| 66 | +return n.nature.Type.Kind() |
| 67 | +} |
| 68 | + |
42 | 69 | // Type returns the type of the node.
|
43 | 70 | func (n *base) Type() reflect.Type {
|
44 |
| -return n.nodeType |
| 71 | +if n.nature.Type == nil { |
| 72 | +return anyType |
| 73 | +} |
| 74 | +return n.nature.Type |
45 | 75 | }
|
46 | 76 |
|
47 | 77 | // SetType sets the type of the node.
|
48 | 78 | func (n *base) SetType(t reflect.Type) {
|
49 |
| -n.nodeType = t |
| 79 | +n.nature.Type = t |
50 | 80 | }
|
51 | 81 |
|
52 | 82 | // NilNode represents nil.
|
|
0 commit comments