Files
cparse/packToken.h
Vinícius Garcia e4ebe719cd Some refactoring and bug fixing (#22)
* 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
2016-07-18 12:11:02 -04:00

73 lines
2.1 KiB
C++

#ifndef PACKTOKEN_H_
#define PACKTOKEN_H_
#include <string>
class calculator;
class Scope;
// Encapsulate TokenBase* into a friendlier interface
class packToken {
TokenBase* base;
public:
static const packToken None;
public:
packToken() : base(0) {}
packToken(const TokenBase& t) : base(t.clone()) {}
packToken(const packToken& t) : base(t.base ? t.base->clone() : 0) {}
packToken& operator=(const packToken& t);
template<class C>
packToken(C c, tokType type) : base(new Token<C>(c, type)) {}
packToken(int d) : base(new Token<double>(d, NUM)) {}
packToken(double d) : base(new Token<double>(d, NUM)) {}
packToken(const char* s) : base(new Token<std::string>(s, STR)) {}
packToken(const std::string& s) : base(new Token<std::string>(s, STR)) {}
packToken(TokenMap_t* tmap) : base(new Token<TokenMap_t*>(tmap, MAP)) {}
~packToken() {if (base) delete base;}
packToken& operator=(int t);
packToken& operator=(double t);
packToken& operator=(const char* t);
packToken& operator=(const std::string& t);
TokenBase* operator->() const;
bool operator==(const packToken& t) const;
packToken& operator[](const std::string& key);
packToken& operator[](const char* key);
const packToken& operator[](const std::string& key) const;
const packToken& operator[](const char* key) const;
operator TokenBase*() { return base; }
bool asBool() const;
double asDouble() const;
std::string asString() const;
TokenMap_t* asMap() const;
std::string str() const;
static std::string str(const TokenBase* t);
private:
// Note:
// This constructor makes sure the TokenBase*
// will be deleted when the packToken destructor is called.
//
// Do not delete the TokenBase* by yourself after
// building a packToken!
//
// If you want to copy the TokenBase do instead:
// packToken(token->clone())
explicit packToken(TokenBase* t) : base(t) {}
// This constructor should only be called
// from inside the calculator os Scope classes:
friend class calculator;
friend class Scope;
};
// To allow cout to print it:
std::ostream& operator<<(std::ostream& os, const packToken& t);
#endif // PACKTOKEN_H_