5 public class SumExpr extends Expr {
11 public SumExpr(SetDescriptor sd, RelationDescriptor rd) {
12 if (sd == null||rd==null) {
13 throw new NullPointerException();
19 public String name() {
20 return "sum("+sd.toString()+"."+rd.toString()+")";
23 public boolean equals(Map remap, Expr e) {
24 if (e==null||!(e instanceof SumExpr))
26 SumExpr se=(SumExpr)e;
27 return (se.sd==sd)&&(se.rd==rd);
30 public boolean usesDescriptor(Descriptor d) {
31 return (sd==d)||(rd==d);
34 public Set useDescriptor(Descriptor d) {
35 HashSet newset=new HashSet();
41 public Descriptor getDescriptor() {
42 throw new Error("Sum shouldn't appear on left hand side!");
45 public boolean inverted() {
49 public Set getRequiredDescriptors() {
50 HashSet v=new HashSet();
56 public void generate(CodeWriter writer, VarDescriptor dest) {
57 writer.outputline("int "+dest.getSafeSymbol()+"=0;");
59 VarDescriptor itvd=VarDescriptor.makeNew("iterator");
60 writer.outputline("struct SimpleIterator "+itvd.getSafeSymbol()+";");
61 writer.outputline("SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+itvd.getSafeSymbol()+");");
62 writer.outputline("while (hasNext(&"+itvd.getSafeSymbol()+")) {");
63 VarDescriptor keyvd=VarDescriptor.makeNew("key");
64 writer.outputline("int "+keyvd.getSafeSymbol()+"=next(&"+itvd.getSafeSymbol()+");");
65 VarDescriptor tmpvar=VarDescriptor.makeNew("tmp");
66 writer.outputline("int "+tmpvar.getSafeSymbol()+";");
67 writer.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash, "+keyvd.getSafeSymbol()+", &"+tmpvar.getSafeSymbol()+");");
68 writer.outputline(dest.getSafeSymbol()+"+="+tmpvar.getSafeSymbol()+";");
69 writer.outputline("}");
72 public void prettyPrint(PrettyPrinter pp) {
74 pp.output(sd.toString());
76 pp.output(rd.toString());
80 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
81 this.td = ReservedTypeDescriptor.INT;
85 public Set getInversedRelations() {