ExprContext.SetVar() no longer requires the explicit specification of the type of number
This commit is contained in:
		
							parent
							
								
									bd323efedf
								
							
						
					
					
						commit
						8f396a35de
					
				
							
								
								
									
										2
									
								
								ast.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								ast.go
									
									
									
									
									
								
							| @ -118,7 +118,7 @@ func (self *ast) eval(ctx ExprContext, preset bool) (result any, err error) { | ||||
| 		if self.forest != nil { | ||||
| 			for _, root := range self.forest { | ||||
| 				if result, err = root.compute(ctx); err == nil { | ||||
| 					ctx.SetVar(control_last_result, result) | ||||
| 					ctx.setVar(control_last_result, result) | ||||
| 				} else { | ||||
| 					//err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
 | ||||
| 					break | ||||
|  | ||||
| @ -33,6 +33,7 @@ type ExprContext interface { | ||||
| 	Clone() ExprContext | ||||
| 	GetVar(varName string) (value any, exists bool) | ||||
| 	SetVar(varName string, value any) | ||||
| 	setVar(varName string, value any) | ||||
| 	EnumVars(func(name string) (accept bool)) (varNames []string) | ||||
| 	EnumFuncs(func(name string) (accept bool)) (funcNames []string) | ||||
| 	GetFuncInfo(name string) ExprFunc | ||||
|  | ||||
							
								
								
									
										12
									
								
								control.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								control.go
									
									
									
									
									
								
							| @ -21,23 +21,23 @@ const ( | ||||
| ) | ||||
| 
 | ||||
| func initDefaultVars(ctx ExprContext) { | ||||
| 	ctx.SetVar(control_bool_shortcut, true) | ||||
| 	ctx.SetVar(control_import_path, init_import_path) | ||||
| 	ctx.setVar(control_bool_shortcut, true) | ||||
| 	ctx.setVar(control_import_path, init_import_path) | ||||
| } | ||||
| 
 | ||||
| func enable(ctx ExprContext, name string) { | ||||
| 	if strings.HasPrefix(name, "_") { | ||||
| 		ctx.SetVar(name, true) | ||||
| 		ctx.setVar(name, true) | ||||
| 	} else { | ||||
| 		ctx.SetVar("_"+name, true) | ||||
| 		ctx.setVar("_"+name, true) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func disable(ctx ExprContext, name string) { | ||||
| 	if strings.HasPrefix(name, "_") { | ||||
| 		ctx.SetVar(name, false) | ||||
| 		ctx.setVar(name, false) | ||||
| 	} else { | ||||
| 		ctx.SetVar("_"+name, false) | ||||
| 		ctx.setVar("_"+name, false) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -57,7 +57,7 @@ func exportVar(ctx ExprContext, name string, value any) { | ||||
| 	if name[0] == '@' { | ||||
| 		name = name[1:] | ||||
| 	} | ||||
| 	ctx.SetVar(name, value) | ||||
| 	ctx.setVar(name, value) | ||||
| } | ||||
| 
 | ||||
| func exportFunc(ctx ExprContext, name string, info ExprFunc) { | ||||
| @ -89,9 +89,9 @@ type funcDefFunctor struct { | ||||
| func (functor *funcDefFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) { | ||||
| 	for i, p := range functor.params { | ||||
| 		if i < len(args) { | ||||
| 			ctx.SetVar(p, args[i]) | ||||
| 			ctx.setVar(p, args[i]) | ||||
| 		} else { | ||||
| 			ctx.SetVar(p, nil) | ||||
| 			ctx.setVar(p, nil) | ||||
| 		} | ||||
| 	} | ||||
| 	result, err = functor.expr.eval(ctx, false) | ||||
|  | ||||
| @ -31,7 +31,7 @@ func evalAssign(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		if functor, ok := v.(Functor); ok { | ||||
| 			ctx.RegisterFunc(leftTerm.source(), functor, 0, -1) | ||||
| 		} else { | ||||
| 			ctx.SetVar(leftTerm.tk.source, v) | ||||
| 			ctx.setVar(leftTerm.tk.source, v) | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
|  | ||||
| @ -75,7 +75,7 @@ func evalAssignCoalesce(ctx ExprContext, self *term) (v any, err error) { | ||||
| 			err = errCoalesceNoFunc(self.children[1]) | ||||
| 		} else { | ||||
| 			v = rightValue | ||||
| 			ctx.SetVar(leftTerm.source(), rightValue) | ||||
| 			ctx.setVar(leftTerm.source(), rightValue) | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
|  | ||||
| @ -1,6 +1,8 @@ | ||||
| // simple-var-store.go
 | ||||
| package expr | ||||
| 
 | ||||
| import "fmt" | ||||
| 
 | ||||
| type SimpleVarStore struct { | ||||
| 	varStore map[string]any | ||||
| } | ||||
| @ -23,10 +25,18 @@ func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) { | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (ctx *SimpleVarStore) SetVar(varName string, value any) { | ||||
| func (ctx *SimpleVarStore) setVar(varName string, value any) { | ||||
| 	ctx.varStore[varName] = value | ||||
| } | ||||
| 
 | ||||
| func (ctx *SimpleVarStore) SetVar(varName string, value any) { | ||||
| 	if allowedValue, ok := fromGenericAny(value); ok { | ||||
| 		ctx.varStore[varName] = allowedValue | ||||
| 	} else { | ||||
| 		panic(fmt.Errorf("unsupported type %T of value %v", value, value)) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (ctx *SimpleVarStore) EnumVars(acceptor func(name string) (accept bool)) (varNames []string) { | ||||
| 	varNames = make([]string, 0) | ||||
| 	for name := range ctx.varStore { | ||||
|  | ||||
							
								
								
									
										18
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								utils.go
									
									
									
									
									
								
							| @ -73,6 +73,8 @@ func anyInteger(v any) (i int64, ok bool) { | ||||
| 		i = int64(intval) | ||||
| 	case uint16: | ||||
| 		i = int64(intval) | ||||
| 	case uint64: | ||||
| 		i = int64(intval) | ||||
| 	case uint32: | ||||
| 		i = int64(intval) | ||||
| 	case int8: | ||||
| @ -89,6 +91,22 @@ func anyInteger(v any) (i int64, ok bool) { | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func fromGenericAny(v any) (exprAny any, ok bool) { | ||||
| 	if exprAny, ok = v.(bool); ok { | ||||
| 		return | ||||
| 	} | ||||
| 	if exprAny, ok = v.(string); ok { | ||||
| 		return | ||||
| 	} | ||||
| 	if exprAny, ok = anyInteger(v); ok { | ||||
| 		return | ||||
| 	} | ||||
| 	if exprAny, ok = anyFloat(v); ok { | ||||
| 		return | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func anyFloat(v any) (float float64, ok bool) { | ||||
| 	ok = true | ||||
| 	switch floatval := v.(type) { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user