Exemple #1
0
        private void GenerateCasePattern(int line, Formal formal, out CasePattern cp)
        {
            Contract.Requires(formal != null);
            formal = new Formal(formal.tok, formal.Name, formal.Type, formal.InParam, formal.IsGhost);

            cp = new CasePattern(CreateToken(formal.Name, line, 0),
                                 new BoundVar(CreateToken(formal.Name, line, 0), formal.Name, new InferredTypeProxy()));
        }
Exemple #2
0
 public virtual CasePattern CloneCasePattern(CasePattern pat)
 {
     Contract.Requires(pat != null);
       if (pat.Var != null) {
     return new CasePattern(pat.tok, CloneBoundVar(pat.Var));
       } else if (pat.Arguments == null) {
     return new CasePattern(pat.tok, pat.Id, null);
       } else {
     return new CasePattern(pat.tok, pat.Id, pat.Arguments.ConvertAll(CloneCasePattern));
       }
 }
Exemple #3
0
        private CasePattern GenerateCasePattern(int line, Formal formal)
        {
            Contract.Requires(formal != null);

/*      var name = PopCaseName();
 *    if (name == null) name = formal.Name;
 */
            formal = new Formal(formal.tok, formal.Name, formal.Type, formal.InParam, formal.IsGhost);
            CasePattern cp = new CasePattern(new Token(line, 0)
            {
                val = formal.Name
            },
                                             new BoundVar(new Token(line, 0)
            {
                val = formal.Name
            }, formal.Name, new InferredTypeProxy()));

            return(cp);
        }
Exemple #4
0
        private CasePattern GenerateCasePattern(int line, Formal formal)
        {
            Contract.Requires(formal != null);
            var name = formal.Name;

            if (_nameVars != null && _nameVars.Count != 0)
            {
                name = _nameVars[0];
                _nameVars.Remove(_nameVars[0]);
            }

            formal = new Formal(formal.tok, name, formal.Type, formal.InParam, formal.IsGhost);
            CasePattern cp = new CasePattern(new Token(line, 0)
            {
                val = formal.Name
            },
                                             new BoundVar(new Token(line, 0)
            {
                val = formal.Name
            }, formal.Name, new InferredTypeProxy()));

            return(cp);
        }
Exemple #5
0
 int TrCasePattern(CasePattern pat, string rhsString, Type bodyType, TextWriter wr) {
   Contract.Requires(pat != null);
   Contract.Requires(rhsString != null);
   int c = 0;
   if (pat.Var != null) {
     var bv = pat.Var;
     if (!bv.IsGhost) {
       wr.Write("Dafny.Helpers.Let<" + TypeName(bv.Type, wr) + "," + TypeName(bodyType, wr) + ">");
       wr.Write("(" + rhsString + ", @" + bv.CompileName + " => ");
       c++;
     }
   } else if (pat.Arguments != null) {
     var ctor = pat.Ctor;
     Contract.Assert(ctor != null);  // follows from successful resolution
     Contract.Assert(pat.Arguments.Count == ctor.Formals.Count);  // follows from successful resolution
     var k = 0;  // number of non-ghost formals processed
     for (int i = 0; i < pat.Arguments.Count; i++) {
       var arg = pat.Arguments[i];
       var formal = ctor.Formals[i];
       if (formal.IsGhost) {
         // nothing to compile, but do a sanity check
         Contract.Assert(!Contract.Exists(arg.Vars, bv => !bv.IsGhost));
       } else {
         c += TrCasePattern(arg, string.Format("(({0})({1})._D).@{2}", DtCtorName(ctor, ((DatatypeValue)pat.Expr).InferredTypeArgs, wr), rhsString, FormalName(formal, k)), bodyType, wr);
         k++;
       }
     }
   }
   return c;
 }
