Skip to content

env[] keyword implemented in parser #382

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 23 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,18 @@ func (v *visitor) ChainNode(node *ast.ChainNode) (reflect.Type, info) {
}

func (v *visitor) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
base, _ := v.visit(node.Node)
prop, _ := v.visit(node.Property)
if an, ok := node.Node.(*ast.IdentifierNode); ok && an.Value == "env" {
// If the index is a constant string, can save some
// cycles later by finding the type of its referent
if name, ok := node.Property.(*ast.StringNode); ok {
if t, ok := v.config.Types[name.Value]; ok {
return t.Type, info{method: t.Method}
} // No error if no type found; it may be added to env between compile and run
}
return anyType, info{}
}
base, _ := v.visit(node.Node)

if name, ok := node.Property.(*ast.StringNode); ok {
if base == nil {
Expand Down
4 changes: 4 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func (c *compiler) NilNode(_ *ast.NilNode) {
}

func (c *compiler) IdentifierNode(node *ast.IdentifierNode) {
if node.Value == "env" {
c.emit(OpLoadEnv)
return
}
if c.mapEnv {
c.emit(OpLoadFast, c.addConstant(node.Value))
} else if len(node.FieldIndex) > 0 {
Expand Down
14 changes: 10 additions & 4 deletions conf/types_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func CreateTypesTable(i interface{}) TypesTable {
for _, key := range v.MapKeys() {
value := v.MapIndex(key)
if key.Kind() == reflect.String && value.IsValid() && value.CanInterface() {
if key.String() == "env" { // Could check for all keywords here
panic("attempt to misuse env keyword as env map key")
}
types[key.String()] = Tag{Type: reflect.TypeOf(value.Interface())}
}
}
Expand Down Expand Up @@ -94,10 +97,13 @@ func FieldsFromStruct(t reflect.Type) TypesTable {
}
}
}

types[FieldName(f)] = Tag{
Type: f.Type,
FieldIndex: f.Index,
if fn := FieldName(f); fn == "env" { // Could check for all keywords here
panic("attempt to misuse env keyword as env struct field tag")
} else {
types[FieldName(f)] = Tag{
Type: f.Type,
FieldIndex: f.Index,
}
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,103 @@ 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"
}(),
"1": "o",
"2": "k",
"num": 10,
"mylist": []int{1, 2, 3, 4, 5},
"MIN": func(a, b int) int {
if a < b {
return a
} else {
return b
}
},
"red": "n",
"irect": "um",
"String Map": map[string]string{
"one": "two",
"three": "four",
},
"OtherMap": map[string]string{
"a": "b",
"c": "d",
},
}

// 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['String Map'].one", "two"},
{"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['String Map']?.five", ""},
{"env.red", "n"},
{"env?.blue", nil},
{"env.mylist[1]", 2},
{"env?.OtherMap?.a", "b"},
{"env?.OtherMap?.d", ""},
}

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()", "bad"},
}

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 @@ -11,6 +11,7 @@ const (
OpLoadFast
OpLoadMethod
OpLoadFunc
OpLoadEnv
OpFetch
OpFetchField
OpMethod
Expand Down
2 changes: 2 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ 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 OpLoadEnv:
vm.push(env)
case OpMethod:
a := vm.pop()
vm.push(runtime.FetchMethod(a, program.Constants[arg].(*runtime.Method)))
Expand Down