Skip to content

Commit

Permalink
Refactor ast nodes to embed private base struct
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 6, 2020
1 parent 75e0078 commit 93941b0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 60 deletions.
58 changes: 27 additions & 31 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,152 +15,148 @@ type Node interface {
SetType(reflect.Type)
}

type Base struct {
type base struct {
loc file.Location
nodeType reflect.Type
}

func (n *Base) Location() file.Location {
func (n *base) Location() file.Location {
return n.loc
}

func (n *Base) SetLocation(loc file.Location) {
func (n *base) SetLocation(loc file.Location) {
n.loc = loc
}

func (n *Base) Type() reflect.Type {
func (n *base) Type() reflect.Type {
return n.nodeType
}

func (n *Base) SetType(t reflect.Type) {
func (n *base) SetType(t reflect.Type) {
n.nodeType = t
}

func Loc(l file.Location) Base {
return Base{loc: l}
}

type NilNode struct {
Base
base
}

type IdentifierNode struct {
Base
base
Value string
}

type IntegerNode struct {
Base
base
Value int
}

type FloatNode struct {
Base
base
Value float64
}

type BoolNode struct {
Base
base
Value bool
}

type StringNode struct {
Base
base
Value string
}

type ConstantNode struct {
Base
base
Value interface{}
}

type UnaryNode struct {
Base
base
Operator string
Node Node
}

type BinaryNode struct {
Base
base
Operator string
Left Node
Right Node
}

type MatchesNode struct {
Base
base
Regexp *regexp.Regexp
Left Node
Right Node
}

type PropertyNode struct {
Base
base
Node Node
Property string
}

type IndexNode struct {
Base
base
Node Node
Index Node
}

type SliceNode struct {
Base
base
Node Node
From Node
To Node
}

type MethodNode struct {
Base
base
Node Node
Method string
Arguments []Node
}

type FunctionNode struct {
Base
base
Name string
Arguments []Node
Fast bool
}

type BuiltinNode struct {
Base
base
Name string
Arguments []Node
}

type ClosureNode struct {
Base
base
Node Node
}

type PointerNode struct {
Base
base
}

type ConditionalNode struct {
Base
base
Cond Node
Exp1 Node
Exp2 Node
}

type ArrayNode struct {
Base
base
Nodes []Node
}

type MapNode struct {
Base
base
Pairs []Node
}

type PairNode struct {
Base
base
Key Node
Value Node
}
9 changes: 8 additions & 1 deletion ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ast
import (
"fmt"
"reflect"
"regexp"
)

func Dump(node Node) string {
Expand All @@ -19,7 +20,7 @@ func dump(v reflect.Value, ident string) string {
out := t.Name() + "{\n"
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Name == "Base" {
if isPrivate(f.Name) {
continue
}
s := v.Field(i)
Expand Down Expand Up @@ -50,3 +51,9 @@ func dump(v reflect.Value, ident string) string {
return fmt.Sprintf("%v", v)
}
}

var isCapital = regexp.MustCompile("^[A-Z]")

func isPrivate(s string) bool {
return !isCapital.Match([]byte(s))
}
2 changes: 1 addition & 1 deletion cmd/exe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bufio"
"flag"
"fmt"
"github.com/antonmedv/expr/ast"
"io/ioutil"
"os"

"github.com/antonmedv/expr"
"github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/checker"
"github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/optimizer"
Expand Down
Loading

0 comments on commit 93941b0

Please sign in to comment.