mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* Add string strip() and string split() functions
* Add list `join()` function.
* Make const version of packToken::token() function
* Add const modifier to Iterable::getIterator() function
* Fix static initialization order bug with packToken::None
Before this commit there was a hidden bug on packToken::None.
If it was called from a static initialized constructor it might
or might not cause a bug.
Now packToken::None is no longe a packToken, but instead a function
that returns a packToken when called.
To adapt old code to conform with this change you should replace
any occurency of `packToken::None` for `packToken::None()`.
* Group together: OppMap_t, rWordMap_t and opMap_t
Now these 3 containers were grouped into a a bigger one: Config_t
This change was done for all the 3 contained configuration data.
And this data had to be passed along on many functions.
Now only 1 argument is required to be passed.
An extra reason is for when a user wants to create his own calculator,
Before this he would need to overwrite 3 virtual functions, one
for each of those configuration containers, now 1 is enough.
The old static variables:
- calculator::default_opPrecedence
- calculator::default_opMap
- calculator::default_rWordMap
Were replaced by the Default static variable:
- Config_t calculator::Default
To access them now it is possible like this:
- calculator::Default().opPrecedence
- calculator::Default().opMap
- calculator::Default().rWordMap
* Small refactor + Add the concept of reserved character
rWordMap_t was replaced by the struct parserMap_t.
This was the small refactor. To add new reserved words the
syntax has changed a little:
```C++
parserMap_t& parser = calculator::Default().parserMap;
parser.add("op", &op_function);
// Before it was like this:
rWordMap_t& rmap = calculator::Default().rWordMap;
rmap["op"] = &op_function;
``
With this change we could add the concept of a reserved character.
To add a reserved character you can do in a very simillar way
to the way you do with reserved words, just make sure to use
simple quotes around the character:
```C++
parserMap_t& parser = calculator::Default().parserMap;
parser.add('=', &assignment);
```
Considerations:
1. Reserved characters work even if the current token contain
more than one character such the token `=-` on the expression:
- "a =-10"
It will consider the minus sign a separated token like: "a = -10".
2. In situations where the current token have meaning such as:
- "a == 10"
The operator meaning will have precedence, and the character
parser will not be called, thus allowing for the correct behavior.
The same is valid if "==" were a reserved, it also takes precedence
in relation to the reserved character.
This concept is complicated. However necessary. With this feature
it will be possible to remove the hardcoded operations:
- Assignment: `=`
- Subscripting: `.`
- Bracketing: `(`, `[` and `{`
And write them on `builtin-features.cpp`.
* Fix built-in type casts for `real` and `int`
* Update output of built-in function `type()`
Before this the output for the type REAL was `float`.
Now it will return the string `real` instead, making it more
coherent with the actual name of the type.
* Fix Travis Build
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() const {
|
|
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);
|
|
}
|