Search in sources :

Example 6 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(ExprCall x) throws Err {
    final Func f = x.fun;
    final int n = f.count();
    final Object candidate = n == 0 ? cacheForConstants.get(f) : null;
    if (candidate != null)
        return candidate;
    final Expr body = f.getBody();
    if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
        throw new ErrorType(body.span(), "Function return value not fully resolved.");
    for (Func ff : current_function) if (ff == f)
        throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
    Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
    List<SimTupleset> list = new ArrayList<SimTupleset>(x.args.size());
    for (int i = 0; i < n; i++) {
        SimTupleset ts = cset(x.args.get(i));
        newenv.put(f.get(i), ts);
        list.add(ts);
    }
    final SimCallback cb = callbacks.get(f);
    if (cb != null) {
        try {
            Object answer = cb.compute(f, list);
            if (answer != null) {
                if (x.args.size() == 0)
                    cacheForConstants.put(f, answer);
                return answer;
            }
        } catch (Exception ex) {
        // if the callback failed, we can just continue with our
        // original attempt to evaluate this call
        }
    }
    Env<ExprVar, Object> oldenv = env;
    env = newenv;
    current_function.add(f);
    Object ans = visitThis(body);
    env = oldenv;
    current_function.remove(current_function.size() - 1);
    if (f.count() == 0)
        cacheForConstants.put(f, ans);
    return ans;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Func(edu.mit.csail.sdg.ast.Func) ArrayList(java.util.ArrayList) Env(edu.mit.csail.sdg.alloy4.Env) IOException(java.io.IOException) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 7 with Expr

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

the class CompModule method getRawQS.

/**
 * Lookup a fully-qualified Sig/Func/Assertion from the current module; it skips
 * PARAMs.
 */
private List<Object> getRawQS(final int r, String name) {
    // (r&1)!=0 => Sig, (r&2) != 0 => assertion, (r&4)!=0 => Func
    List<Object> ans = new ArrayList<Object>();
    CompModule u = this;
    if (name.startsWith("this/"))
        name = name.substring(5);
    for (int level = 0; ; level++) {
        int i = name.indexOf('/');
        if (i < 0) {
            if ((r & 1) != 0) {
                Sig x = u.sigs.get(name);
                if (x != null)
                    if (level == 0 || x.isPrivate == null)
                        ans.add(x);
            }
            if ((r & 2) != 0) {
                Expr x = u.asserts.get(name);
                if (x != null)
                    ans.add(x);
            }
            if ((r & 4) != 0) {
                ArrayList<Func> x = u.funcs.get(name);
                if (x != null)
                    for (Func y : x) if (level == 0 || y.isPrivate == null)
                        ans.add(y);
            }
            if (ans.size() == 0)
                // If nothing at this
                return u.getRawNQS(this, r, name);
            // from this module
            return ans;
        }
        String alias = name.substring(0, i);
        Open uu = u.opens.get(alias);
        if (uu == null || uu.realModule == null)
            // may happen during the initial "module"
            return ans;
        if (level > 0 && uu.isPrivate)
            // that means the module is imported privately
            return ans;
        u = uu.realModule;
        name = name.substring(i + 1);
    }
}
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) Expr(edu.mit.csail.sdg.ast.Expr) Func(edu.mit.csail.sdg.ast.Func) ArrayList(java.util.ArrayList)

Example 8 with Expr

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

the class CompModule method resolveMeta.

