Package edu.mit.csail.sdg.alloy4compiler.ast

Examples of edu.mit.csail.sdg.alloy4compiler.ast.Expr


      }
   }

   /** Constructs the atoms corresponding to the given sig. */
   private void atoms(A4Solution sol, PrimSig s) throws Err {
      Expr sum=Sig.NONE;
      for(PrimSig c:s.children()) { sum=sum.plus(c); atoms(sol, c); }
      A4TupleSet ts = (A4TupleSet) (sol.eval(s.minus(sum))); // This ensures that atoms will be associated with the most specific sig
      for(A4Tuple z: ts) {
         String atom = z.atom(0);
         int i, dollar = atom.lastIndexOf('$');
         try { i = Integer.parseInt(dollar>=0 ? atom.substring(dollar+1) : atom); } catch(NumberFormatException ex) { i = Integer.MAX_VALUE; }
View Full Code Here


            sets.add(set);
            for(A4Tuple tp: (A4TupleSet)(sol.eval(expr.intersect(t)))) {
               atom2sets.get(string2atom.get(tp.atom(0))).add(set);
            }
         } else {
            Expr mask = null;
            List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
            for(int i=0; i<ps.size(); i++) {
               types.add(sig(ps.get(i)));
               if (mask==null) mask=ps.get(i); else mask=mask.product(ps.get(i));
            }
            AlloyRelation rel = makeRel(label, isPrivate, isMeta, types);
            Set<AlloyTuple> ts = new LinkedHashSet<AlloyTuple>();
            for(A4Tuple tp: (A4TupleSet)(sol.eval(expr.intersect(mask)))) {
               AlloyAtom[] atoms = new AlloyAtom[tp.arity()];
View Full Code Here

               if (uu==null || uu.realModule==null || uu.isPrivate) break;
               n = n.substring(i+1);
               mod = uu.realModule;
            }
         }
         Expr match = env.get(name);
         if (match==null) {
            boolean ambiguous = false;
            StringBuilder sb = new StringBuilder();
            for(CompModule m: rootmodule.getAllNameableModules()) {
               Macro mac = m.macros.get(name);
               if (mac==null) continue;
               if (match!=null) ambiguous=true; else match=mac;
               sb.append("\n").append(m.path.length()==0 ? "this" : m.path).append("/").append(name);
            }
            if (ambiguous) return new ExprBad(pos, name, new ErrorType(pos, "There are multiple macros with the same name:"+sb));
         }
         if (match==null) match = rootmodule.globals.get(name);
         if (match!=null) {
            if (match instanceof Macro) return ((Macro)match).changePos(pos);
            match = ExprUnary.Op.NOOP.make(pos, match);
            return ExprChoice.make(pos, asList(match), asList(name));
         }
         Expr th = env.get("this");
         if (th!=null) th = ExprUnary.Op.NOOP.make(pos, th);
         TempList<Expr> ch = new TempList<Expr>();
         TempList<String> re = new TempList<String>();
         Expr ans = rootmodule.populate(ch, re, rootfield, rootsig, rootfunparam, rootfunbody, pos, name, th);
         if (ans!=null) return ans;
         if (ch.size()==0) return new ExprBad(pos, name, hint(pos, name)); else return ExprChoice.make(pos, ch.makeConst(), re.makeConst());
      }
View Full Code Here

      private Expr process(Pos pos, Pos closingBracket, Pos rightPos, List<Expr> choices, List<String> oldReasons, Expr arg) {
         TempList<Expr> list = new TempList<Expr>(choices.size());
         TempList<String> reasons = new TempList<String>(choices.size());
         for(int i=0; i<choices.size(); i++) {
            Expr x=choices.get(i), y=x;
            while(true) {
               if (y instanceof ExprUnary && ((ExprUnary)y).op==ExprUnary.Op.NOOP) y=((ExprUnary)y).sub;
               else if (y instanceof ExprChoice && ((ExprChoice)y).choices.size()==1) y=((ExprChoice)y).choices.get(0);
               else break;
            }
View Full Code Here

         return ExprList.make(x.pos, x.closingBracket, x.op, temp.makeConst());
      }

      /** {@inheritDoc} */
      @Override public Expr visit(ExprITE x) throws Err {
         Expr f = visitThis(x.cond);
         Expr a = visitThis(x.left);
         Expr b = visitThis(x.right);
         return ExprITE.make(x.pos, f, a, b);
      }
View Full Code Here

         return ExprITE.make(x.pos, f, a, b);
      }

      /** {@inheritDoc} */
      @Override public Expr visit(ExprBadJoin x) throws Err {
         Expr left = visitThis(x.left);
         Expr right = visitThis(x.right);
         // If it's a macro invocation, instantiate it
         if (right instanceof Macro) return ((Macro)right).addArg(left).instantiate(this, warns);
         // check to see if it is the special builtin function "Int[]"
         if (left.type().is_int && right.isSame(Sig.SIGINT)) return left.cast2sigint();
         // otherwise, process as regular join or as method call
         left = left.typecheck_as_set();
         if (!left.errors.isEmpty() || !(right instanceof ExprChoice)) return ExprBinary.Op.JOIN.make(x.pos, x.closingBracket, left, right);
         return process(x.pos, x.closingBracket, right.pos, ((ExprChoice)right).choices, ((ExprChoice)right).reasons, left);
      }
