// Eval evaluates expr against a map.
769610
func Eval(expr Expr, m map[string]interface{}) interface{} {
	eval := ValuerEval{Valuer: MapValuer(m)}
	return eval.Eval(expr)
}
870498
func (v *ValuerEval) Eval(expr Expr) interface{} {
	if expr == nil {
		return nil
	}

	switch expr := expr.(type) {
	case *BinaryExpr:
		return v.evalBinaryExpr(expr)
	case *BooleanLiteral:
		return expr.Val
	case *IntegerLiteral:
		return expr.Val
	case *NumberLiteral:
		return expr.Val
	case *UnsignedLiteral:
		return expr.Val
	case *ParenExpr:
		return v.Eval(expr.Expr)
	case *RegexLiteral:
		return expr.Val
	case *StringLiteral:
		return expr.Val
	case *Call:
		if valuer, ok := v.Valuer.(CallValuer); ok {
			var args []interface{}
			if len(expr.Args) > 0 {
				args = make([]interface{}, len(expr.Args))
				for i := range expr.Args {
					args[i] = v.Eval(expr.Args[i])
				}
			}
			val, _ := valuer.Call(expr.Name, args)
			return val
		}
		return nil
	case *VarRef:
		val, _ := v.Valuer.Value(expr.Val)
		return val
	default:
		return nil
	}
}
507857
func EvalInput(input interface{}) EvalOption {
	return func(e *EvalContext) {
		e.rawInput = &input
		e.hasInput = true
	}
}
321128
func (b *builtinSqrtSig) evalReal(row chunk.Row) (float64, bool, error) {
	val, isNull, err := b.args[0].EvalReal(b.ctx, row)
	if isNull || err != nil {
		return 0, isNull, err
	}
	if val < 0 {
		return 0, true, nil
	}
	return math.Sqrt(val), false, nil
}
1160082
func (options *Options) Eval(ctx interface{}, field string) interface{} {
	if ctx == nil {
		return nil
	}

	if field == "" {
		return nil
	}

	val := options.eval.evalField(reflect.ValueOf(ctx), field, false)
	if !val.IsValid() {
		return nil
	}

	return val.Interface()
}
226580
func Eval(expr string, context interface{}) (val interface{}, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Eval paniked. %s", r)
		}
	}()

	exp, err := parser.ParseExpr(expr)

	if err != nil {
		return nilInterf, err
	}

	ctxt := createContext(context)

	val, err = eval(exp, ctxt)
	return val, err
}
867357
func EvalInt(expr string, context interface{}) (int, error) {
	val, err := Eval(expr, context)
	if err != nil {
		return 0, err
	}
	return castInt(val)
}
1350433
func (remote *RemoteDebugger) EvaluateWrap(expr string) (interface{}, error) {
	expr = fmt.Sprintf("(function(){%v})()", expr)
	return remote.Evaluate(expr)
}
71159
func evalBool(exprs []expression.Expression, row []types.Datum, ctx *stmtctx.StatementContext) (bool, error) {
	for _, expr := range exprs {
		data, err := expr.Eval(chunk.MutRowFromDatums(row).ToRow())
		if err != nil {
			return false, errors.Trace(err)
		}
		if data.IsNull() {
			return false, nil
		}

		isBool, err := data.ToBool(ctx)
		if err != nil {
			return false, errors.Trace(err)
		}
		if isBool == 0 {
			return false, nil
		}
	}
	return true, nil
}
883724
func eval(expr ast.Node, context Context) (interface{}, error) {
	//fmt.Println(reflect.TypeOf(expr), time.Now().UnixNano()/int64(10000), expr)
	switch expr.(type) {
	/*
		case *ast.ArrayType:
			return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		case *ast.AssignStmt:
			return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		case *ast.BadDecl:
			return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		case *ast.BadExpr:
			return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		case *ast.BadStmt:
			return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
	*/
	case *constNodeBinaryExpr:
		return expr.(*constNodeBinaryExpr).value, nil
	case *constNodeUnaryExpr:
		return expr.(*constNodeUnaryExpr).value, nil
	case *constNodeBasicLit:
		return expr.(*constNodeBasicLit).value, nil
	case *constNodeIdent:
		return expr.(*constNodeIdent).value, nil
	case *constNodeParenExpr:
		return expr.(*constNodeParenExpr).value, nil
	case *ast.BasicLit:
		return evalBasicLit(expr.(*ast.BasicLit), context)
	case *ast.BinaryExpr:
		return evalBinaryExpr(expr.(*ast.BinaryExpr), context)
		/*
			case *ast.BlockStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.BranchStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.CallExpr:
		return evalCallExpr(expr.(*ast.CallExpr), context)
		/*
			case *ast.CaseClause:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.ChanType:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.CommClause:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.Comment:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.CommentGroup:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.CompositeLit:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.DeclStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.DeferStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.Ellipsis:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.EmptyStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.Field:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.FieldList:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.ForStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.FuncDecl:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.FuncLit:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.FuncType:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.GenDecl:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.GoStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.Ident:
		return evalIdent(expr.(*ast.Ident), context)
		/*
			case *ast.IfStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.ImportSpec:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.IncDecStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.IndexExpr:
		return evalIndexExpr(expr.(*ast.IndexExpr), context)
		/*
			case *ast.InterfaceType:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.KeyValueExpr:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.LabeledStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.MapType:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.Package:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.ParenExpr:
		return evalParenExpr(expr.(*ast.ParenExpr), context)
		/*
			case *ast.RangeStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.ReturnStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.SelectStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.SelectorExpr:
		return evalSelectorExpr(expr.(*ast.SelectorExpr), context)
		/*
			case *ast.SendStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.SliceExpr:
		return evalSliceExpr(expr.(*ast.SliceExpr), context)
	case *ast.StarExpr:
		return evalStarExpr(expr.(*ast.StarExpr), context)
		/*
			case *ast.StructType:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.SwitchStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.TypeAssertExpr:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.TypeSpec:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
			case *ast.TypeSwitchStmt:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	case *ast.UnaryExpr:
		return evalUnaryExpr(expr.(*ast.UnaryExpr), context)
		/*
			case *ast.ValueSpec:
				return nil, fmt.Errorf("%s not suported", reflect.TypeOf(expr))
		*/
	default:
		return nilInterf, fmt.Errorf("Default %s not suported", reflect.TypeOf(expr))
	}
}
377476
func (e *Expr) Eval(getter VarGetter) (Value, error) {
	if e.root == nil {
		return Zero(), nil
	}
	v, err := eval(e, getter, e.root)
	if err != nil {
		return Zero(), err
	}
	return v, nil
}
259368
func (s *state) evalExpr(exp parse.Expr) (v Value, e error) {
	switch exp := exp.(type) {
	case *parse.NullExpr:
		return nil, nil
	case *parse.BoolExpr:
		return exp.Value, nil
	case *parse.NameExpr:
		if val, ok := s.scope.Get(exp.Name); ok {
			v = val
		} else {
			e = errors.New("undefined variable \"" + exp.Name + "\"")
		}
	case *parse.NumberExpr:
		num, err := strconv.ParseFloat(exp.Value, 64)
		if err != nil {
			return nil, err
		}
		return num, nil
	case *parse.StringExpr:
		return exp.Text, nil
	case *parse.GroupExpr:
		return s.evalExpr(exp.X)
	case *parse.UnaryExpr:
		in, err := s.evalExpr(exp.X)
		if err != nil {
			return nil, err
		}
		switch exp.Op {
		case parse.OpUnaryNot:
			return !CoerceBool(in), nil
		case parse.OpUnaryPositive:
			// no-op, +1 = 1, +(-1) = -1, +(false) = 0
			return CoerceNumber(in), nil
		case parse.OpUnaryNegative:
			return -CoerceNumber(in), nil
		}
	case *parse.BinaryExpr:
		left, err := s.evalExpr(exp.Left)
		if err != nil {
			return nil, err
		}
		right, err := s.evalExpr(exp.Right)
		if err != nil {
			return nil, err
		}
		switch exp.Op {
		case parse.OpBinaryAdd:
			return CoerceNumber(left) + CoerceNumber(right), nil
		case parse.OpBinarySubtract:
			return CoerceNumber(left) - CoerceNumber(right), nil
		case parse.OpBinaryMultiply:
			return CoerceNumber(left) * CoerceNumber(right), nil
		case parse.OpBinaryDivide:
			return CoerceNumber(left) / CoerceNumber(right), nil
		case parse.OpBinaryFloorDiv:
			return math.Floor(CoerceNumber(left) / CoerceNumber(right)), nil
		case parse.OpBinaryModulo:
			return float64(int(CoerceNumber(left)) % int(CoerceNumber(right))), nil
		case parse.OpBinaryPower:
			return math.Pow(CoerceNumber(left), CoerceNumber(right)), nil
		case parse.OpBinaryConcat:
			return CoerceString(left) + CoerceString(right), nil
		case parse.OpBinaryEndsWith:
			return strings.HasSuffix(CoerceString(left), CoerceString(right)), nil
		case parse.OpBinaryStartsWith:
			return strings.HasPrefix(CoerceString(left), CoerceString(right)), nil
		case parse.OpBinaryIn:
			return Contains(right, left)
		case parse.OpBinaryNotIn:
			res, err := Contains(right, left)
			if err != nil {
				return false, err
			}
			return !res, nil
		case parse.OpBinaryIs:
			if fn, ok := right.(func(v Value) bool); ok {
				return fn(left), nil
			}
			return nil, errors.New("right operand was of unexpected type")
		case parse.OpBinaryIsNot:
			if fn, ok := right.(func(v Value) bool); ok {
				return !fn(left), nil
			}
			return nil, errors.New("right operand was of unexpected type")
		case parse.OpBinaryMatches:
			reg, err := regexp.Compile(CoerceString(right))
			if err != nil {
				return nil, err
			}
			return reg.MatchString(CoerceString(left)), nil
		case parse.OpBinaryEqual:
			return Equal(left, right), nil
		case parse.OpBinaryNotEqual:
			return !Equal(left, right), nil
		case parse.OpBinaryGreaterEqual:
			return CoerceNumber(left) >= CoerceNumber(right), nil
		case parse.OpBinaryGreaterThan:
			return CoerceNumber(left) > CoerceNumber(right), nil
		case parse.OpBinaryLessEqual:
			return CoerceNumber(left) <= CoerceNumber(right), nil
		case parse.OpBinaryLessThan:
			return CoerceNumber(left) < CoerceNumber(right), nil
		case parse.OpBinaryRange:
			l, r := CoerceNumber(left), CoerceNumber(right)
			res := make([]float64, uint(math.Ceil(r-l))+1)
			for i, k := 0, l; k <= r; i, k = i+1, k+1 {
				res[i] = k
			}
			return res, nil
		case parse.OpBinaryBitwiseAnd:
			return int(CoerceNumber(left)) & int(CoerceNumber(right)), nil
		case parse.OpBinaryBitwiseOr:
			return int(CoerceNumber(left)) | int(CoerceNumber(right)), nil
		case parse.OpBinaryBitwiseXor:
			return int(CoerceNumber(left)) ^ int(CoerceNumber(right)), nil
		case parse.OpBinaryAnd:
			return CoerceBool(left) && CoerceBool(right), nil
		case parse.OpBinaryOr:
			return CoerceBool(left) && CoerceBool(right), nil
		}
	case *parse.FuncExpr:
		return s.evalFunction(exp)
	case *parse.FilterExpr:
		return s.evalFilter(exp)
	case *parse.GetAttrExpr:
		c, err := s.evalExpr(exp.Cont)
		if err != nil {
			return nil, err
		}
		k, err := s.evalExpr(exp.Attr)
		if err != nil {
			return nil, err
		}
		exargs := exp.Args
		args := make([]Value, len(exargs))
		for k, e := range exargs {
			v, err := s.evalExpr(e)
			if err != nil {
				return nil, err
			}
			args[k] = v
		}
		if set, ok := c.(macroSet); ok {
			if macro, ok := set.defs[CoerceString(k)]; ok {
				return s.callMacro(macro, args...)
			}
			return nil, errors.New("undefined macro: " + CoerceString(k))
		}
		v, err = GetAttr(c, k, args...)
		if err != nil {
			return nil, err
		}
	case *parse.TestExpr:
		if tfn, ok := s.env.Tests[exp.Name]; ok {
			eargs := exp.Args
			args := make([]Value, len(eargs))
			for i, e := range eargs {
				v, err := s.evalExpr(e)
				if err != nil {
					return nil, err
				}
				args[i] = v
			}
			return func(v Value) bool {
				return tfn(s, v, args...)
			}, nil
		}
		return nil, fmt.Errorf(`unknown test "%v"`, exp.Name)
	case *parse.TernaryIfExpr:
		cond, err := s.evalExpr(exp.Cond)
		if err != nil {
			return nil, err
		}
		if CoerceBool(cond) == true {
			return s.evalExpr(exp.TrueX)
		}
		return s.evalExpr(exp.FalseX)

	case *parse.HashExpr:
		vals := make(map[string]Value)
		for _, v := range exp.Elements {
			var key Value
			var err error
			if k, ok := v.Key.(*parse.NameExpr); ok {
				key = k.Name
			} else {
				key, err = s.evalExpr(v.Key)
				if err != nil {
					return nil, err
				}
			}
			val, err := s.evalExpr(v.Value)
			if err != nil {
				return nil, err
			}
			vals[CoerceString(key)] = val
		}
		return vals, nil

	case *parse.ArrayExpr:
		vals := make([]Value, len(exp.Elements))
		for i, v := range exp.Elements {
			val, err := s.evalExpr(v)
			if err != nil {
				return nil, err
			}
			vals[i] = val
		}
		return vals, nil
	}

	return v, nil
}
614425
func (p *Predicate) Evaluate(t *Tuple, table *Table) (bool, error) {

	if p.True {
		return true, nil
	}

	// Find left
	var i = 0
	lenTable := len(table.attributes)
	for i = 0; i < lenTable; i++ {
		if table.attributes[i].name == p.LeftValue.lexeme {
			break
		}
	}
	if i == lenTable {
		return false, fmt.Errorf("Attribute [%s] not found in table [%s]", p.LeftValue.lexeme, table.name)
	}

	p.LeftValue.v = t.Values[i]
	return p.Operator(p.LeftValue, p.RightValue), nil
}
737915
func (a Anomalyzer) Eval() float64 {
	threshold := a.Conf.referenceSize + a.Conf.ActiveSize
	if a.Conf.Delay && len(a.Data) < threshold {
		return 0.0
	}
	probmap := make(map[string]float64)
	for _, method := range a.Conf.Methods {

		algorithm := Algorithms[method]
		prob := cap(algorithm(a.Data, *a.Conf), 0, 1)
		if prob != NA {
			// if highrank and lowrank methods exist then only listen to
			// the max of either
			if method == "highrank" || method == "lowrank" {
				if math.IsNaN(probmap["rank"]) {
					probmap["rank"] = 0
				}
				probmap["rank"] = math.Max(probmap["rank"], prob)
			} else {
				probmap[method] = prob
			}
		}
	}

	probs := make(govector.Vector, 0, len(probmap))
	weights := make(govector.Vector, 0, len(probmap))

	for method, prob := range probmap {
		if method == "magnitude" && prob < a.Conf.Sensitivity {
			return 0.0
		}
		probs = append(probs, prob)
		weights = append(weights, a.getWeight(method, prob))
	}

	// ignore the error since we force the length of probs
	// and the weights to be equal
	weighted, _ := probs.WeightedMean(weights)

	// if all the weights are zero, then our weighted mean
	// function attempts to divide by zero which returns a
	// NaN. we'd like it to return 0.
	if math.IsNaN(weighted) {
		weighted = 0
	}

	return weighted
}
606833
func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node, args []parse.Node, final, receiver reflect.Value) reflect.Value {
	if !receiver.IsValid() {
		return zero
	}
	typ := receiver.Type()
	receiver, _ = indirect(receiver)
	// Unless it's an interface, need to get to a value of type *T to guarantee
	// we see all methods of T and *T.
	ptr := receiver
	if ptr.Kind() != reflect.Interface && ptr.CanAddr() {
		ptr = ptr.Addr()
	}
	if method := ptr.MethodByName(fieldName); method.IsValid() {
		return s.evalCall(dot, method, node, fieldName, args, final)
	}
	hasArgs := len(args) > 1 || final.IsValid()
	// It's not a method; must be a field of a struct or an element of a map. The receiver must not be nil.
	receiver, isNil := indirect(receiver)
	if isNil {
		s.errorf("nil pointer evaluating %s.%s", typ, fieldName)
	}
	switch receiver.Kind() {
	case reflect.Struct:
		tField, ok := receiver.Type().FieldByName(fieldName)
		if ok {
			field := receiver.FieldByIndex(tField.Index)
			if tField.PkgPath != "" { // field is unexported
				s.errorf("%s is an unexported field of struct type %s", fieldName, typ)
			}
			// If it's a function, we must call it.
			if hasArgs {
				s.errorf("%s has arguments but cannot be invoked as function", fieldName)
			}
			return field
		}
		s.errorf("%s is not a field of struct type %s", fieldName, typ)
	case reflect.Map:
		// If it's a map, attempt to use the field name as a key.
		nameVal := reflect.ValueOf(fieldName)
		if nameVal.Type().AssignableTo(receiver.Type().Key()) {
			if hasArgs {
				s.errorf("%s is not a method but has arguments", fieldName)
			}
			return receiver.MapIndex(nameVal)
		}
	}
	s.errorf("can't evaluate field %s in type %s", fieldName, typ)
	panic("not reached")
}
1350771
func (c *Constant) EvalInt(ctx sessionctx.Context, _ chunk.Row) (int64, bool, error) {
	if c.DeferredExpr != nil {
		dt, err := c.DeferredExpr.Eval(chunk.Row{})
		if err != nil {
			return 0, true, err
		}
		if dt.IsNull() {
			return 0, true, nil
		}
		val, err := dt.ToInt64(ctx.GetSessionVars().StmtCtx)
		if err != nil {
			return 0, true, err
		}
		c.Value.SetInt64(val)
	} else {
		if c.GetType().Tp == mysql.TypeNull || c.Value.IsNull() {
			return 0, true, nil
		}
	}
	if c.GetType().Hybrid() || c.Value.Kind() == types.KindBinaryLiteral || c.Value.Kind() == types.KindString {
		res, err := c.Value.ToInt64(ctx.GetSessionVars().StmtCtx)
		return res, err != nil, err
	}
	return c.Value.GetInt64(), false, nil
}
170088
func (c *Constant) Eval(_ []types.Datum, _ context.Context) (types.Datum, error) {
	return c.Value, nil
}