Search in sources :

Example 6 with Split

use of gov.sandia.n2a.language.Split in project n2a by frothga.

the class JobC method multiconditional.

/**
 *        Emit the equations associated with a variable.
 *        Assumes that phase indicators have already been factored out by simplify().
 */
public void multiconditional(Variable v, RendererC context, String pad) throws Exception {
    boolean connect = context.part.getConnect();
    boolean init = context.part.getInit();
    boolean isType = v.name.equals("$type");
    if (v.hasAttribute("temporary"))
        context.result.append(pad + type(v) + " " + mangle(v) + ";\n");
    // Select the default equation
    EquationEntry defaultEquation = null;
    for (EquationEntry e : v.equations) if (e.ifString.isEmpty())
        defaultEquation = e;
    // Initialize static objects, and dump dynamic objects needed by conditions
    for (EquationEntry e : v.equations) {
        if (e.condition != null)
            prepareDynamicObjects(e.condition, context, init, pad);
    }
    // Write the conditional equations
    boolean haveIf = false;
    String padIf = pad;
    for (EquationEntry e : v.equations) {
        // Must skip the default equation, as it will be emitted last.
        if (e == defaultEquation)
            continue;
        if (e.condition != null) {
            String ifString;
            if (haveIf) {
                ifString = "else if (";
            } else {
                ifString = "if (";
                haveIf = true;
                padIf = pad + "  ";
            }
            context.result.append(pad + ifString);
            e.condition.render(context);
            context.result.append(")\n");
            context.result.append(pad + "{\n");
        }
        if (isType) {
            // per the N2A language document.
            if (!(e.expression instanceof Split)) {
                Backend.err.get().println("Unexpected expression for $type");
                throw new Backend.AbortRun();
            }
            int index = context.part.splits.indexOf(((Split) e.expression).parts);
            context.result.append(padIf + resolve(v.reference, context, true) + " = " + (index + 1) + ";\n");
        } else {
            prepareDynamicObjects(e.expression, context, init, pad);
            context.result.append(padIf);
            renderEquation(context, e);
        }
        if (haveIf)
            context.result.append(pad + "}\n");
    }
    // Write the default equation
    if (defaultEquation == null) {
        if (v.hasAttribute("temporary")) {
            if (haveIf) {
                context.result.append(pad + "else\n");
                context.result.append(pad + "{\n");
            }
            context.result.append(padIf + zero(resolve(v.reference, context, true), v) + ";\n");
            if (haveIf)
                context.result.append(pad + "}\n");
        } else {
            String defaultValue = null;
            if (isType) {
                // always reset $type to 0
                defaultValue = "0";
            } else if (connect && v.name.equals("$p")) {
                defaultValue = "1";
            } else {
                // to copy forward the current buffered value.
                if (v.assignment == Variable.REPLACE && // local to the equation set, not a reference to an outside variable
                v.reference.variable.container == v.container && v.equations.size() > 0 && // buffered
                v.hasAny("cycle", "externalRead") && !v.hasAttribute("initOnly") && !init && // not in a phase that skips buffering
                !connect) {
                    // copy previous value
                    defaultValue = resolve(v.reference, context, false);
                }
            }
            if (defaultValue != null) {
                if (haveIf) {
                    context.result.append(pad + "else\n");
                    context.result.append(pad + "{\n");
                }
                context.result.append(padIf + resolve(v.reference, context, true) + " = " + defaultValue + ";\n");
                if (haveIf)
                    context.result.append(pad + "}\n");
            }
        }
    } else {
        if (haveIf) {
            context.result.append(pad + "else\n");
            context.result.append(pad + "{\n");
        }
        if (isType) {
            ArrayList<EquationSet> split = ((Split) defaultEquation.expression).parts;
            int index = context.part.splits.indexOf(split);
            context.result.append(padIf + resolve(v.reference, context, true) + " = " + (index + 1) + ";\n");
        } else {
            prepareDynamicObjects(defaultEquation.expression, context, init, pad);
            context.result.append(padIf);
            renderEquation(context, defaultEquation);
        }
        if (haveIf)
            context.result.append(pad + "}\n");
    }
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) Split(gov.sandia.n2a.language.Split) AbortRun(gov.sandia.n2a.plugins.extpoints.Backend.AbortRun) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint)

Aggregations

Split (gov.sandia.n2a.language.Split)6 AccessVariable (gov.sandia.n2a.language.AccessVariable)4 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)3 Operator (gov.sandia.n2a.language.Operator)3 Scalar (gov.sandia.n2a.language.type.Scalar)3 AbortRun (gov.sandia.n2a.plugins.extpoints.Backend.AbortRun)3 TreeSet (java.util.TreeSet)3 EquationSet (gov.sandia.n2a.eqset.EquationSet)2 Visitor (gov.sandia.n2a.language.Visitor)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 AccountableConnection (gov.sandia.n2a.eqset.EquationSet.AccountableConnection)1 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)1 Variable (gov.sandia.n2a.eqset.Variable)1 VariableReference (gov.sandia.n2a.eqset.VariableReference)1 Transformer (gov.sandia.n2a.language.Transformer)1 UnitValue (gov.sandia.n2a.language.UnitValue)1 Output (gov.sandia.n2a.language.function.Output)1 Instance (gov.sandia.n2a.language.type.Instance)1 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)1