Exemple #6
0
    void TrCasePatternOpt(CasePattern pat, Expression rhs, string rhs_string, int indent, TextWriter wr, bool inLetExprBody) {
      Contract.Requires(pat != null);
      Contract.Requires(pat.Var != null || rhs != null);
      if (pat.Var != null) {
        // The trivial Dafny "pattern" expression
        //    var x := G
        // is translated into C# as:
        // var x := G;
        var bv = pat.Var;
        if (!bv.IsGhost) {
          Indent(indent, wr);
          wr.Write("{0} {1} = ", TypeName(bv.Type, wr), "@" + bv.CompileName);
          if (rhs != null) {
            TrExpr(rhs, wr, inLetExprBody);
          } else {
            wr.Write(rhs_string);
          }
          wr.Write(";\n");
        }
      } else if (pat.Arguments != null) {
        // The Dafny "pattern" expression
        //    var Pattern(x,y) := G
        // is translated into C# as:
        // var tmp := G;
        // var x := dtorX(tmp);
        // var y := dtorY(tmp);
        var ctor = pat.Ctor;
        Contract.Assert(ctor != null);  // follows from successful resolution
        Contract.Assert(pat.Arguments.Count == ctor.Formals.Count);  // follows from successful resolution

        // Create the temporary variable to hold G
        var tmp_name = idGenerator.FreshId("_let_tmp_rhs");
        Indent(indent, wr);
        wr.Write("{0} {1} = ", TypeName(rhs.Type, wr), tmp_name);
        TrExpr(rhs, wr, inLetExprBody);
        wr.WriteLine(";");

        var k = 0;  // number of non-ghost formals processed
        for (int i = 0; i < pat.Arguments.Count; i++) {
          var arg = pat.Arguments[i];
          var formal = ctor.Formals[i];
          if (formal.IsGhost) {
            // nothing to compile, but do a sanity check
            Contract.Assert(!Contract.Exists(arg.Vars, bv => !bv.IsGhost));
          } else {            
            TrCasePatternOpt(arg, null, string.Format("(({0})({1})._D).@{2}", DtCtorName(ctor, ((DatatypeValue)pat.Expr).InferredTypeArgs, wr), tmp_name, FormalName(formal, k)), indent, wr, inLetExprBody);
            k++;
          }
        }
      }
    }
Exemple #7
0
 void TrCasePatternOpt(CasePattern pat, Expression rhs, string rhs_string, bool inLetExprBody)
 {
     Contract.Requires(pat != null);
       Contract.Requires(pat.Var != null || rhs != null);
       WriteEAbort("BUGBUG TrCasePatternOpt");
       // bugbug: implement
 }
Exemple #8
0
        int TrCasePattern(CasePattern pat, string rhsString, Type bodyType)
        {
            Contract.Requires(pat != null);
              Contract.Requires(rhsString != null);

              WriteEAbort("BUGBUG TrCasePattern"); // bugbug: implement

              return 0; // bugbug
        }
Exemple #9
0
        void CasePattern(out CasePattern pat)
        {
            IToken id;  List<CasePattern> arguments;
            BoundVar bv;
            pat = null;

            if (IsIdentParen()) {
            Ident(out id);
            Expect(50);
            arguments = new List<CasePattern>();
            if (la.kind == 1 || la.kind == 50) {
                CasePattern(out pat);
                arguments.Add(pat);
                while (la.kind == 22) {
                    Get();
                    CasePattern(out pat);
                    arguments.Add(pat);
                }
            }
            Expect(51);
            pat = new CasePattern(id, id.val, arguments);
            } else if (la.kind == 50) {
            Get();
            id = t;
            arguments = new List<CasePattern>();

            if (la.kind == 1 || la.kind == 50) {
                CasePattern(out pat);
                arguments.Add(pat);
                while (la.kind == 22) {
                    Get();
                    CasePattern(out pat);
                    arguments.Add(pat);
                }
            }
            Expect(51);
            theBuiltIns.TupleType(id, arguments.Count, true); // make sure the tuple type exists
            string ctor = BuiltIns.TupleTypeCtorName;  //use the TupleTypeCtors
            pat = new CasePattern(id, ctor, arguments);

            } else if (la.kind == 1) {
            IdentTypeOptional(out bv);
            pat = new CasePattern(bv.tok, bv);

            } else SynErr(208);
            if (pat == null) {
             pat = new CasePattern(t, "_ParseError", new List<CasePattern>());
            }
        }
Exemple #10
0
 void PrintCasePattern(CasePattern pat) {
   Contract.Requires(pat != null);
   var v = pat.Var;
   if (v != null) {
     wr.Write(v.DisplayName);
     if (v.Type is NonProxyType || DafnyOptions.O.DafnyPrintResolvedFile != null) {
       PrintType(": ", v.Type);
     }
   } else {
     wr.Write(pat.Id);
     if (pat.Arguments != null) {
       wr.Write("(");
       var sep = "";
       foreach (var arg in pat.Arguments) {
         wr.Write(sep);
         PrintCasePattern(arg);
         sep = ", ";
       }
       wr.Write(")");
     }
   }
 }
Exemple #11
0
 public override CasePattern CloneCasePattern(CasePattern pat) {
   if (pat.Var != null) {
     var newPat = new CasePattern(pat.tok, CloneBoundVar(pat.Var));
     newPat.AssembleExpr(null);
     return newPat;
   } else {
     var newArgs = pat.Arguments == null ? null : pat.Arguments.ConvertAll(CloneCasePattern);
     var patE = (DatatypeValue)pat.Expr;
     var newPat = new CasePattern(pat.tok, pat.Id, newArgs);
     newPat.Ctor = pat.Ctor;
     newPat.AssembleExpr(patE.InferredTypeArgs.ConvertAll(CloneType));
     return newPat;
   }
 }
