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 Descriptor getDescriptor() {
24 public RelationDescriptor getRelation() {
28 public Set getInversedRelations() {
29 return expr.getInversedRelations();
32 public Set getRequiredDescriptors() {
33 Set v = expr.getRequiredDescriptors();
38 public boolean usesDescriptor(RelationDescriptor rd) {
42 return expr.usesDescriptor(rd);
45 public void generate(CodeWriter cr, VarDescriptor dest) {
47 String destname = dest.getSafeSymbol();
48 cr.outputline("int " + destname + ";");
50 // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we
51 // set the destination in the guard ... otherwise maybe!
53 VarDescriptor domain = VarDescriptor.makeNew("domain");
54 expr.generate(cr, domain);
56 cr.pushSymbolTable(rule.getSymbolTable());
59 // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
60 SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
61 VarDescriptor rulebinding = sq.getVar();
62 String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
64 // this is to be safe about name overlap because int t = t; sets t to 0!
65 cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
66 cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
69 cr.outputline("// about to inbed relational function");
71 rule.getGuardExpr().prettyPrint(cr);
74 /* now we have to generate the guard test */
75 VarDescriptor guardval = VarDescriptor.makeNew();
76 rule.getGuardExpr().generate(cr, guardval);
78 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
81 /* now we have to generate the inclusion code */
82 RelationInclusion ri = (RelationInclusion) rule.getInclusion();
84 // basically, destname = righthandside<r, r.field>
85 VarDescriptor tempdest = VarDescriptor.makeNew("tempdest");
86 Expr rhs = ri.getRightExpr();
87 rhs.generate(cr, tempdest);
89 cr.outputline(destname + " = " + tempdest.getSafeSymbol() + ";");
92 cr.outputline("else");
95 // three valued logic. if the relation (which is a partial function)
96 // fails its guard, then we have a "maybe" condition, which must
99 cr.outputline("maybe = 1;");
107 public void prettyPrint(PrettyPrinter pp) {
108 expr.prettyPrint(pp);
110 pp.output(relation.getSafeSymbol());
113 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
114 throw new IRException();