mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* Make Iterator class inherit from Iterable
* Add tests for str() representation of all Tokens
* Uninitialized calculators now return `None`
Before this commit evaluating an uninitialized calculator would
cause a segmentation fault. Now it will just return None.
* Add args variable to Makefile for passing args to tests
* Refactor Tuple class to inherit from TokenList
Now the Tuple class inherits from TokenList, this allows:
- To use less code to describe it
- To garbage collect it
* Add the Special Tuple operator `:`
This new Tuple operator differs from the operator `,` on 2 aspects:
1. It has greater operator priority.
2. The generate tuple has the type STUPLE (Special Tuple) so that
these 2 kinds of tuple won't be merged together.
With these changes it is possible to define a tuple like this:
(1, 'b':2, 'c':3, 4)
This will be used to provide make the language easier to use:
- Allow list slicing: my_list[1:30:2]
- Allow setting key-value args for functions: map('attr1':10, 'attr2':20)
The Special Tuple name is currently STuple, which could mean:
- Special Tuple
- System Tuple
- Syntactic Tuple
Another possible name would be 'Nested Tuple', in any case, the
current name is not very good, so new names suggestions are welcome.
* Move extend() function test
* Add keyword arguments support, update map() constructor
Now it is possible to pass keyword values to functions, as a sample
the map() constructor was updated to work with it:
```
m = map(
'a': 1,
'b': 2,
'c': list(1,2,3)
)
print(m) // { "a": 1, "b": 2, "c": [ 1, 2, 3 ] }
```
It should also work in conjunction with positional arguments:
```
func(1, 2, 3, 'optional_arg': 10)
```
But this was not tested yet for lack of a built-in function
to make use of it.
* Add map().pop('key') and map().len() built-in functions
225 lines
5.1 KiB
C++
225 lines
5.1 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;
|
|
}
|
|
|
|
TokenMap TokenMap::empty = TokenMap(&default_global());
|
|
|
|
/* * * * * TokenMap built-in functions * * * * */
|
|
|
|
const char* map_pop_args[] = {"key", "default"};
|
|
packToken map_pop(TokenMap scope) {
|
|
TokenMap map = scope["this"].asMap();
|
|
std::string key = scope["key"].asString();
|
|
|
|
// Check if the item is available and remove it:
|
|
if (map.map().count(key)) {
|
|
packToken value = map[key];
|
|
map.erase(key);
|
|
return value;
|
|
}
|
|
|
|
// If not available return the default value or None
|
|
packToken* def = scope.find("default");
|
|
if (def) {
|
|
return *def;
|
|
} else {
|
|
return packToken::None;
|
|
}
|
|
}
|
|
|
|
packToken map_len(TokenMap scope) {
|
|
TokenMap map = scope.find("this")->asMap();
|
|
return map.map().size();
|
|
}
|
|
|
|
/* * * * * TokenList built-in functions * * * * */
|
|
|
|
const char* push_args[] = {"item"};
|
|
packToken list_push(TokenMap scope) {
|
|
packToken* list = scope.find("this");
|
|
packToken* token = scope.find("item");
|
|
|
|
// If "this" is not a list it will throw here:
|
|
list->asList().list().push_back(*token);
|
|
|
|
return *list;
|
|
}
|
|
|
|
const char* list_pop_args[] = {"pos"};
|
|
packToken list_pop(TokenMap scope) {
|
|
TokenList list = scope.find("this")->asList();
|
|
packToken* token = scope.find("pos");
|
|
|
|
int64_t pos;
|
|
|
|
if ((*token)->type & NUM) {
|
|
pos = token->asInt();
|
|
|
|
// So that pop(-1) is the same as pop(last_idx):
|
|
if (pos < 0) pos = list.list().size()-pos;
|
|
} else {
|
|
pos = list.list().size()-1;
|
|
}
|
|
|
|
packToken result = list.list()[pos];
|
|
|
|
// Erase the item from the list:
|
|
// Note that this operation is optimal if pos == list.size()-1
|
|
list.list().erase(list.list().begin() + pos);
|
|
|
|
return result;
|
|
}
|
|
|
|
packToken list_len(TokenMap scope) {
|
|
TokenList list = scope.find("this")->asList();
|
|
return list.list().size();
|
|
}
|
|
|
|
/* * * * * Initialize TokenList functions * * * * */
|
|
|
|
struct TokenList::Startup {
|
|
Startup() {
|
|
TokenMap& base_list = calculator::type_attribute_map()[LIST];
|
|
base_list["push"] = CppFunction(list_push, 1, push_args, "push");
|
|
base_list["pop"] = CppFunction(list_pop, 1, list_pop_args, "pop");
|
|
base_list["len"] = CppFunction(list_len, "len");
|
|
|
|
TokenMap& base_map = TokenMap::base_map();
|
|
base_map["pop"] = CppFunction(map_pop, 2, map_pop_args, "pop");
|
|
base_map["len"] = CppFunction(map_len, "len");
|
|
}
|
|
} list_startup;
|
|
|
|
/* * * * * 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 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);
|
|
}
|