mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* Replace all `scope->find(name)` calls for `scope[name]`
These calls were on the built-in functions declarations, and
were used to recover the function arguments.
This change was made because the find() function was not
the correct tool for the job; The find() function looks
on the entire prototype chain of the scope for the variable,
and this is not needed on this case since the function arguments
will always be on the most local scope of the function.
This refactor makes the code more readable and sightly more
efficient.
* Rename `argsList` to `args_t`
* Convert Function::name from a literal string to a getter function
* Simplify CppFunction constructor
This commit adds a new constructor to CppFunction that expects
a `const args_t` as argument.
This way it is possible to define a new function without having
to explicitly give how many arguments it receives, i.e.:
CppFunction(&my_func, {"arg1", "arg2", "arg3"}, "my_func");
instead of:
CppFunction(&my_func, 3, {"arg1", "arg2", "arg3"}, "my_func");
* Add new constructor to undefined_operation
* Major refactor: decouple operations from calculator class
Before this commit every new operation would be hard coded
inside the static calculator::calculate() function.
Now it is not required anymore. To add a new operation do as follow:
```C++
struct my_operation : public Operation {
// This describes the operands expected by this operation:
const opID_t getMask() { return build_mask(INT, REAL); }
// This describes the operation behavior:
TokenBase* exec(TokenBase* left, std::string op, TokenBase* right) {
if (/* not satisfy condition */) {
// Ignore this operation and keep looking for a match:
return 0;
}
if (/* erro condition*/) {
throw my_error("...");
}
// if its all ok:
TokenBase* result = some_code(left, right);
// Consume both Tokens:
delete left;
delete right;
// Return:
return result;
}
// Don't forget to declare and static instance:
} my_operation;
struct my_Startup {
my_Startup() {
// Register the operation on the default opMap:
opMap_t& opMap = calculator::default_opMap();
opMap[","].push_back(&my_operation);
opMap[ANY_OP].push_back(&my_other_operation);
}
} my_Startup;
```
The default operations are on the operations.cpp file.
To declare your own default operations, just make sure
to not link your code to `operations.o`, and add your
own description of your operations wherever you want.
In future work:
* Make an optional Operation function that works with
packTokens, so the final user will not need to worry about
memory management.
* Make it possible to overwrite the default_opMap by extending
the calculator class, creating any number of different
calculators as required.
* Add `release` recipe to Makefile
The `make release` will output 2 `.o` files:
- core-shunting-yard.o
- operations.o
The `core` bin contain an working calculator with
no defined operations except for asignment and function call,
that are still hard-coded on the system.
The `operations` bin contain all the other default operations.
If a user wants to define adhoc operations, include only
the `core` binary.
* Add tests for declaring adhoc operations
* Simplify Operation definition
Now there are 2 Operation classes: `BaseOperation` which is the
old class with a new name and the `Operation` that inherits from
the former.
The `exec()` pure virtual function from BaseOperation has the following
signature: TokenBase* exec(TokenBase*, std::string, TokenBase*);
The `exec()` pure virtual function from Operation has the following
signature: packToken exec(packToken, std::string, packToken);
This makes the usage of the Operation class easier since
hiding from the user the required memory management.
Advanced users can declare new operations using the `BaseOperation`
class. Working with TokenBase* is more efficient since
it does not require to call the packToken constructor and
destructor several times.
* Make possible to define adhoc opPrecedence.
Now it is possible to define your own operator precedence map
the same way it is done for operations.
The definition of the operator precedence was now moved to the
file `operations.h` and is initialized just before the operations.
If the user want to define its own operations and precedence map,
he just need to compile his code without the file `operations.o`.
After that he can define his own adhoc behavior in a file on his
project.
It is also possible to overwrite opPrecedence on a child calculator
class. The user just need to overwrite the function "opPrecedence()".
* Update README with new features.
240 lines
6.0 KiB
C++
240 lines
6.0 KiB
C++
#include <sstream>
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
#include "./shunting-yard.h"
|
|
#include "./packToken.h"
|
|
#include "./shunting-yard-exceptions.h"
|
|
|
|
const packToken packToken::None = packToken(TokenNone());
|
|
|
|
packToken::packToken(const TokenMap& map) : base(new TokenMap(map)) {}
|
|
packToken::packToken(const TokenList& list) : base(new TokenList(list)) {}
|
|
|
|
packToken& packToken::operator=(const packToken& t) {
|
|
delete base;
|
|
base = t.base->clone();
|
|
return *this;
|
|
}
|
|
|
|
bool packToken::operator==(const packToken& token) const {
|
|
if (NUM & token.base->type & base->type) {
|
|
return token.asDouble() == asDouble();
|
|
}
|
|
|
|
if (token.base->type != base->type) {
|
|
return false;
|
|
} else {
|
|
// Compare strings to simplify code
|
|
return token.str().compare(str()) == 0;
|
|
}
|
|
}
|
|
|
|
bool packToken::operator!=(const packToken& token) const {
|
|
return !(*this == token);
|
|
}
|
|
|
|
TokenBase* packToken::operator->() const {
|
|
return base;
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream &os, const packToken& t) {
|
|
return os << t.str();
|
|
}
|
|
|
|
packToken& packToken::operator[](const std::string& key) {
|
|
if (base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<TokenMap*>(base))[key];
|
|
}
|
|
const packToken& packToken::operator[](const std::string& key) const {
|
|
if (base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<TokenMap*>(base))[key];
|
|
}
|
|
packToken& packToken::operator[](const char* key) {
|
|
if (base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<TokenMap*>(base))[key];
|
|
}
|
|
const packToken& packToken::operator[](const char* key) const {
|
|
if (base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<TokenMap*>(base))[key];
|
|
}
|
|
|
|
bool packToken::asBool() const {
|
|
switch (base->type) {
|
|
case REAL:
|
|
return static_cast<Token<double>*>(base)->val != 0;
|
|
case INT:
|
|
return static_cast<Token<int64_t>*>(base)->val != 0;
|
|
case STR:
|
|
return static_cast<Token<std::string>*>(base)->val != std::string();
|
|
case MAP:
|
|
case FUNC:
|
|
return true;
|
|
case NONE:
|
|
return false;
|
|
case TUPLE:
|
|
case STUPLE:
|
|
return static_cast<Tuple*>(base)->list().size() != 0;
|
|
default:
|
|
throw bad_cast("Token type can not be cast to boolean!");
|
|
}
|
|
}
|
|
|
|
double packToken::asDouble() const {
|
|
if (!(base->type & NUM)) {
|
|
throw bad_cast(
|
|
"The Token is not a number!");
|
|
}
|
|
if (base->type == REAL) {
|
|
return static_cast<Token<double>*>(base)->val;
|
|
} else {
|
|
return static_cast<Token<int64_t>*>(base)->val;
|
|
}
|
|
}
|
|
|
|
int64_t packToken::asInt() const {
|
|
if (!(base->type & NUM)) {
|
|
throw bad_cast(
|
|
"The Token is not a number!");
|
|
}
|
|
if (base->type == REAL) {
|
|
return static_cast<Token<double>*>(base)->val;
|
|
} else {
|
|
return static_cast<Token<int64_t>*>(base)->val;
|
|
}
|
|
}
|
|
|
|
std::string& packToken::asString() const {
|
|
if (base->type != STR && base->type != VAR && base->type != OP) {
|
|
throw bad_cast(
|
|
"The Token is not a string!");
|
|
}
|
|
return static_cast<Token<std::string>*>(base)->val;
|
|
}
|
|
|
|
TokenMap& packToken::asMap() const {
|
|
if (base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return *static_cast<TokenMap*>(base);
|
|
}
|
|
|
|
TokenList& packToken::asList() const {
|
|
if (base->type != LIST) {
|
|
throw bad_cast(
|
|
"The Token is not a list!");
|
|
}
|
|
return *static_cast<TokenList*>(base);
|
|
}
|
|
|
|
Function* packToken::asFunc() const {
|
|
if (base->type != FUNC) {
|
|
throw bad_cast(
|
|
"The Token is not a function!");
|
|
}
|
|
return static_cast<Function*>(base);
|
|
}
|
|
|
|
std::string packToken::str() const {
|
|
return packToken::str(base);
|
|
}
|
|
|
|
std::string packToken::str(const TokenBase* base) {
|
|
std::stringstream ss;
|
|
TokenMap_t* tmap;
|
|
TokenMap_t::iterator m_it;
|
|
|
|
TokenList_t* tlist;
|
|
TokenList_t::iterator l_it;
|
|
const Function* func;
|
|
bool first;
|
|
std::string name;
|
|
|
|
if (!base) return "undefined";
|
|
|
|
if (base->type & REF) {
|
|
base = static_cast<const RefToken*>(base)->value;
|
|
name = static_cast<const RefToken*>(base)->key.str();
|
|
}
|
|
|
|
switch (base->type) {
|
|
case NONE:
|
|
return "None";
|
|
case OP:
|
|
return static_cast<const Token<std::string>*>(base)->val;
|
|
case VAR:
|
|
return static_cast<const Token<std::string>*>(base)->val;
|
|
case REAL:
|
|
ss << static_cast<const Token<double>*>(base)->val;
|
|
return ss.str();
|
|
case INT:
|
|
ss << static_cast<const Token<int64_t>*>(base)->val;
|
|
return ss.str();
|
|
case STR:
|
|
return "\"" + static_cast<const Token<std::string>*>(base)->val + "\"";
|
|
case FUNC:
|
|
func = static_cast<const Function*>(base);
|
|
if (func->name().size()) return "[Function: " + func->name() + "]";
|
|
if (name.size()) return "[Function: " + name + "]";
|
|
return "[Function]";
|
|
case TUPLE:
|
|
case STUPLE:
|
|
ss << "(";
|
|
first = true;
|
|
for (const TokenBase* token : static_cast<const Tuple*>(base)->list()) {
|
|
if (!first) {
|
|
ss << ", ";
|
|
} else {
|
|
first = false;
|
|
}
|
|
ss << str(token);
|
|
}
|
|
if (first) {
|
|
// Its an empty tuple:
|
|
// Add a `,` to make it different than ():
|
|
ss << ",)";
|
|
} else {
|
|
ss << ")";
|
|
}
|
|
return ss.str();
|
|
case MAP:
|
|
tmap = &(static_cast<const TokenMap*>(base)->map());
|
|
if (tmap->size() == 0) return "{}";
|
|
ss << "{";
|
|
for (m_it = tmap->begin(); m_it != tmap->end(); ++m_it) {
|
|
ss << (m_it == tmap->begin() ? "" : ",");
|
|
ss << " \"" << m_it->first << "\": " << m_it->second.str();
|
|
}
|
|
ss << " }";
|
|
return ss.str();
|
|
case LIST:
|
|
tlist = &(static_cast<const TokenList*>(base)->list());
|
|
if (tlist->size() == 0) return "[]";
|
|
ss << "[";
|
|
for (l_it = tlist->begin(); l_it != tlist->end(); ++l_it) {
|
|
ss << (l_it == tlist->begin() ? "" : ",");
|
|
ss << " " << l_it->str();
|
|
}
|
|
ss << " ]";
|
|
return ss.str();
|
|
default:
|
|
if (base->type & IT) {
|
|
return "[Iterator]";
|
|
}
|
|
return "unknown_type";
|
|
}
|
|
}
|