Files
cparse/objects.h
Vinícius Garcia 9182625ae1 Simplify operations (#38)
* Greatly simplify declaring new operations

Now declaring new operations looks much more like
declaring a new function. The most important difference
is that its arguments are `TokenBase*` instead of `packToken`s.

* Greatly simplify definition of new Operations

Before this commit defining a new Operation would
mean:

1. Extend a class
2. Overwrite a function
3. Manually build a mask
4. Add it to the correct map

Now the process to define a new operations is a lot simpler:

1. Define a function
2. Call the `add()` function to add it to the correct map

Beside this change there is one more simplification: the
signature of the function that describes the operation behavior
now receives `packToken`s as arguments instead of `TokenBase*`.

This means that our users won't have to learn about `TokenBase*`
and won't need to worry about memory management.

An example of the new syntax follows:

```C++
packToken my_operation(const packToken& left, const std::string& op, const packToken& right) {
  return left == right;
}

class myStartup {
  myStartup() {
    opMap_t opMap& = calculator::opMap();
    opMap.add({ANY_TYPE, "==", ANY_TYPE}, &my_operation);
  }
} myStartup;
```

* Simplify RefToken's constructor

This commit simplifies the RefToken's constructor
so that it can accept packTokens instead of TokenBase* as
the `value` argument.

This allows to remove all references of the TokenBase class from
the built-in operations. Making them good examples for our users
to use as template.

* Move all builtin logic to operations.cpp file

* Rename `operations.cpp` to `builtin-operations.cpp`

* Add `make release` as dependency of `make all`

* Fix Travis Build
2016-10-24 21:04:08 -02:00

204 lines
4.6 KiB
C++

#ifndef OBJECTS_H_
#define OBJECTS_H_
#include <map>
#include <list>
#include <vector>
#include <string>
// The pack template class manages
// reference counting.
#include "./pack.h"
class Iterator;
struct Iterable : public TokenBase {
virtual ~Iterable() {}
virtual Iterator* getIterator() = 0;
};
// Iterator super class.
struct Iterator : public Iterable {
Iterator() { this->type = IT; }
virtual ~Iterator() {}
// Return the next position of the iterator.
// When it reaches the end it should return NULL
// and reset the iterator automatically.
virtual packToken* next() = 0;
virtual void reset() = 0;
Iterator* getIterator();
};
class TokenMap;
typedef std::map<std::string, packToken> TokenMap_t;
struct MapData_t {
TokenMap_t map;
TokenMap* parent;
MapData_t(TokenMap* p);
MapData_t(const MapData_t& other);
~MapData_t();
MapData_t& operator=(const MapData_t& other);
};
struct TokenMap : public pack<MapData_t>, public Iterable {
// Static factories:
static TokenMap empty;
static TokenMap& base_map();
static TokenMap& default_global();
public:
// Attribute getters for the
// pack<MapData_t> super class:
TokenMap_t& map() const { return ref->obj->map; }
TokenMap* parent() const { return ref->obj->parent; }
public:
// Implement the Iterable Interface:
struct MapIterator : public Iterator {
const TokenMap_t& map;
TokenMap_t::const_iterator it = map.begin();
packToken last;
MapIterator(const TokenMap_t& map) : map(map) {}
packToken* next();
void reset();
TokenBase* clone() const {
return new MapIterator(*this);
}
};
Iterator* getIterator() {
return new MapIterator(map());
}
public:
TokenMap(TokenMap* parent = &TokenMap::base_map()) : pack(parent) {
// For the TokenBase super class
this->type = MAP;
}
TokenMap(const TokenMap& other) : pack(other) { this->type = MAP; }
virtual ~TokenMap() {}
public:
// Implement the TokenBase abstract class
TokenBase* clone() const {
return new TokenMap(*this);
}
public:
packToken* find(std::string key);
TokenMap* findMap(std::string key);
void assign(std::string key, TokenBase* value);
void insert(std::string key, TokenBase* value);
TokenMap getChild();
packToken& operator[](const std::string& str);
void erase(std::string key);
};
// Build a TokenMap which is a child of default_global()
struct GlobalScope : public TokenMap {
GlobalScope() : TokenMap(&TokenMap::default_global()) {}
};
typedef std::vector<packToken> TokenList_t;
class TokenList : public pack<TokenList_t>, public Iterable {
public:
// Attribute getter for the
// pack<TokenList_t> super class:
TokenList_t& list() const { return *(ref->obj); }
public:
struct ListIterator : public Iterator {
TokenList_t* list;
uint64_t i = 0;
ListIterator(TokenList_t* list) : list(list) {}
packToken* next();
void reset();
TokenBase* clone() const {
return new ListIterator(*this);
}
};
Iterator* getIterator() {
return new ListIterator(&list());
}
public:
TokenList() { this->type = LIST; }
virtual ~TokenList() {}
packToken& operator[](const uint64_t idx) { return list()[idx]; }
public:
// Implement the TokenBase abstract class
TokenBase* clone() const {
return new TokenList(*this);
}
};
class Tuple : public TokenList {
public:
Tuple() { this->type = TUPLE; }
Tuple(const TokenBase* first) {
this->type = TUPLE;
list().push_back(packToken(first->clone()));
}
Tuple(const TokenBase* first, const TokenBase* second) {
this->type = TUPLE;
list().push_back(packToken(first->clone()));
list().push_back(packToken(second->clone()));
}
public:
// Implement the TokenBase abstract class
TokenBase* clone() const {
return new Tuple(*this);
}
};
// This Special Tuple is to be used only as syntactic sugar, and
// constructed only with the operator `:`, i.e.:
// - passing key-word arguments: func(1, 2, optional_arg:10)
// - slicing lists or strings: my_list[2:10:2] (not implemented)
//
// STuple means one of:
// - Special Tuple, Syntactic Tuple or System Tuple
//
// I haven't decided yet. Suggestions accepted.
class STuple : public Tuple {
public:
STuple() { this->type = STUPLE; }
STuple(const TokenBase* first) {
this->type = STUPLE;
list().push_back(packToken(first->clone()));
}
STuple(const TokenBase* first, const TokenBase* second) {
this->type = STUPLE;
list().push_back(packToken(first->clone()));
list().push_back(packToken(second->clone()));
}
public:
// Implement the TokenBase abstract class
TokenBase* clone() const {
return new STuple(*this);
}
};
#endif // OBJECTS_H_