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
134 lines
3.0 KiB
C++
134 lines
3.0 KiB
C++
#include <cstdio>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
#include "./shunting-yard.h"
|
|
#include "./functions.h"
|
|
|
|
/* * * * * Built-in Functions: * * * * */
|
|
|
|
const char* text_arg[] = {"text"};
|
|
packToken default_print(const Scope* scope) {
|
|
// Get a single argument:
|
|
packToken* p = scope->find("text");
|
|
if ((*p)->type == STR) {
|
|
std::string text = p->asString();
|
|
printf("%s\n", text.c_str());
|
|
} else {
|
|
printf("\n");
|
|
}
|
|
|
|
return packToken::None;
|
|
}
|
|
|
|
const char* num_arg[] = {"number"};
|
|
packToken default_sqrt(const Scope* scope) {
|
|
// Get a single argument:
|
|
double number = scope->find("number")->asDouble();
|
|
|
|
return sqrt(number);
|
|
}
|
|
packToken default_sin(const Scope* scope) {
|
|
// Get a single argument:
|
|
double number = scope->find("number")->asDouble();
|
|
|
|
return sin(number);
|
|
}
|
|
packToken default_cos(const Scope* scope) {
|
|
// Get a single argument:
|
|
double number = scope->find("number")->asDouble();
|
|
|
|
return cos(number);
|
|
}
|
|
packToken default_tan(const Scope* scope) {
|
|
// Get a single argument:
|
|
double number = scope->find("number")->asDouble();
|
|
|
|
return tan(number);
|
|
}
|
|
packToken default_abs(const Scope* scope) {
|
|
// Get a single argument:
|
|
double number = scope->find("number")->asDouble();
|
|
|
|
return std::abs(number);
|
|
}
|
|
|
|
|
|
const char* pow_args[] = {"number", "exp"};
|
|
packToken default_pow(const Scope* scope) {
|
|
// Get two arguments:
|
|
double number = scope->find("number")->asDouble();
|
|
double exp = scope->find("exp")->asDouble();
|
|
|
|
return pow(number, exp);
|
|
}
|
|
|
|
/* * * * * Function Initializer Constructor: * * * * */
|
|
|
|
struct Function::Startup {
|
|
Startup() {
|
|
TokenMap_t& global = Scope::default_global();
|
|
|
|
global["print"] = Function(&default_print, 1, text_arg);
|
|
global["sqrt"] = Function(&default_sqrt, 1, num_arg);
|
|
global["sin"] = Function(&default_sin, 1, num_arg);
|
|
global["cos"] = Function(&default_cos, 1, num_arg);
|
|
global["tan"] = Function(&default_tan, 1, num_arg);
|
|
global["abs"] = Function(&default_abs, 1, num_arg);
|
|
global["pow"] = Function(&default_pow, 2, pow_args);
|
|
}
|
|
} startup;
|
|
|
|
/* * * * * 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 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;
|
|
}
|