00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <assert.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <limits.h>
00025 #include "recursivness.hh"
00026
00038
00039 static int annotate(Tree env, Tree sig);
00040 static int position (Tree env, Tree t, int p=1);
00041
00042 Tree RECURSIVNESS = tree(symbol("RecursivnessProp"));
00043
00044
00045
00051 void recursivnessAnnotation(Tree sig)
00052 {
00053 annotate(nil, sig);
00054 }
00055
00056
00064 int getRecursivness(Tree sig)
00065 {
00066 Tree tr;
00067 if ( ! getProperty(sig, RECURSIVNESS, tr)) {
00068 cerr << "Error in getRecursivness of " << *sig << endl;
00069 exit(1);
00070 }
00071 return tree2int(tr);
00072 }
00073
00074
00081 static int annotate(Tree env, Tree sig)
00082 {
00083 Tree tr, var, body;
00084
00085 if (getProperty(sig, RECURSIVNESS, tr)) {
00086 return tree2int(tr);
00087 } else if (isRec(sig, var, body)) {
00088 int p = position(env, sig);
00089 if (p > 0) {
00090 return p;
00091 } else {
00092 int r = annotate(cons(sig, env), body) - 1;
00093 if (r<0) r=0;
00094 setProperty(sig, RECURSIVNESS, tree(r));
00095 return r;
00096 }
00097 } else {
00098 int rmax = 0;
00099 vector<Tree> v; getSubSignals(sig, v);
00100 for (unsigned int i=0; i<v.size(); i++) {
00101 int r = annotate(env, v[i]);
00102 if (r>rmax) rmax=r;
00103 }
00104 setProperty(sig, RECURSIVNESS, tree(rmax));
00105 return rmax;
00106 }
00107 }
00108
00109
00110
00117 static int position (Tree env, Tree t, int p)
00118 {
00119 if (isNil(env)) return 0;
00120 if (hd(env) == t) return p;
00121 else return position (tl(env), t, p+1);
00122 }