FAUST compiler  0.9.9.6b8
Functions
environment.hh File Reference
#include "tlib.hh"
Include dependency graph for environment.hh:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Tree pushValueDef (Tree id, Tree def, Tree lenv)
 Push a new layer and add a single definition. More...
 
bool searchIdDef (Tree id, Tree &def, Tree lenv)
 Search the environment (until first barrier) for the definition of a symbol ID and return it. More...
 
Tree pushMultiClosureDefs (Tree ldefs, Tree visited, Tree lenv)
 Push a new layer with multiple definitions creating the appropriate closures. More...
 
Tree copyEnvReplaceDefs (Tree anEnv, Tree ldefs, Tree visited, Tree curEnv)
 Create a new environment by copying an existing one and replacing some definitions. More...
 
bool isEnvBarrier (Tree t)
 Test if the environment is a barrier (or nil) so that searchIdDef will know where to stop when searching an environment. More...
 
Tree pushEnvBarrier (Tree lenv)
 

Function Documentation

Tree copyEnvReplaceDefs ( Tree  anEnv,
Tree  ldefs,
Tree  visited,
Tree  curEnv 
)

Create a new environment by copying an existing one and replacing some definitions.

Parameters
xenvexisting environment we will copy
ldefslist of pairs (symbol id x definition) that will replace old definitions
visitedset of visited symbols (used for recursive definition detection)
lenvthe current environment to evaluate the definitions
Returns
the new environment

Definition at line 169 of file environment.cpp.

References CTree::branch(), closure(), CTree::exportProperties(), hd(), isBoxCase(), isNil(), nil, pushNewLayer(), setDefNameProperty(), setProperty(), tl(), and updateClosures().

Referenced by realeval().

170 {
171  vector<Tree> ids, clos;
172  Tree copyEnv;
173 
174  anEnv->exportProperties(ids, clos); // get the definitions of the environment
175  copyEnv = pushNewLayer(anEnv->branch(0)); // create new environment with same stack
176  updateClosures(clos, anEnv, copyEnv); // update the closures replacing oldEnv with newEnv
177 
178  for (unsigned int i=0; i < clos.size(); i++) { // transfers the updated definitions to the new environment
179  setProperty(copyEnv, ids[i], clos[i]);
180  }
181 
182  while (!isNil(ldefs)) { // replace the old definitions with the new ones
183  Tree def = hd(ldefs);
184  Tree id = hd(def);
185  Tree rhs= tl(def);
186  Tree cl = closure(rhs,nil,visited,curEnv);
187  stringstream s; s << boxpp(id);
188  if (!isBoxCase(rhs)) setDefNameProperty(cl,s.str());
189  setProperty(copyEnv, id, cl);
190  ldefs = tl(ldefs);
191  }
192  return copyEnv;
193 }
static void updateClosures(vector< Tree > &clos, Tree oldEnv, Tree newEnv)
Replace closure that point to oldEnv with closure on newEnv.
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Tree hd(Tree l)
Definition: list.hh:133
void setDefNameProperty(Tree t, Tree id)
Definition: names.cpp:54
bool isNil(Tree l)
Definition: list.hh:137
static Tree pushNewLayer(Tree lenv)
Push a new (unique) empty layer (where multiple definitions can be stored) on top of an existing envi...
Definition: environment.cpp:26
bool isBoxCase(Tree s)
Definition: boxes.cpp:617
Tree closure(Tree abstr, Tree genv, Tree vis, Tree lenv)
Definition: boxes.cpp:234
Definition: ppbox.hh:58
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree nil
Definition: list.cpp:116
void exportProperties(vector< Tree > &keys, vector< Tree > &values)
export the properties of a CTree as two vectors, one for the keys and one for the associated values ...
Definition: tree.cpp:388
Tree tl(Tree l)
Definition: list.hh:134
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145

Here is the call graph for this function:

Here is the caller graph for this function:

bool isEnvBarrier ( Tree  lenv)

Test if the environment is a barrier (or nil) so that searchIdDef will know where to stop when searching an environment.

Parameters
lenvthe environment to test
Returns
true is barrier reached

Definition at line 56 of file environment.cpp.

References isNil(), and CTree::node().

Referenced by searchIdDef().

