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.addDeclaration("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.addDeclaration("int", tempvar);
70 cr.outputline(tempvar + " = " + domain.getSafeSymbol() + ";");
71 cr.addDeclaration("int", rulebinding.getSafeSymbol());
72 cr.outputline(rulebinding.getSafeSymbol() + " = " + tempvar + ";");
75 cr.outputline("/* about to inbed relational function*/");
77 rule.getGuardExpr().prettyPrint(cr);
80 /* now we have to generate the guard test */
81 VarDescriptor guardval = VarDescriptor.makeNew();
82 rule.getGuardExpr().generate(cr, guardval);
84 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
87 /* now we have to generate the inclusion code */
88 RelationInclusion ri = (RelationInclusion) rule.getInclusion();
90 // basically, destname = righthandside<r, r.field>
91 VarDescriptor tempdest = VarDescriptor.makeNew("tempdest");
92 Expr rhs = ri.getRightExpr();
93 rhs.generate(cr, tempdest);
95 cr.outputline(destname + " = " + tempdest.getSafeSymbol() + ";");
98 cr.outputline("else");
101 // three valued logic. if the relation (which is a partial function)
102 // fails its guard, then we have a "maybe" condition, which must
105 cr.outputline("maybe = 1;");
113 public void prettyPrint(PrettyPrinter pp) {
114 expr.prettyPrint(pp);
116 pp.output(relation.getSafeSymbol());
119 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
120 throw new IRException();