FAUST compiler  0.9.9.6b8
Functions | Variables
environment.cpp File Reference
#include "environment.hh"
#include "errormsg.hh"
#include "boxes.hh"
#include "ppbox.hh"
#include "names.hh"
Include dependency graph for environment.cpp:

Go to the source code of this file.

Functions

static Tree pushNewLayer (Tree lenv)
 Push a new (unique) empty layer (where multiple definitions can be stored) on top of an existing environment. More...
 
Tree pushEnvBarrier (Tree lenv)
 
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. More...
 
static void addLayerDef (Tree id, Tree def, Tree lenv)
 Add a definition (as a property) to the current top level layer. More...
 
Tree pushValueDef (Tree id, Tree def, Tree lenv)
 Push a new layer and add a single definition. More...
 
Tree pushMultiClosureDefs (Tree ldefs, Tree visited, Tree lenv)
 Push a new layer with multiple definitions creating the appropriate closures. 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...
 
static void updateClosures (vector< Tree > &clos, Tree oldEnv, Tree newEnv)
 Replace closure that point to oldEnv with closure on newEnv. 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...
 

Variables

Sym BARRIER = symbol ("BARRIER")
 Push a new environment barrier on top of an existing environment so that searchIdDef (used by the pattern matcher) will not look after the barrier. More...
 

Function Documentation

static void addLayerDef ( Tree  id,
Tree  def,
Tree  lenv 
)
static

Add a definition (as a property) to the current top level layer.

Check and warn for multiple definitions.

Parameters
idthe symbol id to be defined
defthe definition to be binded to the symbol id
lenvthe environment where to add this new definition

Definition at line 69 of file environment.cpp.

References evalwarning(), gErrorCount, getDefFileProp(), getDefLineProp(), getProperty(), print(), and setProperty().

Referenced by pushMultiClosureDefs(), and pushValueDef().

70 {
71  // check for multiple definitions of a symbol in the same layer
72  Tree olddef;
73  if (getProperty(lenv, id, olddef)) {
74  if (def == olddef) {
75  evalwarning(getDefFileProp(id), getDefLineProp(id), "equivalent re-definitions of", id);
76  } else {
77  fprintf(stderr, "%s:%d: ERROR: redefinition of symbols are not allowed : ", getDefFileProp(id), getDefLineProp(id));
78  print(id,stderr);
79  fprintf(stderr, " is already defined in file \"%s\" line %d \n", getDefFileProp(id), getDefLineProp(id));
80  gErrorCount++;
81  }
82  }
83  setProperty(lenv, id, def);
84 }
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
void evalwarning(const char *filename, int linenum, const char *msg, Tree exp)
Definition: errormsg.cpp:53
int getDefLineProp(Tree sym)
Definition: errormsg.cpp:82
int gErrorCount
Definition: errormsg.cpp:31
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
const char * getDefFileProp(Tree sym)
Definition: errormsg.cpp:72
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423
void print(Tree t, FILE *out)
Definition: list.cpp:154

Here is the call graph for this function:

Here is the caller graph for this function:

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:

static Tree pushNewLayer ( Tree  lenv)
static

Push a new (unique) empty layer (where multiple definitions can be stored) on top of an existing environment.

Parameters
lenvthe old environment
Returns
the new environment

Definition at line 26 of file environment.cpp.

References tree(), and unique().

Referenced by copyEnvReplaceDefs(), pushMultiClosureDefs(), and pushValueDef().

27 {
28  return tree(unique("ENV_LAYER"), lenv);
29 }
Symbol * unique(const char *str)
Returns a new unique symbol of name strxxx.
Definition: symbol.hh:97
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 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.

References addLayerDef(), and pushNewLayer().

Referenced by apply_pattern_matcher(), applyList(), iteratePar(), iterateProd(), iterateSeq(), iterateSum(), and real_a2sb().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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:

static void updateClosures ( vector< Tree > &  clos,
Tree  oldEnv,
Tree  newEnv 
)
static

Replace closure that point to oldEnv with closure on newEnv.

Definition at line 149 of file environment.cpp.

References closure(), and isClosure().

Referenced by copyEnvReplaceDefs().

150 {
151  for (unsigned int i=0; i < clos.size(); i++) {
152  Tree exp, genv, visited, lenv;
153  if (isClosure(clos[i], exp, genv, visited, lenv)) {
154  if (lenv == oldEnv) {
155  clos[i] = closure(exp, genv, visited, newEnv);
156  }
157  }
158  }
159 }
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
bool isClosure(Tree t, Tree &abstr, Tree &genv, Tree &vis, Tree &lenv)
Definition: boxes.cpp:239
Tree closure(Tree abstr, Tree genv, Tree vis, Tree lenv)
Definition: boxes.cpp:234

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

Sym BARRIER = symbol ("BARRIER")

Push a new environment barrier on top of an existing environment so that searchIdDef (used by the pattern matcher) will not look after the barrier.

This barrier will not any influence on regular environment lookup.

Parameters
lenvthe old environment
Returns
the new environment

Definition at line 41 of file environment.cpp.