View Full Code Here

         return process(x.pos, x.closingBracket, right.pos, ((ExprChoice)right).choices, ((ExprChoice)right).reasons, left);
      }

      /** {@inheritDoc} */
      @Override public Expr visit(ExprBinary x) throws Err {
         Expr left = visitThis(x.left);
         Expr right = visitThis(x.right);
         if (x.op==ExprBinary.Op.JOIN) {
            // If it's a macro invocation, instantiate it
            if (right instanceof Macro) return ((Macro)right).addArg(left).instantiate(this, warns);
            // check to see if it is the special builtin function "Int[]"
            if (left.type().is_int && right.isSame(Sig.SIGINT)) return left.cast2sigint();
            // otherwise, process as regular join or as method call
            left = left.typecheck_as_set();
            if (!left.errors.isEmpty() || !(right instanceof ExprChoice)) return x.op.make(x.pos, x.closingBracket, left, right);
            return process(x.pos, x.closingBracket, right.pos, ((ExprChoice)right).choices, ((ExprChoice)right).reasons, left);
         }
View Full Code Here

         return x.op.make(x.pos, x.closingBracket, left, right);
      }

      /** {@inheritDoc} */
      @Override public Expr visit(ExprLet x) throws Err {
         Expr right = visitThis(x.expr);
         right = right.resolve(right.type(), warns);
         ExprVar left = ExprVar.make(x.var.pos, x.var.label, right.type());
         put(left.label, left);
         Expr sub = visitThis(x.sub);
         remove(left.label);
         return ExprLet.make(x.pos, left, right, sub);
      }
View Full Code Here

      /** {@inheritDoc} */
      @Override public Expr visit(ExprQt x) throws Err {
         TempList<Decl> decls = new TempList<Decl>(x.decls.size());
         boolean isMetaSig=false, isMetaField=false;
         for(Decl d: x.decls) {
            Expr exp = visitThis(d.expr).resolve_as_set(warns);
            if (exp.mult==0 && exp.type().arity()==1) exp = ExprUnary.Op.ONEOF.make(null, exp);
            if (exp.errors.isEmpty()) {
               if (exp.type().isSubtypeOf(rootmodule.metaSig().plus(rootmodule.metaField()).type())) {
                  isMetaSig = exp.type().intersects(rootmodule.metaSig().type());
                  isMetaField = exp.type().intersects(rootmodule.metaField().type());
               }
            }
            // Below is a special case to allow more fine-grained typechecking when we see "all x:field$" or "some x:field$"
            boolean some = (x.op==ExprQt.Op.SOME), compre = (x.op==ExprQt.Op.COMPREHENSION);
            if (x.decls.size()==1 && d.names.size()==1 && isOneOf(exp) && (x.op==ExprQt.Op.ALL || some || compre) && (isMetaSig || isMetaField)) {
               ExprVar v = (ExprVar)(d.names.get(0));
               // Prevent warnings
               List<ErrorWarning> saved = warns;
               warns = null;
               // Now duplicate the body for each possible Meta Atom binding
               Expr answer = null;
               if (isMetaSig) for(PrimSig child: rootmodule.metaSig().children()) if (child.type().intersects(exp.type())) {
                  put(v.label, child);
                  Expr sub = visitThis(x.sub);
                  remove(v.label);
                  if (compre) answer = child.in(exp).and(sub).ite(child, Sig.NONE).plus(answer);
                  else if (some) answer = child.in(exp).and(sub).or(answer);
                  else answer = child.in(exp).implies(sub).and(answer);
               }
               if (isMetaField) for(PrimSig child: rootmodule.metaField().children()) if (child.type().intersects(exp.type())) {
                  put(v.label, child);
                  Expr sub = visitThis(x.sub);
                  remove(v.label);
                  if (compre) answer = child.in(exp).and(sub).ite(child, Sig.NONE).plus(answer);
                  else if (some) answer = child.in(exp).and(sub).or(answer);
                  else answer = child.in(exp).implies(sub).and(answer);
               }
               if (answer==null) answer = (compre ? Sig.NONE : (some ? ExprConstant.FALSE : ExprConstant.TRUE)); else answer = answer.resolve(answer.type(), null);
               // Now, wrap the body in an ExprLet expression to prevent any more warnings by outer code
               ExprVar combinedAnswer = ExprVar.make(Pos.UNKNOWN, "", answer.type());
               Expr returnValue = ExprLet.make(Pos.UNKNOWN, combinedAnswer, answer, combinedAnswer);
               // Restore the "warns" array, then return the answer
               warns = saved;
               return returnValue;
            }
            // Above is a special case to allow more fine-grained typechecking when we see "all x:field$" or "some x:field$"
            TempList<ExprVar> n = new TempList<ExprVar>(d.names.size());
            for(ExprHasName v: d.names) n.add(ExprVar.make(v.pos, v.label, exp.type()));
            Decl dd = new Decl(d.isPrivate, d.disjoint, d.disjoint2, n.makeConst(), exp);
            for(ExprHasName newname: dd.names) put(newname.label, newname);
            decls.add(dd);
         }
         Expr sub = visitThis(x.sub);
         if (x.op==ExprQt.Op.SUM) sub=sub.resolve_as_int(warns); else sub=sub.resolve_as_formula(warns);
         for(Decl d: decls.makeConst()) for(ExprHasName v: d.names) remove(v.label);
         return x.op.make(x.pos, x.closingBracket, decls.makeConst(), sub);
      }
View Full Code Here

         return x.op.make(x.pos, x.closingBracket, decls.makeConst(), sub);
      }

      /** {@inheritDoc} */
      @Override public Expr visit(ExprVar x) throws Err {
         Expr obj = resolve(x.pos, x.label);
         if (obj instanceof Macro) return ((Macro)obj).instantiate(this, warns); else return obj;
      }
View Full Code Here

TOP

Related Classes of edu.mit.csail.sdg.alloy4compiler.ast.Expr

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.