mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* Remove redundant calculator::compile(const char*, OppMap_t) * Add support for calculator to work as a slave parser As a slave parser I mean it will parse until a specified delimiter and report back the pointer of where it has stopped. This way another class can the calculator constructor to parse or evaluate a sequence of expressions. * Add static calculator::str() implementation for help debug * Add an arguments validation check at calculator::toRPN() * Minor refactor: Move calculator::scope_empty to Scope::empty * Set Function string argument to `const char*` * Update files to satisfy Travis CI rules * Move Function::default_functions to Scope::default_global The TokenMap_t Function::default_functions was used as base map for all Scope instances, and represented the global scope. It is more coherent to keep it with the name default_global and defined as a static member of Scope class. Now any other class that needs to add something to the global scope can just use Scope::default_global. * Make packToken::str(TokenBase*) accept const argument * Fix memory leak problem on Reference Token management * Prevent bug called "static initialization order fiasco" This bug is better explained here: - https://isocpp.org/wiki/faq/ctors#static-init-order Short version: It is caused when the compiler initializes static objects in a random order. And one of these objects depend on the initialization of another one. In our project the singleton class Function::Startup depends on the TokenMap_t Scope::default_global. It might cause no bug if we are lucky with the init order the compiler chooses. But might cause big problems otherwise. This commit fix this problem so we don't need to depend on lucky. * Optimize calculator::toRPN loop * Update README.md features section * Update quote escaping for string literals Now the user can type \\n and it will be converted to \n. It also works for \\t, but nothing happens with other characters like: \\f or \\o Also add more tests for the string section. * Fix bug on string escaping * Add RAII_TokenQueue_t class to facilitate rpn management Since C++ does not allow a finally block it is hard to be sure something will be dealloc'd even when exceptions are thrown. The function calculator::calculate(char* expr, ...) needed to have a catch for each possible exception type thrown by its internal function calls (where a single finally block would have done the job) The only alternative encouraged by C++ is the RAII idiom so the RAII_TokenQueue_t was built. This new class should be used with care, but it has reduced the number of explicit calls to cleanRPN which is good. * Add valgrind check (make check) to Makefile * Fix () operator so m.a(arg) works * Fix `print()` so that it will call `print(None)` * Add string representations to types FUNC and TUPLE
200 lines
4.8 KiB
C++
200 lines
4.8 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::operator=(int t) {
|
|
if (base) delete base;
|
|
base = new Token<double>(t, NUM);
|
|
return *this;
|
|
}
|
|
|
|
packToken& packToken::operator=(double t) {
|
|
if (base) delete base;
|
|
base = new Token<double>(t, NUM);
|
|
return *this;
|
|
}
|
|
|
|
packToken& packToken::operator=(const char* t) {
|
|
if (base) delete base;
|
|
base = new Token<std::string>(t, STR);
|
|
return *this;
|
|
}
|
|
|
|
packToken& packToken::operator=(const std::string& t) {
|
|
if (base) delete base;
|
|
base = new Token<std::string>(t, STR);
|
|
return *this;
|
|
}
|
|
|
|
packToken& packToken::operator=(const packToken& t) {
|
|
if (base) delete base;
|
|
if (t.base) {
|
|
base = t.base->clone();
|
|
} else {
|
|
base = 0;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
// Used mainly for testing
|
|
bool packToken::operator==(const packToken& token) const {
|
|
if (!base) {
|
|
return token.base == 0;
|
|
} else if (!token.base) {
|
|
return base == 0;
|
|
} else if (token.base->type != base->type) {
|
|
return false;
|
|
} else {
|
|
// Compare strings to simplify code
|
|
return token.str().compare(str()) == 0;
|
|
}
|
|
}
|
|
|
|
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 || base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<Token<TokenMap_t*>*>(base)->val)[key];
|
|
}
|
|
const packToken& packToken::operator[](const std::string& key) const {
|
|
if (!base || base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<Token<TokenMap_t*>*>(base)->val)[key];
|
|
}
|
|
packToken& packToken::operator[](const char* key) {
|
|
if (!base || base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<Token<TokenMap_t*>*>(base)->val)[key];
|
|
}
|
|
const packToken& packToken::operator[](const char* key) const {
|
|
if (!base || base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return (*static_cast<Token<TokenMap_t*>*>(base)->val)[key];
|
|
}
|
|
|
|
bool packToken::asBool() const {
|
|
if (!base) {
|
|
throw bad_cast(
|
|
"The Token is not a number!");
|
|
}
|
|
|
|
switch (base->type) {
|
|
case NUM:
|
|
return static_cast<Token<double>*>(base)->val != 0;
|
|
case STR:
|
|
return static_cast<Token<std::string>*>(base)->val != std::string();
|
|
case MAP:
|
|
return true;
|
|
case NONE:
|
|
return false;
|
|
default:
|
|
throw bad_cast("Token type can not be cast to boolean!");
|
|
}
|
|
}
|
|
|
|
double packToken::asDouble() const {
|
|
if (!base || base->type != NUM) {
|
|
throw bad_cast(
|
|
"The Token is not a number!");
|
|
}
|
|
return static_cast<Token<double>*>(base)->val;
|
|
}
|
|
|
|
std::string packToken::asString() const {
|
|
if (!base ||
|
|
(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_t* packToken::asMap() const {
|
|
if (!base || base->type != MAP) {
|
|
throw bad_cast(
|
|
"The Token is not a map!");
|
|
}
|
|
return static_cast<Token<TokenMap_t*>*>(base)->val;
|
|
}
|
|
|
|
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 it;
|
|
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)->name;
|
|
}
|
|
|
|
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 NUM:
|
|
ss << static_cast<const Token<double>*>(base)->val;
|
|
return ss.str();
|
|
case STR:
|
|
return "\"" + static_cast<const Token<std::string>*>(base)->val + "\"";
|
|
case FUNC:
|
|
if (name.size()) return "[Function: " + name + "]";
|
|
return "[Function]";
|
|
case TUPLE:
|
|
ss << "(";
|
|
first = true;
|
|
for (const TokenBase* token : static_cast<const Tuple*>(base)->tuple) {
|
|
if (!first) {
|
|
ss << ", ";
|
|
} else {
|
|
first = false;
|
|
}
|
|
ss << str(token);
|
|
}
|
|
ss << ")";
|
|
return ss.str();
|
|
case MAP:
|
|
tmap = static_cast<const Token<TokenMap_t*>*>(base)->val;
|
|
if (tmap->size() == 0) return "{}";
|
|
ss << "{";
|
|
for (it = tmap->begin(); it != tmap->end(); ++it) {
|
|
ss << (it == tmap->begin() ? "" : ",");
|
|
ss << " \"" << it->first << "\": " << it->second.str();
|
|
}
|
|
ss << " }";
|
|
return ss.str();
|
|
default:
|
|
return "unknown_type";
|
|
}
|
|
}
|