gcc config
[prop.git] / prop-src / constraint.pcc
blob31f7eef236f064a5d500419915e6be63b96e2113
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 //  This file implements the auxiliary methods of the constraint compiler,
4 //  which include various pretty printing and AST transformation routines.
5 //
6 ///////////////////////////////////////////////////////////////////////////////
7 #include <iostream.h>
8 #include <AD/strings/quark.h>
9 #include "ir.ph"
10 #include "ast.ph"
11 #include "constraint.ph"
12 #include "wam.ph"
13 #include "type.h"
14 #include "hashtab.h"
15 #include "datagen.h"
17 ///////////////////////////////////////////////////////////////////////////////
19 //  Instantiate the constraint datatypes
21 ///////////////////////////////////////////////////////////////////////////////
22 instantiate datatype ConstraintSet,
23                      ConstraintDef,
24                      ConstraintRule,
25                      List<ConstraintDef>,
26                      ConstraintBody
29 ///////////////////////////////////////////////////////////////////////////////
31 //  Pretty printing routines.
33 ///////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
37 //  Pretty print a rule set.
39 ///////////////////////////////////////////////////////////////////////////////
40 ostream& operator << (ostream& f, ConstraintSet ruleset)
41 {  match (ruleset) of
42       CONSTRAINTset rules: { return f << rules; }
43    end match;
46 ///////////////////////////////////////////////////////////////////////////////
48 //  Pretty print a list of constraint definition.
50 ///////////////////////////////////////////////////////////////////////////////
51 ostream& operator << (ostream& f, ConstraintDefs defs)
52 {  match while (defs) of
53       #[ one ... rest ]: { f << one; defs = rest; }
54    end match;
55    return f;
58 ///////////////////////////////////////////////////////////////////////////////
60 //  Pretty print a definition.
62 ///////////////////////////////////////////////////////////////////////////////
63 ostream& operator << (ostream& f, ConstraintDef def)
64 {  match (def) of
65       CONSTRAINTruledef r:         { return f << r << '\n'; }
66    |  CONSTRAINTtype (id,ty):      { return f << id << " : " << ty << ";\n"; }
67    |  CONSTRAINTinstness (id,pat): { return f << id << " = " << pat << ";\n"; }
68    |  CONSTRAINTdet  (id,pat,det): { return f << id << " :: " << pat    
69                                               << " is " << det << ";\n";
70                                    }
71    end match;
74 ///////////////////////////////////////////////////////////////////////////////
76 //  Pretty print a constraint rule.
78 ///////////////////////////////////////////////////////////////////////////////
79 ostream& operator << (ostream& f, ConstraintRule rule)
80 {   match (rule) of
81        CONSTRAINTrule { id, pat, body = CONSTRAINTnone ... }: // axiom
82        {  return f << id << ' ' << pat << ';'; }
83     |  CONSTRAINTrule { id, pat, body ... }:                  // inference rule
84        {  return f << id << ' ' << pat << " :- " << body << ';'; }
85     end match; 
88 ///////////////////////////////////////////////////////////////////////////////
90 //  Pretty print the body of a constraint.
92 ///////////////////////////////////////////////////////////////////////////////
93 ostream& operator << (ostream& f, ConstraintBody b)
94 {  match (b) of
95       CONSTRAINTnone:       // skip
96    |  CONSTRAINTcut:        { f << "!"; }
97    |  CONSTRAINTand (a,b):  { f << a << ", " << b; }
98    |  CONSTRAINTif (a,b,c): { f << "if " << a << " then " << b 
99                                 << " else " << c << " end if"; }
100    |  CONSTRAINTbody  _:    { f << "{ ... }"; }
101    |  CONSTRAINTcall  e:    { f << e; }
102    end match;
103    return f;
106 ///////////////////////////////////////////////////////////////////////////////
108 //  Hash function for id/type pair
110 ///////////////////////////////////////////////////////////////////////////////
111 unsigned int id_ty_hash (HashTable::Key k)
112 {  Pair<Id,Ty> key = (Pair<Id,Ty>)(k);
113    return string_hash(key->fst); // + ty_hash(key->snd);
116 ///////////////////////////////////////////////////////////////////////////////
118 //  Equality function for id/type pair
120 ///////////////////////////////////////////////////////////////////////////////
121 Bool id_ty_equal (HashTable::Key a, HashTable::Key b)
122 {  Pair<Id,Ty> x = (Pair<Id,Ty>)(a);
123    Pair<Id,Ty> y = (Pair<Id,Ty>)(b);
124    return string_equal(x->fst,y->fst); // && ty_equal(x->snd,y->snd);
128 ///////////////////////////////////////////////////////////////////////////////
130 //  Constructor and destructor of the constraint compiler
132 ///////////////////////////////////////////////////////////////////////////////
133 ConstraintCompiler:: ConstraintCompiler() : internal(0) {}
134 ConstraintCompiler::~ConstraintCompiler() {}