Commit Graph

13 Commits

Author SHA1 Message Date
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
Vinícius Garcia
59fb48ea6c Add support for assignments on maps, e.g.: a.b = 10 2016-07-11 01:51:33 -03:00
Vinícius Garcia
da1d61092b Add support for assignment on existing variables
Now when resolving a variable's value it is created an rvalue
Token which keeps a reference to the original variable name.

This Token is then qualified to be used on the left side of
assigment operations.

When this is not the case it is converted to a normal Token
so that it can be used for calculations.
2016-07-11 01:51:33 -03:00
Vinícius Garcia
7b7e541d43 Add builtin function support
The builtin functions are defined in the file functions.cpp
Each function receive a Scope* as argument and must return
a packToken type.

The Function class inherits from TokenBase and its constructor
receive 3 arguments:
- A function pointer with signature: packToken (*func)(Scope*)
- The number of named arguments to expect.
- A std::string* list with the arguments name.

The number of named arguments must equal the length of the
string list.

The reason for the only argument be a Scope* is because a function
should also be able to access global variables. This scope is
created so that the arguments are inserted on its most
local TokenMap_t.

After the function execution this TokenMap_t is dellocated.

Builtin functions are now automatically added in the global
TokenMap_t of all scopes by the Scope constructor.
2016-07-09 13:04:47 -03:00
Brandon Amos
9d84f72b04 Use Google code style. Add cpplint and cppcheck to Travis. 2016-06-27 15:01:44 -04:00
Vinícius Garcia
21565f44cb Add static str() function to packToken 2016-06-05 12:50:54 -03:00
Vinícius Garcia
8bc48c01e0 Add 2 utility functions to packToken
The functions are:
- bool packToken::asBool()
- packToken::operator TokenBase*()
2016-06-02 12:57:28 -03:00
Vinícius Garcia
4605387411 Add subscript operator to packToken to simplify MAP access 2016-04-25 12:24:54 -03:00
Vinícius Garcia
36866d0af6 Adapt packToken to work with the MAP type 2016-04-25 11:50:49 -03:00
Vinícius Garcia
03b4e17adc Update packToken to fit strings 2016-04-20 14:28:35 -03:00
Vinícius Garcia
976ef66292 Add copy assignment operator to packToken 2016-04-18 15:36:17 -03:00
Vinícius Garcia
0bb17555b7 Replace the token map value class for the packToken type
This change allow to the user of the `TokenMap_t` to:
- Implicit instantiate a BaseToken* from a double.
- Explicit (and safely) convert a BaseToken* to a double.
- Make use of `cout << map["var"]`
- Make use of a serialization function `str()`

The packToken class is not intended to be used on the internals
of the calculate and compile functions, because it would add
some unnecessary function calls everytime a Token is accessed,
but it is useful to simplify the Token access for the API user.
2016-04-18 15:03:41 -03:00
Vinícius Garcia
e88858c1b9 Add first version of the TokenBase* facade: packToken 2016-04-18 05:24:26 -03:00