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 RelationDescriptor getRelation() {
24 public Set getInversedRelations() {
25 return expr.getInversedRelations();
28 public Set getRequiredDescriptors() {
29 Set v = expr.getRequiredDescriptors();
34 public void generate(CodeWriter cr, VarDescriptor dest) {
36 String destname = dest.getSafeSymbol();
37 cr.outputline("int " + destname + ";");
39 // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we
40 // set the destination in the guard ... otherwise maybe!
42 VarDescriptor domain = VarDescriptor.makeNew("domain");
43 expr.generate(cr, domain);
45 cr.pushSymbolTable(rule.getSymbolTable());
48 // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
49 SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
50 VarDescriptor rulebinding = sq.getVar();
51 String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
53 // this is to be safe about name overlap because int t = t; sets t to 0!
54 cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
55 cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
58 cr.outputline("// about to inbed relational function");
60 rule.getGuardExpr().prettyPrint(cr);
63 /* now we have to generate the guard test */
64 VarDescriptor guardval = VarDescriptor.makeNew();
65 rule.getGuardExpr().generate(cr, guardval);
67 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
70 /* now we have to generate the inclusion code */
71 RelationInclusion ri = (RelationInclusion) rule.getInclusion();
73 // basically, destname = righthandside<r, r.field>
74 VarDescriptor tempdest = VarDescriptor.makeNew("tempdest");
75 Expr rhs = ri.getRightExpr();
76 rhs.generate(cr, tempdest);
78 cr.outputline(destname + " = " + tempdest.getSafeSymbol() + ";");
81 cr.outputline("else");
84 // three valued logic. if the relation (which is a partial function)
85 // fails its guard, then we have a "maybe" condition, which must
88 cr.outputline("maybe = 1;");
96 public void prettyPrint(PrettyPrinter pp) {
99 pp.output(relation.getSafeSymbol());
102 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
103 throw new IRException();