Search in sources :

Example 11 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class A4SolutionReader method parseSkolem.

/**
 * Parse skolem.
 */
private ExprVar parseSkolem(String id) throws IOException, Err {
    final XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown ID " + id + " encountered.");
    if (!node.is("skolem"))
        throw new IOException("ID " + id + " is not a skolem.");
    String label = label(node);
    Expr type = null;
    for (XMLNode sub : node) if (sub.is("types")) {
        Expr t = parseType(sub);
        if (type == null)
            type = t;
        else
            type = type.plus(t);
    }
    int arity;
    if (type == null || (arity = type.type().arity()) < 1)
        throw new IOException("Skolem " + label + " is maltyped.");
    ExprVar var = ExprVar.make(Pos.UNKNOWN, label, type.type());
    TupleSet ts = parseTuples(node, arity);
    expr2ts.put(var, ts);
    return var;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) TupleSet(kodkod.instance.TupleSet) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) IOException(java.io.IOException)

Example 12 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class VizTree method convertValueToText.

/**
 * {@inheritDoc}
 */
@Override
public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
    String c = ">";
    if (onWindows)
        c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
    if (val instanceof A4Solution)
        return "<html> <b" + c + encode(title == null ? "" : title) + "</b></html>";
    if (val instanceof Sig) {
        String label = ((Sig) val).label;
        if (label.startsWith("this/"))
            label = label.substring(5);
        return "<html> <b" + c + "sig " + encode(label) + "</b></html>";
    }
    if (val instanceof ExprVar)
        return "<html> <b" + c + "set " + encode(((ExprVar) val).label) + "</b></html>";
    if (val instanceof String)
        return "<html> <span" + c + encode((String) val) + "</span></html>";
    if (val instanceof Pair)
        return "<html> <b" + c + "field " + encode(((ExprHasName) (((Pair<?, ?>) val).b)).label) + "</b></html>";
    if (val instanceof A4Tuple) {
        StringBuilder sb = new StringBuilder("<html> <span" + c);
        A4Tuple tp = (A4Tuple) val;
        for (int i = 1; i < tp.arity(); i++) {
            if (i > 1)
                sb.append(" -> ");
            sb.append(encode(tp.atom(i)));
        }
        sb.append("</span></html>");
        return sb.toString();
    }
    return "";
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 13 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class CompModule method resolveFuncDecls.

/**
 * Each FunAST will now point to a bodyless Func object.
 */
private JoinableList<Err> resolveFuncDecls(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    for (ArrayList<Func> list : funcs.values()) {
        for (int listi = 0; listi < list.size(); listi++) {
            Func f = list.get(listi);
            String fullname = (path.length() == 0 ? "this/" : (path + "/")) + f.label;
            // Each PARAMETER can refer to earlier parameter in the same
            // function, and any SIG or FIELD visible from here.
            // Each RETURNTYPE can refer to the parameters of the same
            // function, and any SIG or FIELD visible from here.
            Context cx = new Context(this, warns);
            cx.rootfunparam = true;
            TempList<Decl> tmpdecls = new TempList<Decl>();
            boolean err = false;
            for (Decl d : f.decls) {
                TempList<ExprVar> tmpvars = new TempList<ExprVar>();
                Expr val = cx.check(d.expr).resolve_as_set(warns);
                if (!val.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(val.errors);
                }
                for (ExprHasName n : d.names) {
                    ExprVar v = ExprVar.make(n.span(), n.label, val.type());
                    cx.put(n.label, v);
                    tmpvars.add(v);
                    rep.typecheck((f.isPred ? "pred " : "fun ") + fullname + ", Param " + n.label + ": " + v.type() + "\n");
                }
                tmpdecls.add(new Decl(d.isPrivate, d.disjoint, d.disjoint2, tmpvars.makeConst(), val));
            }
            Expr ret = null;
            if (!f.isPred) {
                ret = cx.check(f.returnDecl).resolve_as_set(warns);
                if (!ret.errors.isEmpty()) {
                    err = true;
                    errors = errors.make(ret.errors);
                }
            }
            if (err)
                continue;
            try {
                f = new Func(f.pos, f.isPrivate, fullname, tmpdecls.makeConst(), ret, f.getBody());
                list.set(listi, f);
                rep.typecheck("" + f + ", RETURN: " + f.returnDecl.type() + "\n");
            } catch (Err ex) {
                errors = errors.make(ex);
            }
        }
    }
    return errors;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) Decl(edu.mit.csail.sdg.ast.Decl) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName)

Example 14 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.

the class SimInstance method read.

/**
 * Construct a new simulation context by reading the given file.
 */
public static synchronized SimInstance read(Module root, String filename, List<ExprVar> vars) throws Err, IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(filename);
        bis = new BufferedInputStream(fis);
        // read maxseq
        if (!readkey(bis).equals("maxseq"))
            throw new IOException("Expecting maxseq = ...");
        int maxseq = readNonNegativeIntThenLinebreak(bis);
        // read bitwidth
        if (!readkey(bis).equals("bitwidth"))
            throw new IOException("Expecting bitwidth = ...");
        int bitwidth = readNonNegativeIntThenLinebreak(bis);
        // construct the SimInstance object with no atoms and no relations
        SimInstance ans = new SimInstance(root, bitwidth, maxseq);
        // parse all the relations
        Map<String, SimTupleset> sfs = new HashMap<String, SimTupleset>();
        while (true) {
            String key = readkey(bis);
            if (key.length() == 0)
                // we don't expect any more data after this
                break;
            sfs.put(key, SimTupleset.read(bis));
        }
        // assign its value in the new SimInstance's sfs map
        for (final Sig s : root.getAllReachableSigs()) if (!s.builtin) {
            SimTupleset ts = sfs.get("sig " + s.label);
            if (ts != null)
                ans.sfs.put(s, ts);
            for (final Field f : s.getFields()) if (!f.defined) {
                ts = sfs.get("field " + s.label + " " + f.label);
                if (ts != null)
                    ans.sfs.put(f, ts);
            }
        }
        // assign its value in the new SimInstance's sfs map
        if (vars != null)
            for (ExprVar v : vars) {
                SimTupleset ts = sfs.get("var " + v.label);
                if (ts != null)
                    ans.sfs.put(v, ts);
            }
        // close the files then return the answer
        bis.close();
        bis = null;
        fis.close();
        fis = null;
        return ans;
    } finally {
        // free the temporary array
        readcache = null;
        // if an exception occurred, we'll try to close to files anyway,
        // since open file descriptors is a scarce resource
        Util.close(bis);
        Util.close(fis);
    }
}
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) BufferedInputStream(java.io.BufferedInputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 15 with ExprVar

use of edu.mit.csail.sdg.ast.ExprVar 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)

Aggregations

ExprVar (edu.mit.csail.sdg.ast.ExprVar)22 Expr (edu.mit.csail.sdg.ast.Expr)11 Sig (edu.mit.csail.sdg.ast.Sig)11 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)8 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)7 Field (edu.mit.csail.sdg.ast.Sig.Field)7 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)5 Func (edu.mit.csail.sdg.ast.Func)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 SafeList (edu.mit.csail.sdg.alloy4.SafeList)3 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)3 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)3 A4Solution (edu.mit.csail.sdg.translator.A4Solution)3 IOException (java.io.IOException)3 IdentityHashMap (java.util.IdentityHashMap)3 Map (java.util.Map)3