Search in sources :

Example 6 with ErrorSyntax

use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.

the class CompModule method resolveCommand.

/**
 * Resolve a particular command.
 */
private Command resolveCommand(Command cmd, ConstList<Sig> exactSigs, Expr globalFacts) throws Err {
    Command parent = cmd.parent == null ? null : resolveCommand(cmd.parent, exactSigs, globalFacts);
    String cname = ((ExprVar) (cmd.formula)).label;
    Expr e;
    Clause declaringClause = null;
    if (cmd.check) {
        // We prefer assertion in the
        List<Object> m = getRawQS(2, cname);
        // topmost module
        if (m.size() == 0 && cname.indexOf('/') < 0)
            m = getRawNQS(this, 2, cname);
        if (m.size() > 1)
            unique(cmd.pos, cname, m);
        if (m.size() < 1)
            throw new ErrorSyntax(cmd.pos, "The assertion \"" + cname + "\" cannot be found.");
        Expr expr = (Expr) (m.get(0));
        e = expr.not();
    } else {
        // We prefer fun/pred in the
        List<Object> m = getRawQS(4, cname);
        // topmost module
        if (m.size() == 0 && cname.indexOf('/') < 0)
            m = getRawNQS(this, 4, cname);
        if (m.size() > 1)
            unique(cmd.pos, cname, m);
        if (m.size() < 1)
            throw new ErrorSyntax(cmd.pos, "The predicate/function \"" + cname + "\" cannot be found.");
        Func f = (Func) (m.get(0));
        declaringClause = f;
        e = f.getBody();
        if (!f.isPred)
            e = e.in(f.returnDecl);
        if (f.decls.size() > 0)
            e = ExprQt.Op.SOME.make(null, null, f.decls, e);
    }
    if (e == null)
        e = ExprConstant.TRUE;
    TempList<CommandScope> sc = new TempList<CommandScope>(cmd.scope.size());
    for (CommandScope et : cmd.scope) {
        Sig s = getRawSIG(et.sig.pos, et.sig.label);
        if (s == null)
            throw new ErrorSyntax(et.sig.pos, "The sig \"" + et.sig.label + "\" cannot be found.");
        sc.add(new CommandScope(null, s, et.isExact, et.startingScope, et.endingScope, et.increment));
    }
    if (cmd.nameExpr != null) {
        cmd.nameExpr.setReferenced(declaringClause);
    }
    return new Command(cmd.pos, cmd.nameExpr, cmd.label, cmd.check, cmd.overall, cmd.bitwidth, cmd.maxseq, cmd.expects, sc.makeConst(), exactSigs, globalFacts.and(e), parent);
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Func(edu.mit.csail.sdg.ast.Func) 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) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Command(edu.mit.csail.sdg.ast.Command) Clause(edu.mit.csail.sdg.ast.Clause) CommandScope(edu.mit.csail.sdg.ast.CommandScope)

Example 7 with ErrorSyntax

use of edu.mit.csail.sdg.alloy4.ErrorSyntax 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 8 with ErrorSyntax

use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.

the class CompLexer method alloy_num.

private final Symbol alloy_num(String txt) throws Err {
    Pos p = alloy_here(txt);
    int n = 0;
    try {
        txt = txt.replaceAll("_", "");
        n = Integer.parseInt(txt);
    } catch (NumberFormatException ex) {
        throw new ErrorSyntax(p, "The number " + txt + " " + ex);
    }
    return new Symbol(CompSym.NUMBER, p, ExprConstant.Op.NUMBER.make(p, n));
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Pos(edu.mit.csail.sdg.alloy4.Pos) Symbol(java_cup.runtime.Symbol)

Example 9 with ErrorSyntax

use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.

the class CompLexer method alloy_hexnum.

private final Symbol alloy_hexnum(String txt) throws Err {
    Pos p = alloy_here(txt);
    int n = 0;
    try {
        txt = txt.substring(2).replaceAll("_", "");
        n = Integer.parseInt(txt, 16);
    } catch (NumberFormatException ex) {
        throw new ErrorSyntax(p, "The hex number " + txt + " " + ex);
    }
    return new Symbol(CompSym.NUMBER, p, ExprConstant.Op.NUMBER.make(p, n));
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Pos(edu.mit.csail.sdg.alloy4.Pos) Symbol(java_cup.runtime.Symbol)

Example 10 with ErrorSyntax

use of edu.mit.csail.sdg.alloy4.ErrorSyntax in project org.alloytools.alloy by AlloyTools.

the class CompModule method addMacro.

// ============================================================================================================================//
/**
 * Add a MACRO declaration.
 */
void addMacro(Pos p, Pos isPrivate, String n, List<ExprVar> decls, Expr v) throws Err {
    if (!Version.experimental)
        throw new ErrorSyntax(p, "LET declaration is allowed only inside a toplevel paragraph.");
    ConstList<ExprVar> ds = ConstList.make(decls);
    status = 3;
    dup(p, n, false);
    for (int i = 0; i < ds.size(); i++) for (int j = i + 1; j < ds.size(); j++) if (ds.get(i).label.equals(ds.get(j).label))
        throw new ErrorSyntax(ds.get(j).span(), "The parameter name \"" + ds.get(j).label + "\" cannot appear more than once.");
    Macro ans = new Macro(p, isPrivate, this, n, ds, v);
    Macro old = macros.put(n, ans);
    if (old != null) {
        macros.put(n, old);
        throw new ErrorSyntax(p, "You cannot declare more than one macro with the same name \"" + n + "\" in the same file.");
    }
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)35 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)10 Pos (edu.mit.csail.sdg.alloy4.Pos)9 Expr (edu.mit.csail.sdg.ast.Expr)9 Sig (edu.mit.csail.sdg.ast.Sig)9 ExprVar (edu.mit.csail.sdg.ast.ExprVar)7 ArrayList (java.util.ArrayList)7 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)6 Symbol (java_cup.runtime.Symbol)6 Err (edu.mit.csail.sdg.alloy4.Err)5 Func (edu.mit.csail.sdg.ast.Func)5 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)5 LinkedHashMap (java.util.LinkedHashMap)5 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)4 Command (edu.mit.csail.sdg.ast.Command)4 IOException (java.io.IOException)4 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 Env (edu.mit.csail.sdg.alloy4.Env)2 Decl (edu.mit.csail.sdg.ast.Decl)2 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)2