aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/SetVarExpression.java
blob: 4f30e0b066f0ad208f9af5f930623b4832bf9017 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;

/**
 * @author Simon Thoresen Hult
 */
public final class SetVarExpression extends Expression {

    private final String varName;

    public SetVarExpression(String varName) {
        super(UnresolvedDataType.INSTANCE);
        this.varName = varName;
    }

    public String getVariableName() {
        return varName;
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        context.setVariable(varName, context.getValue());
    }

    @Override
    protected void doVerify(VerificationContext context) {
        DataType next = context.getValueType();
        DataType prev = context.getVariable(varName);
        if (prev != null && !prev.equals(next)) {
            throw new VerificationException(this, "Attempting to assign conflicting types to variable '" + varName +
                                                  "', " + prev.getName() + " vs " + next.getName());
        }
        context.setVariable(varName, next);
    }

    @Override
    public DataType createdOutputType() {
        return null;
    }

    @Override
    public String toString() {
        return "set_var " + varName;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof SetVarExpression rhs)) return false;
        if (!varName.equals(rhs.varName)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode() + varName.hashCode();
    }

}