5 public class RelationFunctionExpr extends Expr {
7 // #WHAT I WAS DOING: about to define relationfunctionexpr thich should take a expr, relation and rule and generated
8 // the functional value or "maybe" if not there!
11 RelationDescriptor relation;
14 public RelationFunctionExpr(Expr expr, RelationDescriptor relation, Rule rule) {
16 this.relation = relation;
20 public boolean equals(Map remap, Expr e) {
21 throw new Error("UNIMPLEMENTED!!!");
24 public Descriptor getDescriptor() {
28 public RelationDescriptor getRelation() {
32 public Set getInversedRelations() {
33 return expr.getInversedRelations();
36 public Set getRequiredDescriptors() {
37 Set v = expr.getRequiredDescriptors();
42 public boolean usesDescriptor(Descriptor rd) {
46 return expr.usesDescriptor(rd);
49 public void generate(CodeWriter cr, VarDescriptor dest) {
51 String destname = dest.getSafeSymbol();
52 cr.outputline("int " + destname + ";");
54 // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we
55 // set the destination in the guard ... otherwise maybe!
57 VarDescriptor domain = VarDescriptor.makeNew("domain");
58 expr.generate(cr, domain);
60 cr.pushSymbolTable(rule.getSymbolTable());
63 // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
64 SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
65 VarDescriptor rulebinding = sq.getVar();
66 String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
68 // this is to be safe about name overlap because int t = t; sets t to 0!
69 cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
70 cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
73 cr.outputline("// about to inbed relational function");
75 rule.getGuardExpr().prettyPrint(cr);
78 /* now we have to generate the guard test */
79 VarDescriptor guardval = VarDescriptor.makeNew();
80 rule.getGuardExpr().generate(cr, guardval);
82 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
85 /* now we have to generate the inclusion code */
86 RelationInclusion ri = (RelationInclusion) rule.getInclusion();
88 // basically, destname = righthandside<r, r.field>
89 VarDescriptor tempdest = VarDescriptor.makeNew("tempdest");
90 Expr rhs = ri.getRightExpr();
91 rhs.generate(cr, tempdest);
93 cr.outputline(destname + " = " + tempdest.getSafeSymbol() + ";");
96 cr.outputline("else");
99 // three valued logic. if the relation (which is a partial function)
100 // fails its guard, then we have a "maybe" condition, which must
103 cr.outputline("maybe = 1;");
111 public void prettyPrint(PrettyPrinter pp) {
112 expr.prettyPrint(pp);
114 pp.output(relation.getSafeSymbol());
117 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
118 throw new IRException();