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");
}
}
Aggregations