Search in sources :

Example 31 with Expr

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());
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) Func(edu.mit.csail.sdg.ast.Func)

Example 32 with Expr

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);
}
Also used : Context(edu.mit.csail.sdg.parser.CompModule.Context) ExprBad(edu.mit.csail.sdg.ast.ExprBad) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos)

Example 33 with Expr

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');
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) Field(edu.mit.csail.sdg.ast.Sig.Field) Expr(edu.mit.csail.sdg.ast.Expr) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 34 with Expr

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()");
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ExprQt(edu.mit.csail.sdg.ast.ExprQt)

Example 35 with Expr

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.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr)

Aggregations

Expr (edu.mit.csail.sdg.ast.Expr)46 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)17 Sig (edu.mit.csail.sdg.ast.Sig)15 Func (edu.mit.csail.sdg.ast.Func)12 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 Decl (edu.mit.csail.sdg.ast.Decl)8 Field (edu.mit.csail.sdg.ast.Sig.Field)8 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BinaryExpression (kodkod.ast.BinaryExpression)5