Skip to content

Commit 75e0078

Browse files
committed
Make type checking non-strict by default
1 parent 821f089 commit 75e0078

File tree

7 files changed

+97
-232
lines changed

7 files changed

+97
-232
lines changed

checker/checker.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@ func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
2828
v.types = config.Types
2929
v.operators = config.Operators
3030
v.expect = config.Expect
31-
v.strict = !config.AllowUndefinedVariables
32-
v.defaultType = config.UndefinedVariableType
31+
v.strict = config.Strict
32+
v.defaultType = config.DefaultType
3333
}
3434

3535
t = v.visit(tree.Node)
3636

3737
if v.expect != reflect.Invalid {
3838
switch v.expect {
3939
case reflect.Int64, reflect.Float64:
40-
if isNumber(t) {
41-
goto okay
40+
if !isNumber(t) {
41+
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
4242
}
4343
default:
44-
if t.Kind() == v.expect {
45-
goto okay
44+
if t.Kind() != v.expect {
45+
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
4646
}
4747
}
48-
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
4948
}
5049

51-
okay:
5250
return
5351
}
5452

cmd/exe/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/antonmedv/expr/ast"
1212
"github.com/antonmedv/expr/checker"
1313
"github.com/antonmedv/expr/compiler"
14-
"github.com/antonmedv/expr/internal/conf"
1514
"github.com/antonmedv/expr/optimizer"
1615
"github.com/antonmedv/expr/parser"
1716
"github.com/antonmedv/expr/vm"
@@ -88,9 +87,7 @@ func printAst() {
8887
check(err)
8988

9089
if typeCheck {
91-
_, err = checker.Check(tree, &conf.Config{
92-
AllowUndefinedVariables: true,
93-
})
90+
_, err = checker.Check(tree, nil)
9491
check(err)
9592

9693
if opt {

expr.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func Env(i interface{}) Option {
5050
c.MapEnv = true
5151
} else {
5252
if reflect.ValueOf(i).Kind() == reflect.Map {
53-
c.UndefinedVariableType = reflect.TypeOf(i).Elem()
53+
c.DefaultType = reflect.TypeOf(i).Elem()
5454
}
5555
}
56-
c.CheckTypes = true
56+
c.Strict = true
5757
c.Types = conf.CreateTypesTable(i)
5858
}
5959
}
@@ -64,8 +64,7 @@ func Env(i interface{}) Option {
6464
// runtime.fetch will panic as there is no way to get missing field zero value.
6565
func AllowUndefinedVariables() Option {
6666
return func(c *conf.Config) {
67-
c.CheckTypes = true
68-
c.AllowUndefinedVariables = true
67+
c.Strict = false
6968
}
7069
}
7170

@@ -124,13 +123,11 @@ func Compile(input string, ops ...Option) (*vm.Program, error) {
124123
return nil, err
125124
}
126125

127-
if config.CheckTypes {
128-
_, err = checker.Check(tree, config)
129-
if err != nil {
130-
return nil, err
131-
}
132-
checker.PatchOperators(tree, config)
126+
_, err = checker.Check(tree, config)
127+
if err != nil {
128+
return nil, err
133129
}
130+
checker.PatchOperators(tree, config)
134131

135132
if config.Optimize {
136133
optimizer.Optimize(&tree.Node)

0 commit comments

Comments
 (0)