Skip to content
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

env[] keyword implemented in parser #382

Merged
merged 23 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
486e0da
Implemented env[] keyword and tests.
mdmcconnell May 19, 2023
39225da
env keyword must take string (no unquoted literals)
mdmcconnell May 19, 2023
2cb92eb
Fixed error failing to ignore closing bracket
mdmcconnell May 22, 2023
14abd99
Removed env keyword from lexer.
mdmcconnell Jun 10, 2023
268eb41
Implemented env[] keyowrd for abitrary identfiers
mdmcconnell Jun 10, 2023
03616c7
Allow for non-integer index with env keyword
mdmcconnell Jun 27, 2023
65d64c5
Allow for non-integer index with env keyword
mdmcconnell Jun 27, 2023
a5c8d90
Removed env implementation (previous commit)
mdmcconnell Jun 27, 2023
9e3c044
Added OpFetchEnv
mdmcconnell Jun 27, 2023
bb22e0e
Implemented OpFetchEnv
mdmcconnell Jun 27, 2023
887f0b7
Improved error message for non-string env index
mdmcconnell Jun 27, 2023
f5ddd6a
Tests env when index must be evaluated
mdmcconnell Jul 9, 2023
fc28fb2
Deal with env[] keywod member node.
mdmcconnell Jul 9, 2023
9b3857f
Allow for non-integer index with env keyword
mdmcconnell Jul 9, 2023
9a2d59f
Typo.
mdmcconnell Jul 9, 2023
3e8608f
Check when env index is StringNode, handle type
mdmcconnell Jul 11, 2023
e5303ca
Implement env keyword in IdentifierNode with OpLoadEnv
mdmcconnell Jul 11, 2023
f45da29
Removed unused code
mdmcconnell Jul 11, 2023
e4130f3
Add OpLoadEnv, remove OpFetchEnv
mdmcconnell Jul 11, 2023
1cdb746
Add OpLoadEnv, remove OpFetchEnv
mdmcconnell Jul 11, 2023
b1fe261
Check for incorrect use of env keyword
mdmcconnell Jul 11, 2023
84d3631
Remove obsolete tests
mdmcconnell Jul 11, 2023
fee4852
Added tests for env. and env?.
mdmcconnell Jul 11, 2023
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
9 changes: 7 additions & 2 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (v *visitor) IdentifierNode(node *ast.IdentifierNode) (reflect.Type, info)
if fn, ok := v.config.Functions[node.Value]; ok {
// Return anyType instead of func type as we don't know the arguments yet.
// The func type can be one of the fn.Types. The type will be resolved
// when the arguments are known in CallNode.
// when the arguments areknown in CallNode.
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
return anyType, info{fn: fn}
}
if v.config.Types == nil {
Expand Down Expand Up @@ -383,6 +383,9 @@ func (v *visitor) ChainNode(node *ast.ChainNode) (reflect.Type, info) {
}

func (v *visitor) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
if an, ok := node.Node.(*ast.IdentifierNode); ok && an.Value == "env" {
return anyType, info{}
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
}
base, _ := v.visit(node.Node)
prop, _ := v.visit(node.Property)

Expand Down Expand Up @@ -429,7 +432,9 @@ func (v *visitor) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
return t, info{}

case reflect.Array, reflect.Slice:
if !isInteger(prop) && !isAny(prop) {
if isInteger(prop) || isAny(prop) {
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
// ok, we normally expect an integer or interface that could be one
} else {
return v.error(node.Property, "array elements can only be selected using an integer (got %v)", prop)
}
t, c := deref(base.Elem())
Expand Down
28 changes: 17 additions & 11 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,19 +491,25 @@ func (c *compiler) MemberNode(node *ast.MemberNode) {
}
}

c.compile(base)
if node.Optional {
ph := c.emit(OpJumpIfNil, placeholder)
c.chains[len(c.chains)-1] = append(c.chains[len(c.chains)-1], ph)
}

if op == OpFetch {
// Implementation of direct env access
if ident, ok := base.(*ast.IdentifierNode); ok && ident.Value == "env" {
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
c.compile(node.Property)
c.emit(OpFetch)
c.emit(OpFetchEnv)
} else {
c.emitLocation(node.Location(), op, c.addConstant(
&runtime.Field{Index: index, Path: path},
))
c.compile(base)
if node.Optional {
ph := c.emit(OpJumpIfNil, placeholder)
c.chains[len(c.chains)-1] = append(c.chains[len(c.chains)-1], ph)
}

if op == OpFetch {
c.compile(node.Property)
c.emit(OpFetch)
} else {
c.emitLocation(node.Location(), op, c.addConstant(
&runtime.Field{Index: index, Path: path},
))
}
}

deref:
Expand Down
91 changes: 91 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,97 @@ func TestEval_nil_in_maps(t *testing.T) {
})
}

