use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class CompModule method unique.
/**
* Throw an exception if there are more than 1 match; return nonnull if only one
* match; return null if no match.
*/
private Object unique(Pos pos, String name, List<Object> objs) throws Err {
if (objs.size() == 0)
return null;
if (objs.size() == 1)
return objs.get(0);
StringBuilder msg = new StringBuilder("The name \"").append(name);
msg.append("\" is ambiguous.\n" + "There are ").append(objs.size()).append(" choices:");
for (int i = 0; i < objs.size(); i++) {
msg.append("\n\n#").append(i + 1).append(": ");
Object x = objs.get(i);
if (x instanceof Sig) {
Sig y = (Sig) x;
msg.append("sig ").append(y.label).append("\n" + "at ").append(y.pos.toShortString());
} else if (x instanceof Func) {
Func y = (Func) x;
msg.append(y.isPred ? "pred " : "fun ").append(y.label).append("\n" + "at ").append(y.pos.toShortString());
} else if (x instanceof Expr) {
Expr y = (Expr) x;
msg.append("assertion at ").append(y.pos.toShortString());
}
}
throw new ErrorSyntax(pos, msg.toString());
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class Macro method instantiate.
/**
* Instantiate it.
*
* @param warnings - the list that will receive any warning we generate; can be
* null if we wish to ignore warnings
*/
Expr instantiate(Context cx, List<ErrorWarning> warnings) throws Err {
if (cx.unrolls <= 0) {
Pos p = span();
return new ExprBad(p, toString(), new ErrorType(p, "Macro substitution too deep; possibly indicating an infinite recursion."));
}
if (params.size() != args.size())
return this;
Context cx2 = new Context(realModule, warnings, cx.unrolls - 1);
for (int n = params.size(), i = 0; i < n; i++) {
Expr tmp = args.get(i);
if (!(tmp instanceof Macro))
tmp = tmp.resolve(tmp.type(), warnings);
cx2.put(params.get(i).label, tmp);
}
return cx2.check(body);
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class SimInstance method write.
/**
* Write the bitwidth, maxseq, set of all atoms, and map of all sig/field/var
* into the given file.
*/
private void write(BufferedOutputStream out) throws IOException {
write(out, "maxseq = " + maxseq + ("\n" + "bitwidth = ") + bitwidth + "\n");
for (Map.Entry<Expr, SimTupleset> entry : sfs.entrySet()) {
Expr e = entry.getKey();
if (e instanceof Sig)
write(out, "sig " + ((Sig) e).label + " = ");
else if (e instanceof Field)
write(out, "field " + ((Field) e).sig.label + " " + ((Field) e).label + " = ");
else if (e instanceof ExprVar)
write(out, "var " + ((ExprVar) e).label + " = ");
else
continue;
entry.getValue().write(out);
out.write('\n');
}
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class SimInstance method visit.
/**
* {@inheritDoc}
*/
@Override
public Object visit(ExprQt x) throws Err {
Expr xx = x.desugar();
if (xx instanceof ExprQt)
x = (ExprQt) xx;
else
return visitThis(xx);
if (x.op == ExprQt.Op.COMPREHENSION) {
TempList<SimTuple> ans = new TempList<SimTuple>();
enumerate(ans, 0, x, x.sub, 0);
return SimTupleset.make(ans.makeConst());
}
if (x.op == ExprQt.Op.ALL)
return enumerate(null, 0, x, x.sub.not(), 0) == 0;
if (x.op == ExprQt.Op.NO)
return enumerate(null, 0, x, x.sub, 0) == 0;
if (x.op == ExprQt.Op.SOME)
return enumerate(null, 0, x, x.sub, 0) >= 1;
if (x.op == ExprQt.Op.LONE)
return enumerate(null, 0, x, x.sub, 0) <= 1;
if (x.op == ExprQt.Op.ONE)
return enumerate(null, 0, x, x.sub, 0) == 1;
if (x.op == ExprQt.Op.SUM)
return trunc(enumerate(null, 0, x, x.sub, 0));
throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprQt.accept()");
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class SimInstance method visit.
/**
* {@inheritDoc}
*/
@Override
public SimTupleset visit(Field x) throws Err {
if (x.defined) {
final ExprVar v = (ExprVar) (x.sig.decl.get());
final Expr b = x.decl().expr;
final Env<ExprVar, Object> oldenv = env;
env = new Env<ExprVar, Object>();
if (!b.hasVar(v)) {
SimTupleset ans = cset(x.sig).product(cset(b));
env = oldenv;
return ans;
}
SimTupleset ans = SimTupleset.EMPTY;
for (SimTuple a : visit(x.sig)) {
SimTupleset left = SimTupleset.make(a);
env.put(v, left);
SimTupleset right = cset(b);
env.remove(v);
ans = left.product(right).union(ans);
}
env = oldenv;
return ans;
}
Object ans = sfs.get(x);
if (ans instanceof SimTupleset)
return (SimTupleset) ans;
else
throw new ErrorFatal("Unknown field " + x + " encountered during evaluation.");
}
Aggregations