Skip to content

Commit

Permalink
Make type checking non-strict by default
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 6, 2020
1 parent 821f089 commit 75e0078
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 232 deletions.
14 changes: 6 additions & 8 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,25 @@ func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
v.types = config.Types
v.operators = config.Operators
v.expect = config.Expect
v.strict = !config.AllowUndefinedVariables
v.defaultType = config.UndefinedVariableType
v.strict = config.Strict
v.defaultType = config.DefaultType
}

t = v.visit(tree.Node)

if v.expect != reflect.Invalid {
switch v.expect {
case reflect.Int64, reflect.Float64:
if isNumber(t) {
goto okay
if !isNumber(t) {
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
}
default:
if t.Kind() == v.expect {
goto okay
if t.Kind() != v.expect {
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
}
}
return nil, fmt.Errorf("expected %v, but got %v", v.expect, t)
}

okay:
return
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/exe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/checker"
"github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/internal/conf"
"github.com/antonmedv/expr/optimizer"
"github.com/antonmedv/expr/parser"
"github.com/antonmedv/expr/vm"
Expand Down Expand Up @@ -88,9 +87,7 @@ func printAst() {
check(err)

if typeCheck {
_, err = checker.Check(tree, &conf.Config{
AllowUndefinedVariables: true,
})
_, err = checker.Check(tree, nil)
check(err)

if opt {
Expand Down
17 changes: 7 additions & 10 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func Env(i interface{}) Option {
c.MapEnv = true
} else {
if reflect.ValueOf(i).Kind() == reflect.Map {
c.UndefinedVariableType = reflect.TypeOf(i).Elem()
c.DefaultType = reflect.TypeOf(i).Elem()
}
}
c.CheckTypes = true
c.Strict = true
c.Types = conf.CreateTypesTable(i)
}
}
Expand All @@ -64,8 +64,7 @@ func Env(i interface{}) Option {
// runtime.fetch will panic as there is no way to get missing field zero value.
func AllowUndefinedVariables() Option {
return func(c *conf.Config) {
c.CheckTypes = true
c.AllowUndefinedVariables = true
c.Strict = false
}
}

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

if config.CheckTypes {
_, err = checker.Check(tree, config)
if err != nil {
return nil, err
}
checker.PatchOperators(tree, config)
_, err = checker.Check(tree, config)
if err != nil {
return nil, err
}
checker.PatchOperators(tree, config)

if config.Optimize {
optimizer.Optimize(&tree.Node)
Expand Down
Loading

0 comments on commit 75e0078

Please sign in to comment.