// ============================================================================================================================//
private static void resolveMeta(final CompModule root) throws Err {
    // Now, add the meta sigs and fields if needed
    Map<Sig, PrimSig> sig2meta = new LinkedHashMap<Sig, PrimSig>();
    Map<Field, PrimSig> field2meta = new LinkedHashMap<Field, PrimSig>();
    boolean hasMetaSig = false, hasMetaField = false;
    root.new2old.put(root.metaSig, root.metaSig);
    root.sigs.put(base(root.metaSig), root.metaSig);
    root.new2old.put(root.metaField, root.metaField);
    root.sigs.put(base(root.metaField), root.metaField);
    for (CompModule m : root.allModules) for (Sig s : new ArrayList<Sig>(m.sigs.values())) if (m != root || (s != root.metaSig && s != root.metaField)) {
        PrimSig ka = new PrimSig(s.label + "$", root.metaSig, Attr.ONE, PRIVATE.makenull(s.isPrivate), Attr.META);
        sig2meta.put(s, ka);
        ka.addDefinedField(Pos.UNKNOWN, null, Pos.UNKNOWN, "value", s);
        m.new2old.put(ka, ka);
        m.sigs.put(base(ka), ka);
        hasMetaSig = true;
        Expr allfields = ExprConstant.EMPTYNESS;
        for (Field field : s.getFields()) {
            Pos priv = field.isPrivate;
            if (priv == null)
                priv = s.isPrivate;
            PrimSig kb = new PrimSig(s.label + "$" + field.label, root.metaField, Attr.ONE, PRIVATE.makenull(priv), Attr.META);
            field2meta.put(field, kb);
            m.new2old.put(kb, kb);
            m.sigs.put(base(kb), kb);
            hasMetaField = true;
            kb.addDefinedField(Pos.UNKNOWN, null, Pos.UNKNOWN, "value", field);
            if (allfields == ExprConstant.EMPTYNESS)
                allfields = kb;
            else
                allfields = allfields.plus(kb);
        }
        ka.addDefinedField(Pos.UNKNOWN, null, Pos.UNKNOWN, "fields", allfields);
    }
    for (Map.Entry<Sig, PrimSig> e : sig2meta.entrySet()) {
        Expr expr = null;
        if ((e.getKey()) instanceof PrimSig) {
            PrimSig a = (PrimSig) (e.getKey());
            if (a.parent != null && a.parent != UNIV)
                expr = sig2meta.get(a.parent);
        }
        e.getValue().addDefinedField(Pos.UNKNOWN, null, Pos.UNKNOWN, "parent", (expr == null ? ExprConstant.EMPTYNESS : expr));
    }
    for (Map.Entry<Sig, PrimSig> e : sig2meta.entrySet()) {
        Sig s = e.getKey();
        PrimSig s2 = e.getValue();
        Expr allfields = ExprConstant.EMPTYNESS;
        for (Field f : s.getFields()) {
            PrimSig metaF = field2meta.get(f);
            if (allfields == ExprConstant.EMPTYNESS)
                allfields = metaF;
            else
                allfields = allfields.plus(metaF);
        }
        if (s instanceof PrimSig)
            for (Sig c : (((PrimSig) s).descendents())) for (Field f : c.getFields()) {
                PrimSig metaF = field2meta.get(f);
                if (allfields == ExprConstant.EMPTYNESS)
                    allfields = metaF;
                else
                    allfields = allfields.plus(metaF);
            }
        s2.addDefinedField(Pos.UNKNOWN, null, Pos.UNKNOWN, "subfields", allfields);
    }
    if (hasMetaSig == false)
        root.facts.add(new Pair<String, Expr>("sig$fact", root.metaSig.no().and(root.metaField.no())));
    else if (hasMetaField == false)
        root.facts.add(new Pair<String, Expr>("sig$fact", root.metaField.no()));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 9 with Expr

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

the class CompModule method resolveAssertions.

/**
 * Each assertion name now points to a typechecked Expr rather than an
 * untypechecked Exp.
 */
private JoinableList<Err> resolveAssertions(A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    Context cx = new Context(this, warns);
    for (Map.Entry<String, Expr> e : asserts.entrySet()) {
        Expr expr = e.getValue();
        expr = cx.check(expr).resolve_as_formula(warns);
        if (expr.errors.isEmpty()) {
            e.setValue(expr);
            rep.typecheck("Assertion " + e.getKey() + ": " + expr.type() + "\n");
        } else
            errors = errors.make(expr.errors);
    }
    return errors;
}
Also used : Expr(edu.mit.csail.sdg.ast.Expr) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with Expr

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

the class CompModule method addOpen.

/**
 * Add an OPEN declaration.
 */
void addOpen(Pos pos, Pos isPrivate, ExprVar name, List<ExprVar> args, ExprVar alias) throws Err {
    if (status > 2)
        throw new ErrorSyntax(pos, "The \"open\" declaration must occur before any\n" + "sig/pred/fun/fact/assert/check/run command.");
    String as = (alias == null ? "" : alias.label);
    if (name.label.length() == 0)
        throw new ErrorSyntax(name.span(), "The filename cannot be empty.");
    if (as.indexOf('$') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'$\' character");
    if (as.indexOf('@') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'@\' character");
    if (as.indexOf('/') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'/\' character");
    if (as.length() == 0) {
        as = "open$" + (1 + opens.size());
        if (args == null || args.size() == 0) {
            for (int i = 0; ; i++) {
                if (i >= name.label.length()) {
                    as = name.label;
                    break;
                }
                char c = name.label.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                    continue;
                if (i == 0)
                    break;
                if (!(c >= '0' && c <= '9') && c != '_' && c != '\'' && c != '\"')
                    break;
            }
        }
    }
    final TempList<String> newlist = new TempList<String>(args == null ? 0 : args.size());
    if (args != null)
        for (int i = 0; i < args.size(); i++) {
            ExprVar arg = args.get(i);
            if (arg.label.length() == 0)
                throw new ErrorSyntax(arg.span(), "Argument cannot be empty.");
            if (arg.label.indexOf('@') >= 0)
                throw new ErrorSyntax(arg.span(), "Argument cannot contain the \'@\' chracter.");
            newlist.add(arg.label);
        }
    Open x = opens.get(as);
    if (x != null) {
        // we allow this, especially because of util/sequniv
        if (x.args.equals(newlist.makeConst()) && x.filename.equals(name.label))
            return;
        throw new ErrorSyntax(pos, "You cannot import two different modules\n" + "using the same alias.");
    }
    List<Expr> expressions = new ArrayList<>(args == null ? Collections.emptySet() : args);
    expressions.add(0, name);
    expressions.add(alias);
    x = new Open(pos, isPrivate != null, as, newlist.makeConst(), name.label, expressions);
    opens.put(as, x);
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Expr(edu.mit.csail.sdg.ast.Expr) ArrayList(java.util.ArrayList)

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