The function implementation is not yet complete,
there are still some caveeats for example:
- Functions with no parameters will have an untested behavior.
- If a function receive a tuple as argument it will believe it
has received a list of parameters.
- If there are more arguments than expected the extra ones
can not be accessed.
One case that is already handled is the case when you have
less arguments than expected. In this case the remaining
arguments will be filled with packToken::None copies.
Before this fix it would not recognize that the minus was
an unary operator, because it did not raised the lastTokenWasOp flag
for '(' and for '[' as would be expected.
This statement was casting the exception specific types to
type `std::exception` and thus hiding its internal message.
Now we have one catch for each specific exception thrown by
calculator::calculate.
The builtin functions are defined in the file functions.cpp
Each function receive a Scope* as argument and must return
a packToken type.
The Function class inherits from TokenBase and its constructor
receive 3 arguments:
- A function pointer with signature: packToken (*func)(Scope*)
- The number of named arguments to expect.
- A std::string* list with the arguments name.
The number of named arguments must equal the length of the
string list.
The reason for the only argument be a Scope* is because a function
should also be able to access global variables. This scope is
created so that the arguments are inserted on its most
local TokenMap_t.
After the function execution this TokenMap_t is dellocated.
Builtin functions are now automatically added in the global
TokenMap_t of all scopes by the Scope constructor.
This commit removes the public attribute calculator::scope,
for 2 reasons:
1. It was not necessary since all functions accept a scope argument.
2. Interpret it as global variable was redundant since dealing with
the scope hierarchy is the job of the Scope class.
For these same reasons the functions that accepted a local and a
global scope as arguments were also updated to receive only one
scope instance as argument.
There were a memory leak problem when throwing errors.
Now before every throw it makes sure to release all
memory from local structures on functions toRPN() and calculate().
packToken(TokenBase*) not only builds a packToken but also
gets responsible for dellocating it, and this is not a normal
class behavior, since it could lead to a error prone code like this:
int main() {
TokenBase* b = new Token<double>(10, NUM);
packToken p = b;
... some code here ...
delete b;
... some more code ...
return 0; // Seg fault.
}
This commit makes this function private and explicit,
and only accessible from inside the class calculator which is
now friend of class packToken.
It was done to guarantee that only main developers have need
to fully comprehend this constructor behavior avoiding headaches.
Now instead of passing only maps of variables (TokenMap_t*)
to calculator instances you can keep record a Scope hierarchy.
The scope class works as a stack of TokenMap_t*.
When searching for a variable, it searches the
stack on a top-down order and stops as soon as it finds
a match for the search key.
The old API was not modified, everything still works as before,
because the Scope class constructor accepts a TokenMap_t*, so
you can insert a TokenMap_t* anywhere you could use an Scope instance.