I already have a structure (A std::vector of std::map `s encapsulated into a 'scope' class ....(..than may translate into a dynamic array of binary trees..AFAIK)) in which all variables are stored.
A new map is pushed back each time a new scope appears...and is popped when the scope exits.
The relevant portion of the code is...
The scope class can be found here (..but it is fairly obvious what it does.) : Confusing Template errorCode:std::string function::operator()(list& l) { std::string result; if(l.size()!=arg_list.size())throw(exception("Wrong no. of Args.")); std::map<std::string,std::string> scope_map; auto x=arg_list.begin(); auto y = l.begin(); for(;x!=arg_list.end();x++,y++) //<--This loop makes the map relating the values of the variable to their name { std::string foo(*x),bar(*y); scope_map[foo]=bar; } var_scope.new_local(scope_map); //var_scope is declared in "env.h" if(compiled) { result = (*body_c)(arg_list); } else { result = body_l.eval(); } var_scope.exit_scope(); //Pops the variable map of the current scope. return result; }
This is my usage of the 'virtual' stack represented with the scope class.
I'd try both approaches(linking this somehow to the machine stack... and detecting if the function will be 'bad' before executing it).
For the later, I have a good idea how to implement it so that catches most problems.
But I've no idea how to use this data structure to establish a relation to the machine stack.