Skip to content

Add variables #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ type ConditionalNode struct {
Exp2 Node
}

type VariableDeclaratorNode struct {
base
Name string
Value Node
Expr Node
}

type ArrayNode struct {
base
Nodes []Node
Expand Down
4 changes: 4 additions & 0 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (n *PointerNode) String() string {
return "#"
}

func (n *VariableDeclaratorNode) String() string {
return fmt.Sprintf("let %s = %s; %s", n.Name, n.Value.String(), n.Expr.String())
}

func (n *ConditionalNode) String() string {
var cond, exp1, exp2 string
if _, ok := n.Cond.(*ConditionalNode); ok {
Expand Down
3 changes: 3 additions & 0 deletions ast/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func Walk(node *Node, v Visitor) {
case *ClosureNode:
Walk(&n.Node, v)
case *PointerNode:
case *VariableDeclaratorNode:
Walk(&n.Value, v)
Walk(&n.Expr, v)
case *ConditionalNode:
Walk(&n.Cond, v)
Walk(&n.Exp1, v)
Expand Down
46 changes: 41 additions & 5 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

type Function struct {
Name string
Func func(args ...interface{}) (interface{}, error)
Types []reflect.Type
Builtin1 func(arg interface{}) interface{}
Validate func(args []reflect.Type) (reflect.Type, error)
Name string
Func func(args ...interface{}) (interface{}, error)
Types []reflect.Type
Builtin1 func(arg interface{}) interface{}
Validate func(args []reflect.Type) (reflect.Type, error)
Predicate bool
}

var (
Expand All @@ -34,6 +35,41 @@ func init() {
}

var Builtins = []*Function{
{
Name: "all",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) bool)),
},
{
Name: "none",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) bool)),
},
{
Name: "any",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) bool)),
},
{
Name: "one",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) bool)),
},
{
Name: "filter",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) []interface{})),
},
{
Name: "map",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) interface{}) []interface{})),
},
{
Name: "count",
Predicate: true,
Types: types(new(func([]interface{}, func(interface{}) bool) int)),
},
{
Name: "len",
Builtin1: Len,
Expand Down
25 changes: 17 additions & 8 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func TestBuiltin_works_with_any(t *testing.T) {
}

for _, b := range builtin.Builtins {
if b.Predicate {
continue
}
t.Run(b.Name, func(t *testing.T) {
arity := 1
if c, ok := config[b.Name]; ok {
Expand Down Expand Up @@ -249,12 +252,15 @@ func TestBuiltin_disallow_builtins_override(t *testing.T) {

func TestBuiltin_DisableBuiltin(t *testing.T) {
t.Run("via env", func(t *testing.T) {
for _, name := range builtin.Names {
t.Run(name, func(t *testing.T) {
for _, b := range builtin.Builtins {
if b.Predicate {
continue // TODO: allow to disable predicates
}
t.Run(b.Name, func(t *testing.T) {
env := map[string]interface{}{
name: func() int { return 42 },
b.Name: func() int { return 42 },
}
program, err := expr.Compile(name+"()", expr.Env(env), expr.DisableBuiltin(name))
program, err := expr.Compile(b.Name+"()", expr.Env(env), expr.DisableBuiltin(b.Name))
require.NoError(t, err)

out, err := expr.Run(program, env)
Expand All @@ -264,15 +270,18 @@ func TestBuiltin_DisableBuiltin(t *testing.T) {
}
})
t.Run("via expr.Function", func(t *testing.T) {
for _, name := range builtin.Names {
t.Run(name, func(t *testing.T) {
fn := expr.Function(name,
for _, b := range builtin.Builtins {
if b.Predicate {
continue // TODO: allow to disable predicates
}
t.Run(b.Name, func(t *testing.T) {
fn := expr.Function(b.Name,
func(params ...interface{}) (interface{}, error) {
return 42, nil
},
new(func() int),
)
program, err := expr.Compile(name+"()", fn, expr.DisableBuiltin(name))
program, err := expr.Compile(b.Name+"()", fn, expr.DisableBuiltin(b.Name))
require.NoError(t, err)

out, err := expr.Run(program, nil)
Expand Down
41 changes: 41 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
type visitor struct {
config *conf.Config
collections []reflect.Type
scopes []scope
parents []ast.Node
err *file.Error
}

type scope struct {
name string
vtype reflect.Type
info info
}

type info struct {
method bool
fn *builtin.Function
Expand Down Expand Up @@ -104,6 +111,8 @@ func (v *visitor) visit(node ast.Node) (reflect.Type, info) {
t, i = v.ClosureNode(n)
case *ast.PointerNode:
t, i = v.PointerNode(n)
case *ast.VariableDeclaratorNode:
t, i = v.VariableDeclaratorNode(n)
case *ast.ConditionalNode:
t, i = v.ConditionalNode(n)
case *ast.ArrayNode:
Expand Down Expand Up @@ -135,6 +144,9 @@ func (v *visitor) NilNode(*ast.NilNode) (reflect.Type, info) {
}

func (v *visitor) IdentifierNode(node *ast.IdentifierNode) (reflect.Type, info) {
if s, ok := v.lookupVariable(node.Value); ok {
return s.vtype, s.info
}
if node.Value == "$env" {
return mapType, info{}
}
Expand Down Expand Up @@ -862,6 +874,35 @@ func (v *visitor) PointerNode(node *ast.PointerNode) (reflect.Type, info) {
return v.error(node, "cannot use %v as array", collection)
}

func (v *visitor) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) (reflect.Type, info) {
if _, ok := v.config.Types[node.Name]; ok {
return v.error(node, "cannot redeclare %v", node.Name)
}
if _, ok := v.config.Functions[node.Name]; ok {
return v.error(node, "cannot redeclare function %v", node.Name)
}
if _, ok := v.config.Builtins[node.Name]; ok {
return v.error(node, "cannot redeclare builtin %v", node.Name)
}
if _, ok := v.lookupVariable(node.Name); ok {
return v.error(node, "cannot redeclare variable %v", node.Name)
}
vtype, vinfo := v.visit(node.Value)
v.scopes = append(v.scopes, scope{node.Name, vtype, vinfo})
t, i := v.visit(node.Expr)
v.scopes = v.scopes[:len(v.scopes)-1]
return t, i
}

func (v *visitor) lookupVariable(name string) (scope, bool) {
for i := len(v.scopes) - 1; i >= 0; i-- {
if v.scopes[i].name == name {
return v.scopes[i], true
}
}
return scope{}, false
}

func (v *visitor) ConditionalNode(node *ast.ConditionalNode) (reflect.Type, info) {
c, _ := v.visit(node.Cond)
if !isBool(c) && !isAny(c) {
Expand Down
25 changes: 23 additions & 2 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ var successTests = []string{
"Any.A?.B == nil",
"(Any.Bool ?? Bool) > 0",
"Bool ?? Bool",
"let foo = 1; foo == 1",
}

func TestCheck(t *testing.T) {
Expand Down Expand Up @@ -506,6 +507,26 @@ repeat("0", 1/0)
cannot use float64 as argument (type int) to call repeat (1:14)
| repeat("0", 1/0)
| .............^

let map = 42; map
cannot redeclare builtin map (1:5)
| let map = 42; map
| ....^

let len = 42; len
cannot redeclare builtin len (1:5)
| let len = 42; len
| ....^

let Float = 42; Float
cannot redeclare Float (1:5)
| let Float = 42; Float
| ....^

let foo = 1; let foo = 2; foo
cannot redeclare variable foo (1:18)
| let foo = 1; let foo = 2; foo
| .................^
`

func TestCheck_error(t *testing.T) {
Expand Down Expand Up @@ -634,7 +655,7 @@ func TestCheck_AllowUndefinedVariables(t *testing.T) {
A int
}

tree, err := parser.Parse(`any + fn()`)
tree, err := parser.Parse(`Any + fn()`)
require.NoError(t, err)

config := conf.New(Env{})
Expand All @@ -647,7 +668,7 @@ func TestCheck_AllowUndefinedVariables(t *testing.T) {
func TestCheck_AllowUndefinedVariables_DefaultType(t *testing.T) {
env := map[string]bool{}

tree, err := parser.Parse(`any`)
tree, err := parser.Parse(`Any`)
require.NoError(t, err)

config := conf.New(env)
Expand Down
Loading