@@ -9,20 +9,29 @@ import (
99)
1010
1111type Config struct {
12- Env interface {}
13- MapEnv bool
14- Types TypesTable
15- Operators OperatorsTable
16- Expect reflect.Kind
17- Optimize bool
18- Strict bool
19- DefaultType reflect.Type
20- ConstExprFns map [string ]reflect.Value
21- Visitors []ast.Visitor
22- err error
12+ Env interface {}
13+ Types TypesTable
14+ MapEnv bool
15+ DefaultType reflect.Type
16+ Operators OperatorsTable
17+ Expect reflect.Kind
18+ Optimize bool
19+ Strict bool
20+ ConstFns map [string ]reflect.Value
21+ Visitors []ast.Visitor
2322}
2423
2524func New (env interface {}) * Config {
25+ c := & Config {
26+ Operators : make (map [string ][]string ),
27+ ConstFns : make (map [string ]reflect.Value ),
28+ Optimize : true ,
29+ }
30+ c .WithEnv (env )
31+ return c
32+ }
33+
34+ func (c * Config ) WithEnv (env interface {}) {
2635 var mapEnv bool
2736 var mapValueType reflect.Type
2837 if _ , ok := env .(map [string ]interface {}); ok {
@@ -33,58 +42,37 @@ func New(env interface{}) *Config {
3342 }
3443 }
3544
36- return & Config {
37- Env : env ,
38- MapEnv : mapEnv ,
39- Types : CreateTypesTable (env ),
40- Operators : make (map [string ][]string ),
41- Optimize : true ,
42- Strict : true ,
43- DefaultType : mapValueType ,
44- ConstExprFns : make (map [string ]reflect.Value ),
45- }
45+ c .Env = env
46+ c .Types = CreateTypesTable (env )
47+ c .MapEnv = mapEnv
48+ c .DefaultType = mapValueType
49+ c .Strict = true
4650}
4751
48- // Check validates the compiler configuration.
49- func (c * Config ) Check () error {
50- // Check that all functions that define operator overloading
51- // exist in environment and have correct signatures.
52- for op , fns := range c .Operators {
53- for _ , fn := range fns {
54- fnType , ok := c .Types [fn ]
55- if ! ok || fnType .Type .Kind () != reflect .Func {
56- return fmt .Errorf ("function %s for %s operator does not exist in environment" , fn , op )
57- }
58- requiredNumIn := 2
59- if fnType .Method {
60- requiredNumIn = 3 // As first argument of method is receiver.
61- }
62- if fnType .Type .NumIn () != requiredNumIn || fnType .Type .NumOut () != 1 {
63- return fmt .Errorf ("function %s for %s operator does not have a correct signature" , fn , op )
64- }
52+ func (c * Config ) Operator (operator string , fns ... string ) {
53+ c .Operators [operator ] = append (c .Operators [operator ], fns ... )
54+ for _ , fn := range fns {
55+ fnType , ok := c .Types [fn ]
56+ if ! ok || fnType .Type .Kind () != reflect .Func {
57+ panic (fmt .Errorf ("function %s for %s operator does not exist in the environment" , fn , operator ))
6558 }
66- }
67-
68- // Check that all ConstExprFns are functions .
69- for name , fn := range c . ConstExprFns {
70- if fn . Kind () != reflect . Func {
71- return fmt .Errorf ("const expression %q must be a function " , name )
59+ requiredNumIn := 2
60+ if fnType . Method {
61+ requiredNumIn = 3 // As first argument of method is receiver .
62+ }
63+ if fnType . Type . NumIn () != requiredNumIn || fnType . Type . NumOut () != 1 {
64+ panic ( fmt .Errorf ("function %s for %s operator does not have a correct signature " , fn , operator ) )
7265 }
7366 }
74-
75- return c .err
7667}
7768
7869func (c * Config ) ConstExpr (name string ) {
7970 if c .Env == nil {
80- c .Error (fmt .Errorf ("no environment for const expression: %v" , name ))
81- return
71+ panic ("no environment is specified for ConstExpr()" )
8272 }
83- c .ConstExprFns [name ] = reflect .ValueOf (runtime .Fetch (c .Env , name ))
84- }
85-
86- func (c * Config ) Error (err error ) {
87- if c .err == nil {
88- c .err = err
73+ fn := reflect .ValueOf (runtime .Fetch (c .Env , name ))
74+ if fn .Kind () != reflect .Func {
75+ panic (fmt .Errorf ("const expression %q must be a function" , name ))
8976 }
77+ c .ConstFns [name ] = fn
9078}
0 commit comments