Simplify containers (#26)

* Add a new CppFunction constructor for funcs with 0 args

* Update `pack` class API

* Remove `packMap` type in favor of the `TokenMap` class

The `packMap` type was defined as a `pack<TokenMap>`, so it would
manage memory of all Maps used on the system.

TLDR; This is a better design choice than it was before.

The reason for this change is to solve this 2 problems:

1. It creates 2 maps that do almost the same thing: packMap and TokenMap
   and it might confuse a reader of the code.

2. It could not count all TokenMap references, since some of these
   maps were referenced from inside the TokenMap class, i.e. the
   parent TokenMap of each TokenMap.

For those reasons we modified the TokenMap class. Now it inherits
from the pack<> class and manage a reference to a struct called `MapData_t`

----

This commit also makes TokenMap inherit from TokenBase, as well as
the Iterable interface.

This way it will be possible to cast a TokenBase* to an Iterable*
if the token type is an iterator, i.e. if `IT & type != 0`.

* Remove `packList` type in favor of `TokenList` class

Now TokenList inherits from:
- pack<TokenList_t>
- TokenBase
- Iterable

The reasons for this commit are the same of the last commit.

* Change "arglist" function keyword to "args"

* Refactor: Iterable is now a public TokenBase

Now there is no need to make TokenMap and TokenList
inherit from TokenBase and Iterable.

Now the Iterable class means "an Iterable Token"

* Make Iterator inherit from TokenBase

* Fix Travis build
This commit is contained in:
Vinícius Garcia
2016-09-01 10:50:41 -03:00
committed by Brandon Amos
parent 96e8b9b23d
commit 87c480956d
10 changed files with 399 additions and 317 deletions

View File

@@ -12,8 +12,8 @@
// - https://isocpp.org/wiki/faq/ctors#static-init-order
//
TokenMap& TokenMap::base_map() {
static TokenMap global_map(0);
return global_map;
static TokenMap _base_map(0);
return _base_map;
}
TokenMap& TokenMap::default_global() {
@@ -26,42 +26,44 @@ TokenMap TokenMap::empty = TokenMap(&default_global());
/* * * * * TokenList built-in functions * * * * */
const char* push_args[] = {"item"};
packToken list_push(packMap scope) {
packToken* list = scope->find("this");
packToken* token = scope->find("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);
list->asList().list().push_back(*token);
return *list;
}
const char* pop_args[] = {"pos"};
packToken list_pop(packMap scope) {
TokenList* list = scope->find("this")->asList();
packToken* token = scope->find("pos");
packToken list_pop(TokenMap scope) {
TokenList list = scope.find("this")->asList();
packToken* token = scope.find("pos");
uint pos;
int64_t pos;
if ((*token)->type == NUM) {
pos = (uint)(token->asDouble());
// 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;
pos = list.list().size()-1;
}
packToken result = list->list[pos];
packToken result = list.list()[pos];
// Erase the item from the list:
// Note that this operation is optimal if pos == list.size()
list->list.erase(list->list.begin() + pos);
// Note that this operation is optimal if pos == list.size()-1
list.list().erase(list.list().begin() + pos);
return result;
}
const char* list_no_args[] = {""};
packToken list_len(packMap scope) {
packList list = scope->find("this")->asList();
return list->list.size();
packToken list_len(TokenMap scope) {
TokenList list = scope.find("this")->asList();
return list.list().size();
}
/* * * * * Initialize TokenList functions * * * * */
@@ -71,7 +73,7 @@ struct TokenList::Startup {
TokenMap& base = calculator::type_attribute_map()[LIST];
base["push"] = CppFunction(list_push, 1, push_args, "push");
base["pop"] = CppFunction(list_pop, 1, pop_args, "pop");
base["len"] = CppFunction(list_len, 0, list_no_args, "len");
base["len"] = CppFunction(list_len, "len");
}
} list_startup;
@@ -156,27 +158,49 @@ Tuple& Tuple::operator=(const Tuple& t) {
return *this;
}
/* * * * * 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);
TokenMap_t::iterator it = map().find(key);
if (it != map.end()) {
if (it != map().end()) {
return &it->second;
} else if (parent) {
return parent->find(key);
} else if (parent()) {
return parent()->find(key);
} else {
return 0;
}
}
const packToken* TokenMap::find(std::string key) const {
TokenMap_t::const_iterator it = map.find(key);
TokenMap_t::const_iterator it = map().find(key);
if (it != map.end()) {
if (it != map().end()) {
return &it->second;
} else if (parent) {
return parent->find(key);
} else if (parent()) {
return parent()->find(key);
} else {
return 0;
}
@@ -194,7 +218,7 @@ void TokenMap::assign(std::string key, TokenBase* value) {
if (variable) {
(*variable) = packToken(value);
} else {
map[key] = packToken(value);
map()[key] = packToken(value);
}
}
@@ -203,7 +227,7 @@ void TokenMap::insert(std::string key, TokenBase* value) {
}
packToken& TokenMap::operator[](const std::string& key) {
return map[key];
return map()[key];
}
TokenMap TokenMap::getChild() {
@@ -211,5 +235,5 @@ TokenMap TokenMap::getChild() {
}
void TokenMap::erase(std::string key) {
map.erase(key);
map().erase(key);
}