// Expressions
// VisitExpression implements corresponding Visitor interface method
10445
func (v *evalVisitor) VisitExpression(node *ast.Expression) interface{} {
	v.at(node)

	var result interface{}
	done := false

	v.pushExpr(node)

	// helper call
	if helperName := node.HelperName(); helperName != "" {
		if helper := v.findHelper(helperName); helper != zero {
			result = v.callHelper(helperName, helper, node)
			done = true
		}
	}

	if !done {
		// literal
		if literal, ok := node.LiteralStr(); ok {
			if val := v.evalField(v.curCtx(), literal, true); val.IsValid() {
				result = val.Interface()
				done = true
			}
		}
	}

	if !done {
		// field path
		if path := node.FieldPath(); path != nil {
			// @todo Find a cleaner way ! Don't break the pattern !
			// this is an exception to visitor pattern, because we need to pass the info
			// that this path is at root of current expression
			if val := v.evalPathExpression(path, true); val != nil {
				result = val
			}
		}
	}

	v.popExpr()

	return result
}
300280
func Inspect(expr Expression, f func(Expression) bool) {
	Walk(inspector(f), expr)
}
24223
func (e Expression) returnExpression(expressionType expressionType) *string {
	if e.expressionMap == nil {
		return nil
	}
	if s, exists := e.expressionMap[expressionType]; exists {
		return &s
	}
	return nil
}
121722
func (m *dbConn) DeleteExpression(p interface{}, where expr.Node) (int, error) {

	var deletedKeys []schema.Key
	txn := m.db.Txn(true)
	iter, err := txn.Get(m.md.tbl.Name, m.md.primaryIndex)
	if err != nil {
		txn.Abort()
		u.Errorf("could not get values %v", err)
		return 0, err
	}
deleteLoop:
	for {
		item := iter.Next()
		if item == nil {
			break
		}

		msg, ok := item.(*datasource.SqlDriverMessage)
		if !ok {
			u.Warnf("wat?  %T   %#v", item, item)
			err = fmt.Errorf("unexpected message type %T", item)
			break
		}
		whereValue, ok := vm.Eval(msg.ToMsgMap(m.md.tbl.FieldPositions), where)
		if !ok {
			u.Debugf("could not evaluate where: %v", msg)
		}
		switch whereVal := whereValue.(type) {
		case value.BoolValue:
			if whereVal.Val() == false {
				//this means do NOT delete
			} else {
				// Delete!
				if err = txn.Delete(m.md.tbl.Name, msg); err != nil {
					u.Errorf("could not delete %v", err)
					break deleteLoop
				}
				indexVal := msg.Vals[0]
				deletedKeys = append(deletedKeys, schema.NewKeyUint(makeId(indexVal)))
			}
		case nil:
			// ??
			u.Warnf("this should be fine, couldn't evaluate so don't delete %v", msg)
		default:
			if whereVal.Nil() {
				// Doesn't match, so don't delete
			} else {
				u.Warnf("unknown where eval result? %T", whereVal)
			}
		}
	}
	if err != nil {
		txn.Abort()
		return 0, err
	}
	txn.Commit()
	return len(deletedKeys), nil
}
1105923
func (f *regularFieldsPrinter) VisitArray(a *proto.Array) {
	a.SubType.Accept(f)
}
1147308
func (g *Graph) evalExpression(p *Graph) (interface{}, error) {

	// Return nil and empty strings as is
	if p.This == nil {
		return nil, ErrNilReceiver
	}

	s := p.ThisString()

	if len(s) == 0 {
		return "", nil
	}

	// first check if it is a number because it can have an operatorChar
	// in front: the minus sign
	if isNumber(s) {
		return p.ThisNumber(), nil
	}

	switch s {
	case "!":
		// Unary expression !expr
		return !g.evalBool(p.Out[0]), nil
	case TypeExpression:
		return g.evalExpression(p.GetAt(0))
	case TypePath:
		return g.evalPath(p)
	case TypeGroup:
		// TODO expression list (could also be OGDL flow!)
		r := New(TypeGroup)
		for _, expr := range p.Out {
			itf, err := g.evalExpression(expr)
			if err == nil {
				r.Add(itf)
			}
		}
		return r, nil
	case TypeString:
		if p.Len() == 0 {
			return "", nil
		}
		return p.GetAt(0).ThisString(), nil

	}

	c := s[0]

	// [!] Operator should be identified. Operators written as strings are
	// missinterpreted.
	if isOperatorChar(c) {
		if len(s) <= 2 {
			if len(s) == 1 || isOperatorChar(s[1]) {
				return g.evalBinary(p), nil
			}
		}
	}

	if c == '"' || c == '\'' {
		return s, nil
	}

	if IsLetter(rune(c)) {
		if s == "false" {
			return false, nil
		}
		if s == "true" {
			return true, nil
		}
		return s, nil
	}

	return p, nil
}
1240803
func (v *arraySchemaVisitor) VisitArray(result *proto.Array) {
	v.Result = result
	v.Kind = "array"
	v.Err = copySubElementPatchStrategy(result.Path.String(), result.GetExtensions(), result.SubType.GetExtensions())
}
426980
func expression(res ...*regexp.Regexp) *regexp.Regexp {
	var s string
	for _, re := range res {
		s += re.String()
	}

	return match(s)
}
786482
func GetParamExpression(ctx sessionctx.Context, v *driver.ParamMarkerExpr) (Expression, error) {
	useCache := ctx.GetSessionVars().StmtCtx.UseCache
	tp := types.NewFieldType(mysql.TypeUnspecified)
	types.DefaultParamTypeForValue(v.GetValue(), tp)
	value := &Constant{Value: v.Datum, RetType: tp}
	if useCache {
		f, err := NewFunctionBase(ctx, ast.GetParam, &v.Type,
			DatumToConstant(types.NewIntDatum(int64(v.Order)), mysql.TypeLonglong))
		if err != nil {
			return nil, err
		}
		f.GetType().Tp = v.Type.Tp
		value.DeferredExpr = f
	}
	return value, nil
}
890957
func (s *PlacementConstraint) SetExpression(v string) *PlacementConstraint {
	s.Expression = &v
	return s
}
167748
func (pp *primitiveParser) visitStruct(reachable map[string]*data, k string, visible bool) {
	if _, ok := reachable[k]; ok { // already included
		return
	}

	s := pp.structmap[k]

	d := data{
		Name: k,
		Byte: pp.typedecls[pp.fset.Position(s.Pos()).Line],
	}
	d.Immutable, d.Version, d.Fields = pp.gatherFields(reachable, s, visible)
	d.Visible = visible || d.Immutable

	sort.Sort(sortFields(d.Fields))
	reachable[k] = &d
}
723550
func (p *Parser) Expression() bool {

	if !p.UnaryExpression() {
		return false
	}

	for {
		p.Space()
		b, ok := p.Operator()
		if ok {
			p.ev.Add(b)
		} else {
			return true
		}
		p.Space()
		if !p.UnaryExpression() {
			return false // error
		}
		p.Space()
	}
}
819576
func (vis *GenericVisitor) Visit(x interface{}) Visitor {
	if vis.f(x) {
		return nil
	}
	return vis
}
339688
func (visitor *toStringVisitor) VisitPrefixedExp(exp *sql.PrefixedExp) {
	if exp.Prefix == "FROM" {
		visitor.generated.WriteString(" FROM ")
		visitor.generated.WriteString(sql.EntityTableName(visitor.entity))
	} else {
		visitor.generated.WriteString(exp.Prefix)
	}
	if exp.AfterPrefix != nil {
		for _, exp := range exp.AfterPrefix {
			exp.Accept(visitor)
		}
	}
	visitor.generated.WriteString(exp.Suffix)

	if exp.Prefix != "FROM" && exp.Binding != nil && len(exp.Binding) > 0 {
		if visitor.binding != nil {
			visitor.binding = append(visitor.binding, exp.Binding...)
		} else {
			visitor.binding = exp.Binding
		}
	}
}
1359931
func (v *printVisitor) VisitSubExpression(node *SubExpression) interface{} {
	node.Expression.Accept(v)

	return nil
}
420923
func (v *evalVisitor) VisitMustache(node *ast.MustacheStatement) interface{} {
	v.at(node)

	// evaluate expression
	expr := node.Expression.Accept(v)

	// check if this is a safe string
	isSafe := isSafeString(expr)

	// get string value
	str := Str(expr)
	if !isSafe && !node.Unescaped {
		// escape html
		str = Escape(str)
	}

	return str
}
430893
func (fg *FilterGenerator) walkExpr(node expr.Node, depth int) (interface{}, error) {
	if depth > MaxDepth {
		return nil, fmt.Errorf("hit max depth on segment generation. bad query?")
	}
	//u.Debugf("%d fg.expr T:%T  %#v", depth, node, node)
	var err error
	var filter interface{}
	switch n := node.(type) {
	case *expr.UnaryNode:
		// Urnaries do their own negation
		filter, err = fg.unaryExpr(n, depth+1)
	case *expr.BooleanNode:
		// Also do their own negation
		filter, err = fg.booleanExpr(n, depth+1)
	case *expr.BinaryNode:
		filter, err = fg.binaryExpr(n, depth+1)
	case *expr.TriNode:
		filter, err = fg.triExpr(n, depth+1)
	case *expr.IdentityNode:
		iv := strings.ToLower(n.Text)
		switch iv {
		case "match_all", "*":
			return MatchAll, nil
		}
		//HACK As a special case support true as "match_all"; we could support
		//    false -> MatchNone, but that seems useless and wasteful of ES cpu.
		if n.Bool() {
			return MatchAll, nil
		}
		u.Warnf("What is this? %v", n)
	case *expr.IncludeNode:
		if incErr := vm.ResolveIncludes(fg.inc, n); incErr != nil {
			return nil, incErr
		}
		filter, err = fg.walkExpr(n.ExprNode, depth+1)
	case *expr.FuncNode:
		filter, err = fg.funcExpr(n, depth+1)
	default:
		u.Warnf("not handled %v", node)
		return nil, fmt.Errorf("qlindex: unsupported node in expression: %T (%s)", node, node)
	}
	if err != nil {
		// Convert MissingField errors to a logical `false`
		if _, ok := err.(*gentypes.MissingFieldError); ok {
			//u.Debugf("depth=%d filters=%s missing field: %s", depth, node, err)
			return MatchNone, nil
		}
		return nil, err
	}

	nn, isNegateable := node.(expr.NegateableNode)
	if isNegateable {
		if nn.Negated() {
			return NotFilter(filter), nil
		}
	}
	return filter, nil
}