mirror of
https://github.com/allankoechke/cparse.git
synced 2026-12-01 09:13:31 +00:00
* Simplify `handle_op()` and `handle_unary()` functions
Now both receive an rpnBuilder as argument, instead of receiving
several different variables.
`handle_unary()` now writes to `data->lastTokenWasUnary()` instead
of returning a true/false argument to be written on it outside
the function as it was before. The new return type of the
function is void.
* Join the functions `handle_unary` and `handle_op`
Since they were always called together and did almost the same
thing there was no reason to keep them separated.
* Minor refactor - Move `opPrecedence` into `rpnData`
Now the function `handle_op` requires only 2 arguments:
- const char* expr
- rpnBuilder* data
* Expose `handle_op` to API
`handle_op()` function was exposed to API so that it could be used
on custom reserved word parsers defined by our users.
* Add the builtin map constructor operator: '{}'
Now it is possible to create maps just by writing:
```C++
calculator::calculate("M = { 'my':'map' }");
```
* Copy `map` and `list` constructors to `objects.*`
These constructors were defined as CppFunctions on
`builtin-features.cpp`, now that we are defining
the implicit constructors for map: `{}` and list: `[]` it
was necessary to define it on someplace the user could not change.
The original code on `builtin-features.cpp` is unchanged. The user
can still overwrite his own definition of the builtin `map` and `list`
functions, as he wish.
* Add list implicit constructor `[1,2,3]`
Now you can declare a new list as you would in JavaScript:
```C++
calculator::calculate("L = [1,2,3]");
```
* Add logo to repository
* Add move constructor to packToken class
* Move some functions into the rpnBuilder class
The functions:
- `handle_op()`
- `handle_unary()`
- `cleanRPN()`
were moved inside the rpnBuilder class, allowing now to
use `data.handle_op("+")` instead of `handle_op("+", &data)`.
More functions and functionalities should be moved into this class
in future, since we can use it to decouple the rpn building process
from the calculator class.
* Minor refactor: Move open/close bracket code to functions
* Minor refactor - move 2 functions in `builtin-features.cpp`
* Make public the static functions: toRPN and calculate
These 2 functions are useful to be used on the user defined
reserved word parsers, as such they are now available
to be called outside the calculator class.
* Refactor: Decouple internal variables of calculate()
Now the internal variables of the calculate function are
joined together on a struct called "evaluationData".
It can then be exposed for the user to access it in future commits.
* Refactor: Expose calculator variables to Operation::exec()
Now the `Operation::exec` function's signature is:
```C++
packToken exec(const packToken& left, const packToken& right,
const evaluationData* data);
```
The std::string op (that was before this commit the second argument
of the exec() function) is now available inside the evaluationData type.
This struct also contains several other internal variables used
by the calculator::calculate() function. This commit thus allows
more power and flexibility to user Operations.
168 lines
3.5 KiB
C++
168 lines
3.5 KiB
C++
#include "./shunting-yard.h"
|
|
|
|
#include "./objects.h"
|
|
#include "./functions.h"
|
|
|
|
/* * * * * Initialize TokenMap * * * * */
|
|
|
|
// Using the "Construct On First Use Idiom"
|
|
// to avoid the "static initialization order fiasco",
|
|
// for more information read:
|
|
//
|
|
// - https://isocpp.org/wiki/faq/ctors#static-init-order
|
|
//
|
|
TokenMap& TokenMap::base_map() {
|
|
static TokenMap _base_map(0);
|
|
return _base_map;
|
|
}
|
|
|
|
TokenMap& TokenMap::default_global() {
|
|
static TokenMap global_map(base_map());
|
|
return global_map;
|
|
}
|
|
|
|
packToken TokenMap::default_constructor(TokenMap scope) {
|
|
return scope["kwargs"];
|
|
}
|
|
|
|
TokenMap TokenMap::empty = TokenMap(&default_global());
|
|
|
|
|
|
/* * * * * Iterator functions * * * * */
|
|
|
|
Iterator* Iterator::getIterator() {
|
|
return static_cast<Iterator*>(this->clone());
|
|
}
|
|
|
|
/* * * * * TokenMap iterator implemented functions * * * * */
|
|
|
|
packToken* TokenMap::MapIterator::next() {
|
|
if (it != map.end()) {
|
|
last = packToken(it->first);
|
|
++it;
|
|
return &last;
|
|
} else {
|
|
it = map.begin();
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void TokenMap::MapIterator::reset() { it = map.begin(); }
|
|
|
|
/* * * * * TokenList functions: * * * * */
|
|
|
|
packToken TokenList::default_constructor(TokenMap scope) {
|
|
// Get the arguments:
|
|
TokenList list = scope["args"].asList();
|
|
|
|
// If the only argument is iterable:
|
|
if (list.list().size() == 1 && list.list()[0]->type & IT) {
|
|
TokenList new_list;
|
|
Iterator* it = static_cast<Iterable*>(list.list()[0].token())->getIterator();
|
|
|
|
packToken* next = it->next();
|
|
while (next) {
|
|
new_list.list().push_back(*next);
|
|
next = it->next();
|
|
}
|
|
|
|
delete it;
|
|
return new_list;
|
|
} else {
|
|
return list;
|
|
}
|
|
}
|
|
|
|
/* * * * * TokenList iterator implemented functions * * * * */
|
|
|
|
packToken* TokenList::ListIterator::next() {
|
|
if (i < list->size()) {
|
|
return &list->at(i++);
|
|
} else {
|
|
i = 0;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void TokenList::ListIterator::reset() { i = 0; }
|
|
|
|
/* * * * * MapData_t struct: * * * * */
|
|
|
|
MapData_t::MapData_t(TokenMap* p) : parent(p ? new TokenMap(*p) : 0) {}
|
|
MapData_t::MapData_t(const MapData_t& other) {
|
|
map = other.map;
|
|
if (other.parent) {
|
|
parent = new TokenMap(*(other.parent));
|
|
} else {
|
|
parent = 0;
|
|
}
|
|
}
|
|
MapData_t::~MapData_t() { if (parent) delete parent; }
|
|
|
|
MapData_t& MapData_t::operator=(const MapData_t& other) {
|
|
if (this != &other) {
|
|
if (parent) delete parent;
|
|
map = other.map;
|
|
parent = other.parent;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
/* * * * * TokenMap Class: * * * * */
|
|
|
|
packToken* TokenMap::find(std::string key) {
|
|
TokenMap_t::iterator it = map().find(key);
|
|
|
|
if (it != map().end()) {
|
|
return &it->second;
|
|
} else if (parent()) {
|
|
return parent()->find(key);
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
TokenMap* TokenMap::findMap(std::string key) {
|
|
TokenMap_t::iterator it = map().find(key);
|
|
|
|
if (it != map().end()) {
|
|
return this;
|
|
} else if (parent()) {
|
|
return parent()->findMap(key);
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void TokenMap::assign(std::string key, TokenBase* value) {
|
|
if (value) {
|
|
value = value->clone();
|
|
} else {
|
|
throw std::invalid_argument("TokenMap assignment expected a non NULL argument as value!");
|
|
}
|
|
|
|
packToken* variable = find(key);
|
|
|
|
if (variable) {
|
|
(*variable) = packToken(value);
|
|
} else {
|
|
map()[key] = packToken(value);
|
|
}
|
|
}
|
|
|
|
void TokenMap::insert(std::string key, TokenBase* value) {
|
|
(*this)[key] = packToken(value->clone());
|
|
}
|
|
|
|
packToken& TokenMap::operator[](const std::string& key) {
|
|
return map()[key];
|
|
}
|
|
|
|
TokenMap TokenMap::getChild() {
|
|
return TokenMap(this);
|
|
}
|
|
|
|
void TokenMap::erase(std::string key) {
|
|
map().erase(key);
|
|
}
|