Adding support for sum expressions.
authorbdemsky <bdemsky>
Wed, 27 Oct 2004 19:04:55 +0000 (19:04 +0000)
committerbdemsky <bdemsky>
Wed, 27 Oct 2004 19:04:55 +0000 (19:04 +0000)
Repair/RepairCompiler/MCC/CDL.cup
Repair/RepairCompiler/MCC/IR/SemanticChecker.java
Repair/RepairCompiler/MCC/IR/SumExpr.java [new file with mode: 0755]
Repair/RepairCompiler/MCC/Lexer.lex
Repair/RepairCompiler/MCC/MDL.cup
Repair/RepairCompiler/MCC/SDL.cup
Repair/RepairCompiler/MCC/TDL.cup

index 12021e6d0fdfc587145de676907d87b336413a46..f491a6e556567f337e575b0b0f162a057acb4c5c 100755 (executable)
@@ -132,6 +132,7 @@ parser code {:
     terminal SUB; 
     terminal MULT; 
     terminal DIV;
+    terminal SUM;
 
     terminal NOT;
     terminal LT;
@@ -141,7 +142,6 @@ parser code {:
     terminal EQ;
     terminal NE;
 
-
     terminal FORALL;
     terminal IN;
     terminal INTEST;
@@ -478,8 +478,17 @@ setexpr ::=
        ;
        
 expr ::=
-       
-       ID:var
+
+       SUM OPENPAREN ID:set DOT ID:relation CLOSEPAREN
+       {:
+       debugMessage(PRODSTRING);
+       ParseNode expr = new ParseNode("sumexpr", parser.curLine(3));
+       expr.addChild("dot").addChild("set", parser.curLine(3)).addChild(set);
+       expr.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
+       RESULT = expr;
+       :}
+
+       | ID:var
        {:
        debugMessage(PRODSTRING);
        ParseNode expr = new ParseNode("expr", parser.curLine(1));      
index 2e54c87778d1ba03de910a10e0581ec949217d66..734f6d05365c85183a06d175649127377ed1ccf2 100755 (executable)
@@ -1265,6 +1265,8 @@ public class SemanticChecker {
             // we've got a variable reference... we'll have to scope check it later
             // when we are completely done... there are also some issues of cyclic definitions
             return new VarExpr(pn.getChild("var").getTerminal());
+        } else if (pn.getChild("sumexpr") != null) {
+            return parse_sum(pn.getChild("sumexpr"));
         } else if (pn.getChild("literal") != null) {
             return parse_literal(pn.getChild("literal"));
         } else if (pn.getChild("operator") != null) {
@@ -1406,6 +1408,25 @@ public class SemanticChecker {
         return new SizeofExpr(setexpr);
     }
 
+    private SumExpr parse_sum(ParseNode pn) {
+        if (!precheck(pn, "sumexpr")) {
+            return null;
+        }
+        String setname = pn.getChild("set").getTerminal();
+        assert setname != null;
+        SetDescriptor sd = lookupSet(setname);
+
+        if (sd == null) {
+            er.report(pn, "Unknown or undefined set '" + setname + "'");
+            return null;
+        }
+
+        RelationDescriptor rd = lookupRelation(pn.getChild("dot").getChild("relation").getTerminal());
+        rd.addUsage(RelationDescriptor.IMAGE);
+
+        return new SumExpr(sd,rd);
+    }
+
     private CastExpr parse_cast(ParseNode pn) {
         if (!precheck(pn, "cast")) {
             return null;
diff --git a/Repair/RepairCompiler/MCC/IR/SumExpr.java b/Repair/RepairCompiler/MCC/IR/SumExpr.java
new file mode 100755 (executable)
index 0000000..a03338e
--- /dev/null
@@ -0,0 +1,77 @@
+package MCC.IR;
+
+import java.util.*;
+
+public class SumExpr extends Expr {
+
+    SetDescriptor sd;
+    RelationDescriptor rd;
+
+
+    public SumExpr(SetDescriptor sd, RelationDescriptor rd) {
+        if (sd == null||rd==null) {
+            throw new NullPointerException();
+        }
+        this.sd=sd;
+        this.rd=rd;
+    }
+
+    public String name() {
+       return "sum("+sd.getSafeSymbol()+"."+rd.getSafeSymbol()+")";
+    }
+
+    public boolean equals(Map remap, Expr e) {
+       if (e==null||!(e instanceof SumExpr))
+           return false;
+       SumExpr se=(SumExpr)e;
+       return (se.sd==sd)&&(se.rd==rd);
+    }
+
+    public boolean usesDescriptor(Descriptor d) {
+        return (sd==d)||(rd==d);
+    }
+
+    public Set useDescriptor(Descriptor d) {
+        HashSet newset=new HashSet();
+        if ((d==sd)||(d==rd))
+            newset.add(this);
+        return newset;
+    }
+
+    public Descriptor getDescriptor() {
+        throw new Error();
+    }
+
+    public boolean inverted() {
+       return false;
+    }
+
+    public Set getRequiredDescriptors() {
+        HashSet v=new HashSet();
+        v.add(sd);
+        v.add(rd);
+        return v;
+    }
+
+    public void generate(CodeWriter writer, VarDescriptor dest) {
+        throw new Error();
+    }
+
+    public void prettyPrint(PrettyPrinter pp) {
+        pp.output("sum(");
+        pp.output(sd.getSafeSymbol());
+        pp.output(".");
+        pp.output(rd.getSafeSymbol());
+        pp.output(")");
+    }
+
+    public TypeDescriptor typecheck(SemanticAnalyzer sa) {
+        this.td = ReservedTypeDescriptor.INT;
+        return this.td;
+    }
+
+    public Set getInversedRelations() {
+        return new HashSet();
+    }
+
+}
index ed6f239116dea1aafa0a1b99baa6df03a8a628d8..0664e0be013c324f7196b6bc574a3925ed23aab2 100755 (executable)
@@ -29,19 +29,20 @@ CHAR=(\\\"|\\\'|\\\\|\\t|\\n|[\x20-\x21\x23-\x26\x28-\x5B\x5D-\x7E])
 %%
 
 <YYINITIAL> {WHITESPACE}               {}
-<YYINITIAL> \n                         { LineCount.addLineBreak(yychar+1); } 
+<YYINITIAL> \n                         { LineCount.addLineBreak(yychar+1); }
 
-<YYINITIAL> "{"                                { return tok(Sym.OPENBRACE, yytext()); } 
-<YYINITIAL> "}"                                { return tok(Sym.CLOSEBRACE, yytext()); } 
-<YYINITIAL> "("                                { return tok(Sym.OPENPAREN, yytext()); } 
-<YYINITIAL> ")"                                { return tok(Sym.CLOSEPAREN, yytext()); } 
-<YYINITIAL> "["                                { return tok(Sym.OPENBRACKET, yytext()); } 
-<YYINITIAL> "]"                                { return tok(Sym.CLOSEBRACKET, yytext()); } 
+<YYINITIAL> "{"                                { return tok(Sym.OPENBRACE, yytext()); }
+<YYINITIAL> "}"                                { return tok(Sym.CLOSEBRACE, yytext()); }
+<YYINITIAL> "("                                { return tok(Sym.OPENPAREN, yytext()); }
+<YYINITIAL> ")"                                { return tok(Sym.CLOSEPAREN, yytext()); }
+<YYINITIAL> "["                                { return tok(Sym.OPENBRACKET, yytext()); }
+<YYINITIAL> "]"                                { return tok(Sym.CLOSEBRACKET, yytext()); }
 
 <YYINITIAL> "+"                                { return tok(Sym.ADD, yytext()); }
 <YYINITIAL> "-"                                { return tok(Sym.SUB, yytext()); }
 <YYINITIAL> "*"                                { return tok(Sym.MULT, yytext()); }
 <YYINITIAL> "/"                                { return tok(Sym.DIV, yytext()); }
+<YYINITIAL> sum                                { return tok(Sym.SUM, yytext()); }
 
 
 <YYINITIAL> "<"                                { return tok(Sym.LT, yytext()); }
@@ -56,11 +57,11 @@ CHAR=(\\\"|\\\'|\\\\|\\t|\\n|[\x20-\x21\x23-\x26\x28-\x5B\x5D-\x7E])
 <YYINITIAL> in\?                        { return tok(Sym.INTEST, yytext()); }
 <YYINITIAL> in                          { return tok(Sym.IN, yytext()); }
 
-<YYINITIAL> ","                                { return tok(Sym.COMMA, yytext()); } 
+<YYINITIAL> ","                                { return tok(Sym.COMMA, yytext()); }
 <YYINITIAL> sizeof                      { return tok(Sym.SIZEOF, yytext()); }
 
-<YYINITIAL> ".~"                       { return tok(Sym.DOTINV, yytext()); } 
-<YYINITIAL> "."                                { return tok(Sym.DOT, yytext()); } 
+<YYINITIAL> ".~"                       { return tok(Sym.DOTINV, yytext()); }
+<YYINITIAL> "."                                { return tok(Sym.DOT, yytext()); }
 
 <YYINITIAL> "and"                      { return tok(Sym.AND, yytext()); }
 <YYINITIAL> "or"                       { return tok(Sym.OR, yytext()); }
index c25e436d398a1eec03e0ab72a20fd32f5095774a..0ba43a71ad934cfe86fac970311e46cbc02a20f9 100755 (executable)
@@ -139,6 +139,7 @@ parser code {:
     terminal SUB; 
     terminal MULT; 
     terminal DIV;
+    terminal SUM;
 
     terminal NOT;
     terminal LT;
index be328fec4ae7f284858b6429a3ca016c2c0dbb39..454717108727fa5f02a0b5660169ea27667fd081 100755 (executable)
@@ -122,6 +122,7 @@ parser code {:
     terminal SUB; 
     terminal MULT; 
     terminal DIV;
+    terminal SUM;
 
     terminal NOT;
     terminal LT;
index ee4ba5988cbd1e63976490ff6ee408c027b786c4..c905958facbef2b5e6538b65a9098587e489728d 100755 (executable)
@@ -120,6 +120,7 @@ parser code {:
     terminal SUB; 
     terminal MULT; 
     terminal DIV;
+    terminal SUM;
 
     terminal NOT;
     terminal LT;