mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* Move Tuple class from functions.h to objects.h
* Add packToken::token() function to return the TokenBase*
* Fix bug on Tuple default constructor
* Fix bug on function calls with 0 args
* Add Python string format operator: '%'
Now it is possible to write: 'replace %s for text' % 'text'
and it works like it would in Python.
It also work with:
- Numbers: 'number %s' % 10 == 'number 10'
- Multiple items: '%s %s %s' % (1,2,3) == '1 2 3'
- Escapping %s:
- 'escape \%s %s' % '' == 'escape %s '
- 'ignore % and %% %s' % '' == 'ignore % and %% '
- It also throws if the number of formats and arguments diverge:
- ' %s %s ' % 'one arg' // throws
- ' %s ' % ('two', 'args') // throws
* Add tests for Iterators and improve MapIterator
* Add built-in `type()` function
* Move the function call procedure to `Function::call()`
* Greatly simplify the resolve_reference function
* Add tests for the prototypical inheritance feature
* Add function `packToken::asFunc()`
* Add built-in function `extend(obj)`
This function allow users to play with the prototipical
inheritance feature. The function will return a new object whose
parent (i.e. prototype) is `obj`.
* Move TokenMap function implementations to objects.cpp
* Add `map.instanceof(other_map)` function
216 lines
4.6 KiB
C++
216 lines
4.6 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 global_map(0);
|
|
return global_map;
|
|
}
|
|
|
|
TokenMap& TokenMap::default_global() {
|
|
static TokenMap global_map(base_map());
|
|
return global_map;
|
|
}
|
|
|
|
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");
|
|
|
|
// If "this" is not a list it will throw here:
|
|
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");
|
|
|
|
uint pos;
|
|
|
|
if ((*token)->type == NUM) {
|
|
pos = (uint)(token->asDouble());
|
|
} 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()
|
|
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();
|
|
}
|
|
|
|
/* * * * * Initialize TokenList functions * * * * */
|
|
|
|
struct TokenList::Startup {
|
|
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");
|
|
}
|
|
} list_startup;
|
|
|
|
/* * * * * 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; }
|
|
|
|
/* * * * * Tuple Functions: * * * * */
|
|
|
|
Tuple::Tuple(const TokenBase* a) {
|
|
tuple.push_back(a->clone());
|
|
this->type = TUPLE;
|
|
}
|
|
|
|
Tuple::Tuple(const TokenBase* a, const TokenBase* b) {
|
|
tuple.push_back(a->clone());
|
|
tuple.push_back(b->clone());
|
|
this->type = TUPLE;
|
|
}
|
|
|
|
void Tuple::push_back(const TokenBase* tb) {
|
|
tuple.push_back(tb->clone());
|
|
}
|
|
|
|
TokenBase* Tuple::pop_front() {
|
|
if (tuple.size() == 0) {
|
|
throw std::range_error("Can't pop front of an empty Tuple!");
|
|
}
|
|
|
|
TokenBase* value = tuple.front();
|
|
tuple.pop_front();
|
|
return value;
|
|
}
|
|
|
|
unsigned int Tuple::size() {
|
|
return tuple.size();
|
|
}
|
|
|
|
Tuple::Tuple_t Tuple::copyTuple(const Tuple_t& t) {
|
|
Tuple_t copy;
|
|
Tuple_t::const_iterator it;
|
|
for (it = t.begin(); it != t.end(); ++it) {
|
|
copy.push_back((*it)->clone());
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
void Tuple::cleanTuple(Tuple_t* t) {
|
|
while (t->size()) {
|
|
delete t->back();
|
|
t->pop_back();
|
|
}
|
|
}
|
|
|
|
Tuple& Tuple::operator=(const Tuple& t) {
|
|
cleanTuple(&tuple);
|
|
tuple = copyTuple(t.tuple);
|
|
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;
|
|
}
|
|
}
|
|
|
|
const packToken* TokenMap::find(std::string key) const {
|
|
TokenMap_t::const_iterator it = map.find(key);
|
|
|
|
if (it != map.end()) {
|
|
return &it->second;
|
|
} else if (parent) {
|
|
return parent->find(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);
|
|
}
|