// Test the use of env keyword. Forms env[] and env[”] are valid.
// The enclosed identifier must be in the expression env.
func TestEnv_keyword(t *testing.T) {
env := map[string]interface{}{
"space test": "ok",
"space_test": "not ok", // Seems to be some underscore substituting happening, check that.
"Section 1-2a": "ok",
`c:\ndrive\2015 Information Table`: "ok",
"%*worst function name ever!!": func() string {
return "ok"
}(),
"env": func(string) string {
return "num"
},
"1": "o",
"2": "k",
"num": 10,
"my list": []int{1, 2, 3, 4, 5},
"MIN": func(a, b int) int {
if a < b {
return a
} else {
return b
}
},
"red": "n",
"irect": "um",
}

// No error cases
var tests = []struct {
code string
want interface{}
}{
{"env['space test']", "ok"},
{"env['Section 1-2a']", "ok"},
{`env["c:\\ndrive\\2015 Information Table"]`, "ok"},
{"env['%*worst function name ever!!']", "ok"},
{"env('Confusing!')", "num"}, // not keyword, but some function named env
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
{"env['1'] + env['2']", "ok"},
{"1 + env['num'] + env['num']", 21},
{"MIN(env['num'],0)", 0},
{"env['nu' + 'm']", 10},
{"env[red + irect]", 10},
{"env[env('na')]", 10},
}

for _, tt := range tests {
t.Run(tt.code, func(t *testing.T) {

program, err := expr.Compile(tt.code, expr.Env(env))
require.NoError(t, err, "compile error")

got, err := expr.Run(program, env)
require.NoError(t, err, "execution error")

assert.Equal(t, tt.want, got, tt.code)
})
}

for _, tt := range tests {
t.Run(tt.code, func(t *testing.T) {
got, err := expr.Eval(tt.code, env)
require.NoError(t, err, "eval error: "+tt.code)

assert.Equal(t, tt.want, got, "eval: "+tt.code)
})
}

// error cases
tests = []struct {
code string
want interface{}
}{
{"env[this is a problem", "bad"},
{"env['also a problem'", "bad"},
{"env[space test]", "bad"},
{"env[1] + env[2]", "bad"},
mdmcconnell marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tt := range tests {
t.Run(tt.code, func(t *testing.T) {

_, err := expr.Eval(tt.code, expr.Env(env))
require.Error(t, err, "compile error")

})
}

}

type Bar interface {
Bar() int
}
Expand Down
1 change: 1 addition & 0 deletions vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
OpLoadFunc
OpFetch
OpFetchField
OpFetchEnv
OpMethod
OpTrue
OpFalse
Expand Down
7 changes: 7 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)
a := vm.pop()
vm.push(runtime.FetchField(a, program.Constants[arg].(*runtime.Field)))

case OpFetchEnv:
b := vm.pop()
if _, ok := b.(string); !ok {
panic("index for env[] keyord must be string")
}
vm.push(runtime.Fetch(env, b))

case OpMethod:
a := vm.pop()
vm.push(runtime.FetchMethod(a, program.Constants[arg].(*runtime.Method)))
Expand Down