57 {
58  return isNil(lenv) || (lenv->node() == Node(BARRIER));
59 }
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isNil(Tree l)
Definition: list.hh:137
Sym BARRIER
Push a new environment barrier on top of an existing environment so that searchIdDef (used by the pat...
Definition: environment.cpp:41

Here is the call graph for this function:

Here is the caller graph for this function:

Tree pushEnvBarrier ( Tree  lenv)

Definition at line 43 of file environment.cpp.

References tree().

Referenced by evalCase().

44 {
45  return tree(BARRIER, lenv);
46 }
Sym BARRIER
Push a new environment barrier on top of an existing environment so that searchIdDef (used by the pat...
Definition: environment.cpp:41
Tree tree(const Node &n)
Definition: tree.hh:186

Here is the call graph for this function:

Here is the caller graph for this function:

Tree pushMultiClosureDefs ( Tree  ldefs,
Tree  visited,
Tree  lenv 
)

Push a new layer with multiple definitions creating the appropriate closures.

Parameters
ldefslist of pairs (symbol id x definition) to be binded to the symbol id
visitedset of visited symbols (used for recursive definition detection)
lenvthe environment where to push the layer and add all the definitions
Returns
the new environment

Definition at line 109 of file environment.cpp.

References addLayerDef(), closure(), hd(), isBoxCase(), isNil(), nil, pushNewLayer(), setDefNameProperty(), and tl().

Referenced by evaldocexpr(), evalprocess(), and realeval().

110 {
111  Tree lenv2 = pushNewLayer(lenv);
112  while (!isNil(ldefs)) {
113  Tree def = hd(ldefs);
114  Tree id = hd(def);
115  Tree rhs= tl(def);
116  Tree cl = closure(tl(def),nil,visited,lenv2);
117  stringstream s; s << boxpp(id);
118  if (!isBoxCase(rhs)) setDefNameProperty(cl,s.str());
119  addLayerDef( id, cl, lenv2 );
120  ldefs = tl(ldefs);
121  }
122  return lenv2;
123 }
static void addLayerDef(Tree id, Tree def, Tree lenv)
Add a definition (as a property) to the current top level layer.
Definition: environment.cpp:69
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Tree hd(Tree l)
Definition: list.hh:133
void setDefNameProperty(Tree t, Tree id)
Definition: names.cpp:54
bool isNil(Tree l)
Definition: list.hh:137
static Tree pushNewLayer(Tree lenv)
Push a new (unique) empty layer (where multiple definitions can be stored) on top of an existing envi...
Definition: environment.cpp:26
bool isBoxCase(Tree s)
Definition: boxes.cpp:617
Tree closure(Tree abstr, Tree genv, Tree vis, Tree lenv)
Definition: boxes.cpp:234
Definition: ppbox.hh:58
Tree nil
Definition: list.cpp:116
Tree tl(Tree l)
Definition: list.hh:134

Here is the call graph for this function:

Here is the caller graph for this function:

Tree pushValueDef ( Tree  id,
Tree  def,
Tree  lenv 
)

Push a new layer and add a single definition.

Parameters
idthe symbol id to be defined
defthe definition to be binded to the symbol id
lenvthe environment where to push the layer and add the definition
Returns
the new environment

Definition at line 94 of file environment.cpp.

95 {
96  Tree lenv2 = pushNewLayer(lenv);
97  addLayerDef(id, def, lenv2);
98  return lenv2;
99 }
static void addLayerDef(Tree id, Tree def, Tree lenv)
Add a definition (as a property) to the current top level layer.
Definition: environment.cpp:69
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static Tree pushNewLayer(Tree lenv)
Push a new (unique) empty layer (where multiple definitions can be stored) on top of an existing envi...
Definition: environment.cpp:26
bool searchIdDef ( Tree  id,
Tree def,
Tree  lenv 
)

Search the environment (until first barrier) for the definition of a symbol ID and return it.

Used by the pattern matcher.

Parameters
idthe symbol ID to search
defwhere to store the definition if any
lenvthe environment
Returns
true if a definition was found

Definition at line 135 of file environment.cpp.

References CTree::branch(), getProperty(), and isEnvBarrier().

Referenced by apply_pattern_matcher().

136 {
137  // search the environment until a definition is found
138  // or a barrier (or nil) is reached
139 
140  while (!isEnvBarrier(lenv) && !getProperty(lenv, id, def)) {
141  lenv = lenv->branch(0);
142  }
143  return !isEnvBarrier(lenv);
144 }
bool isEnvBarrier(Tree lenv)
Test if the environment is a barrier (or nil) so that searchIdDef will know where to stop when search...
Definition: environment.cpp:56
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423

Here is the call graph for this function:

Here is the caller graph for this function: