Search in sources :

Example 1 with ExprConstant

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

the class OurSyntaxWidget method getTooltip.

public String getTooltip(MouseEvent event) {
    try {
        int offset = pane.viewToModel(event.getPoint());
        CompModule module = getModule();
        if (module == null)
            return null;
        String text = pane.getText();
        Pos pos = Pos.toPos(text, offset, offset + 1);
        Expr expr = module.find(pos);
        if (expr instanceof ExprBad) {
            return expr.toString();
        }
        if (expr != null) {
            Clause referenced = expr.referenced();
            if (referenced != null) {
                String s = referenced.explain();
                String table = "<html><pre>" + s + "</pre></html>";
                s = table.replaceAll("\n", "<br/>");
                return s;
            } else if (expr instanceof ExprConstant) {
                String token = pos.substring(text);
                if (token != null) {
                    String match = expr.toString();
                    if (!Objects.equals(token, match))
                        return match;
                }
            }
        }
    } catch (Exception e) {
    // e.printStackTrace();
    // ignore compile errors etc.
    }
    return null;
}
Also used : ExprBad(edu.mit.csail.sdg.ast.ExprBad) CompModule(edu.mit.csail.sdg.parser.CompModule) Expr(edu.mit.csail.sdg.ast.Expr) Clause(edu.mit.csail.sdg.ast.Clause) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with ExprConstant

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

the class BoundsComputer method sim.

// ==============================================================================================================//
/**
 * If ex is a simple combination of Relations, then return that combination,
 * else return null.
 */
private Expression sim(Expr ex) {
    while (ex instanceof ExprUnary) {
        ExprUnary u = (ExprUnary) ex;
        if (u.op != ExprUnary.Op.NOOP && u.op != ExprUnary.Op.EXACTLYOF)
            break;
        ex = u.sub;
    }
    if (ex instanceof ExprBinary) {
        ExprBinary b = (ExprBinary) ex;
        if (b.op == ExprBinary.Op.ARROW || b.op == ExprBinary.Op.PLUS || b.op == ExprBinary.Op.JOIN) {
            Expression left = sim(b.left);
            if (left == null)
                return null;
            Expression right = sim(b.right);
            if (right == null)
                return null;
            if (b.op == ExprBinary.Op.ARROW)
                return left.product(right);
            if (b.op == ExprBinary.Op.PLUS)
                return left.union(right);
            else
                return left.join(right);
        }
    }
    if (ex instanceof ExprConstant) {
        switch(((ExprConstant) ex).op) {
            case EMPTYNESS:
                return Expression.NONE;
        }
    }
    if (ex == Sig.NONE)
        return Expression.NONE;
    if (ex == Sig.SIGINT)
        return Expression.INTS;
    if (ex instanceof Sig)
        return sol.a2k((Sig) ex);
    if (ex instanceof Field)
        return sol.a2k((Field) ex);
    return null;
}
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) Field(edu.mit.csail.sdg.ast.Sig.Field) ExprBinary(edu.mit.csail.sdg.ast.ExprBinary) Expression(kodkod.ast.Expression) ExprUnary(edu.mit.csail.sdg.ast.ExprUnary) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant)

Example 3 with ExprConstant

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

the class TranslateAlloyToKodkod method visit.

/* =============================== */
/* Evaluates an ExprBinary node. */
/* =============================== */
/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprBinary x) throws Err {
    Expr a = x.left, b = x.right;
    Expression s, s2, eL, eR;
    IntExpression i;
    Formula f;
    Object objL, objR;
    switch(x.op) {
        case IMPLIES:
            f = cform(a).not().or(cform(b));
            return k2pos(f, x);
        case IN:
            return k2pos(isIn(cset(a), b), x);
        case NOT_IN:
            return k2pos(isIn(cset(a), b).not(), x);
        case LT:
            i = cint(a);
            f = i.lt(cint(b));
            return k2pos(f, x);
        case LTE:
            i = cint(a);
            f = i.lte(cint(b));
            return k2pos(f, x);
        case GT:
            i = cint(a);
            f = i.gt(cint(b));
            return k2pos(f, x);
        case GTE:
            i = cint(a);
            f = i.gte(cint(b));
            return k2pos(f, x);
        case NOT_LT:
            i = cint(a);
            f = i.lt(cint(b)).not();
            return k2pos(f, x);
        case NOT_LTE:
            i = cint(a);
            f = i.lte(cint(b)).not();
            return k2pos(f, x);
        case NOT_GT:
            i = cint(a);
            f = i.gt(cint(b)).not();
            return k2pos(f, x);
        case NOT_GTE:
            i = cint(a);
            f = i.gte(cint(b)).not();
            return k2pos(f, x);
        case AND:
            f = cform(a);
            f = f.and(cform(b));
            return k2pos(f, x);
        case OR:
            f = cform(a);
            f = f.or(cform(b));
            return k2pos(f, x);
        case IFF:
            f = cform(a);
            f = f.iff(cform(b));
            return k2pos(f, x);
        case PLUSPLUS:
            s = cset(a);
            return s.override(cset(b));
        case MUL:
            i = cint(a);
            return i.multiply(cint(b));
        case DIV:
            i = cint(a);
            return i.divide(cint(b));
        case REM:
            i = cint(a);
            return i.modulo(cint(b));
        case SHL:
            i = cint(a);
            return i.shl(cint(b));
        case SHR:
            i = cint(a);
            return i.shr(cint(b));
        case SHA:
            i = cint(a);
            return i.sha(cint(b));
        case PLUS:
            return cset(a).union(cset(b));
        // s = (Expression)obj; return s.union(cset(b));
        case IPLUS:
            return cint(a).plus(cint(b));
        case MINUS:
            // exception)
            if (a instanceof ExprConstant && ((ExprConstant) a).op == ExprConstant.Op.NUMBER && ((ExprConstant) a).num() == 0)
                if (b instanceof ExprConstant && ((ExprConstant) b).op == ExprConstant.Op.NUMBER && ((ExprConstant) b).num() == max + 1)
                    return IntConstant.constant(min);
            return cset(a).difference(cset(b));
        // s=(Expression)obj; return s.difference(cset(b));
        case IMINUS:
            return cint(a).minus(cint(b));
        case INTERSECT:
            s = cset(a);
            return s.intersection(cset(b));
        case ANY_ARROW_SOME:
        case ANY_ARROW_ONE:
        case ANY_ARROW_LONE:
        case SOME_ARROW_ANY:
        case SOME_ARROW_SOME:
        case SOME_ARROW_ONE:
        case SOME_ARROW_LONE:
        case ONE_ARROW_ANY:
        case ONE_ARROW_SOME:
        case ONE_ARROW_ONE:
        case ONE_ARROW_LONE:
        case LONE_ARROW_ANY:
        case LONE_ARROW_SOME:
        case LONE_ARROW_ONE:
        case LONE_ARROW_LONE:
        case ISSEQ_ARROW_LONE:
        case ARROW:
            s = cset(a);
            return s.product(cset(b));
        case JOIN:
            a = a.deNOP();
            s = cset(a);
            s2 = cset(b);
            if (a instanceof Sig && ((Sig) a).isOne != null && s2 instanceof BinaryExpression) {
                BinaryExpression bin = (BinaryExpression) s2;
                if (bin.op() == ExprOperator.PRODUCT && bin.left() == s)
                    return bin.right();
            }
            return s.join(s2);
        case EQUALS:
            objL = visitThis(a);
            objR = visitThis(b);
            eL = toSet(a, objL);
            eR = toSet(b, objR);
            if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
                f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr());
            else
                f = eL.eq(eR);
            return k2pos(f, x);
        case NOT_EQUALS:
            objL = visitThis(a);
            objR = visitThis(b);
            eL = toSet(a, objL);
            eR = toSet(b, objR);
            if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
                f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr()).not();
            else
                f = eL.eq(eR).not();
            return k2pos(f, x);
        case DOMAIN:
            s = cset(a);
            s2 = cset(b);
            for (int j = s2.arity(); j > 1; j--) s = s.product(Expression.UNIV);
            return s.intersection(s2);
        case RANGE:
            s = cset(a);
            s2 = cset(b);
            for (int j = s.arity(); j > 1; j--) s2 = Expression.UNIV.product(s2);
            return s.intersection(s2);
    }
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprBinary.accept()");
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) BinaryExpression(kodkod.ast.BinaryExpression) IntToExprCast(kodkod.ast.IntToExprCast) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) IntExpression(kodkod.ast.IntExpression) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant)

Example 4 with ExprConstant

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

the class CompModule method resolveFacts.

/**
 * Each fact name now points to a typechecked Expr rather than an untypechecked
 * Exp; we'll also add the sig appended facts.
 */
private JoinableList<Err> resolveFacts(CompModule res, A4Reporter rep, JoinableList<Err> errors, List<ErrorWarning> warns) throws Err {
    Context cx = new Context(this, warns);
    for (int i = 0; i < facts.size(); i++) {
        String name = facts.get(i).a;
        Expr expr = facts.get(i).b;
        Expr checked = cx.check(expr);
        expr = checked.resolve_as_formula(warns);
        if (expr.errors.isEmpty()) {
            facts.set(i, new Pair<String, Expr>(name, expr));
            rep.typecheck("Fact " + name + ": " + expr.type() + "\n");
        } else
            errors = errors.make(expr.errors);
    }
    for (Sig s : sigs.values()) {
        Expr f = res.old2appendedfacts.get(res.new2old.get(s));
        if (f == null)
            continue;
        if (f instanceof ExprConstant && ((ExprConstant) f).op == ExprConstant.Op.TRUE)
            continue;
        Expr formula;
        cx.rootsig = s;
        if (s.isOne == null) {
            cx.put("this", s.decl.get());
            formula = cx.check(f).resolve_as_formula(warns);
        } else {
            cx.put("this", s);
            formula = cx.check(f).resolve_as_formula(warns);
        }
        cx.remove("this");
        if (formula.errors.size() > 0)
            errors = errors.make(formula.errors);
        else {
            s.addFact(formula);
            rep.typecheck("Fact " + s + "$fact: " + formula.type() + "\n");
        }
    }
    return errors;
}
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) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant)

Aggregations

ExprConstant (edu.mit.csail.sdg.ast.ExprConstant)4 Expr (edu.mit.csail.sdg.ast.Expr)3 Sig (edu.mit.csail.sdg.ast.Sig)3 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 Expression (kodkod.ast.Expression)2 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)1 Clause (edu.mit.csail.sdg.ast.Clause)1 ExprBad (edu.mit.csail.sdg.ast.ExprBad)1 ExprBinary (edu.mit.csail.sdg.ast.ExprBinary)1 ExprUnary (edu.mit.csail.sdg.ast.ExprUnary)1 Field (edu.mit.csail.sdg.ast.Sig.Field)1 CompModule (edu.mit.csail.sdg.parser.CompModule)1 BadLocationException (javax.swing.text.BadLocationException)1 BinaryExpression (kodkod.ast.BinaryExpression)1 Formula (kodkod.ast.Formula)1 IntExpression (kodkod.ast.IntExpression)1 IntToExprCast (kodkod.ast.IntToExprCast)1 QuantifiedFormula (kodkod.ast.QuantifiedFormula)1