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);
}
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;
}
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));
}
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));
}
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.");
}
}
Aggregations