Exemple #12
0
    private CasePattern GenerateCasePattern(int line, Formal formal) {
      Contract.Requires(formal != null);
/*      var name = PopCaseName();
      if (name == null) name = formal.Name; 
 */
      formal = new Formal(formal.tok, formal.Name, formal.Type, formal.InParam, formal.IsGhost);
      CasePattern cp = new CasePattern(new Token(line, 0) { val = formal.Name },
        new BoundVar(new Token(line, 0) { val = formal.Name }, formal.Name, new InferredTypeProxy()));
      return cp;
    }
Exemple #13
0
	void VarDeclStatement(out Statement/*!*/ s) {
		IToken x = null, assignTok = null;  bool isGhost = false;
		LocalVariable d;
		AssignmentRhs r;
		List<LocalVariable> lhss = new List<LocalVariable>();
		List<AssignmentRhs> rhss = new List<AssignmentRhs>();
		IToken suchThatAssume = null;
		Expression suchThat = null;
		Attributes attrs = null;
		IToken endTok;
		s = dummyStmt;
		
		if (la.kind == 65) {
			Get();
			isGhost = true;  x = t; 
		}
		Expect(61);
		if (!isGhost) { x = t; } 
		if (la.kind == 1 || la.kind == 46) {
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			LocalIdentTypeOptional(out d, isGhost);
			lhss.Add(d); d.Attributes = attrs; attrs = null; 
			while (la.kind == 22) {
				Get();
				while (la.kind == 46) {
					Attribute(ref attrs);
				}
				LocalIdentTypeOptional(out d, isGhost);
				lhss.Add(d); d.Attributes = attrs; attrs = null; 
			}
			if (la.kind == 25 || la.kind == 46 || la.kind == 104) {
				if (la.kind == 104) {
					Get();
					assignTok = t; 
					Rhs(out r);
					rhss.Add(r); 
					while (la.kind == 22) {
						Get();
						Rhs(out r);
						rhss.Add(r); 
					}
				} else {
					while (la.kind == 46) {
						Attribute(ref attrs);
					}
					Expect(25);
					assignTok = t; 
					if (la.kind == _assume) {
						Expect(31);
						suchThatAssume = t; 
					}
					Expression(out suchThat, false, true);
				}
			}
			while (!(la.kind == 0 || la.kind == 28)) {SynErr(198); Get();}
			Expect(28);
			endTok = t; 
			ConcreteUpdateStatement update;
			if (suchThat != null) {
			 var ies = new List<Expression>();
			 foreach (var lhs in lhss) {
			   ies.Add(new IdentifierExpr(lhs.Tok, lhs.Name));
			 }
			 update = new AssignSuchThatStmt(assignTok, endTok, ies, suchThat, suchThatAssume, attrs);
			} else if (rhss.Count == 0) {
			 update = null;
			} else {
			 var ies = new List<Expression>();
			 foreach (var lhs in lhss) {
			   ies.Add(new AutoGhostIdentifierExpr(lhs.Tok, lhs.Name));
			 }
			 update = new UpdateStmt(assignTok, endTok, ies, rhss);
			}
			s = new VarDeclStmt(x, endTok, lhss, update);
			
		} else if (la.kind == 50) {
			Get();
			var letLHSs = new List<CasePattern>();
			var letRHSs = new List<Expression>();
			List<CasePattern> arguments = new List<CasePattern>();
			CasePattern pat;
			Expression e = dummyExpr;
			IToken id = t;
			
			if (la.kind == 1 || la.kind == 50) {
				CasePattern(out pat);
				arguments.Add(pat); 
				while (la.kind == 22) {
					Get();
					CasePattern(out pat);
					arguments.Add(pat); 
				}
			}
			Expect(51);
			theBuiltIns.TupleType(id, arguments.Count, true); // make sure the tuple type exists
			string ctor = BuiltIns.TupleTypeCtorName;  //use the TupleTypeCtors
			pat = new CasePattern(id, ctor, arguments); 
			if (isGhost) { pat.Vars.Iter(bv => bv.IsGhost = true); }
			letLHSs.Add(pat);
			
			if (la.kind == 104) {
				Get();
			} else if (la.kind == 25 || la.kind == 46) {
				while (la.kind == 46) {
					Attribute(ref attrs);
				}
				Expect(25);
				SemErr(pat.tok, "LHS of assign-such-that expression must be variables, not general patterns"); 
			} else SynErr(199);
			Expression(out e, false, true);
			letRHSs.Add(e); 
			Expect(28);
			s = new LetStmt(e.tok, e.tok, letLHSs, letRHSs); 
		} else SynErr(200);
	}