Commit Graph

134 Commits

Author SHA1 Message Date
Brandon Amos
cb8865efb2 Bump to C++11. Add default functions to Scope() constructor. 2016-07-10 13:31:12 -04:00
Brandon Amos
21eb0d7f99 Add abs to resolve #2. 2016-07-10 08:34:03 -04:00
Brandon Amos
b98844e10c Merge pull request #20 from VinGarcia/master
Add function support
2016-07-10 07:09:09 -04:00
Vinícius Garcia
f62d717479 Add <stdexcept> to functions.cpp 2016-07-10 01:56:29 -03:00
Vinícius Garcia
ad5f28c5ce Fix style 2016-07-10 01:44:23 -03:00
Vinícius Garcia
80f29955f5 Fix style to conform with Travis suggestions 2016-07-10 01:30:04 -03:00
Vinícius Garcia
9ea4e26f2c Add TUPLE type and functions with multiple arguments
The function implementation is not yet complete,
there are still some caveeats for example:

- Functions with no parameters will have an untested behavior.
- If a function receive a tuple as argument it will believe it
  has received a list of parameters.
- If there are more arguments than expected the extra ones
  can not be accessed.

One case that is already handled is the case when you have
less arguments than expected. In this case the remaining
arguments will be filled with packToken::None copies.
2016-07-09 17:45:18 -03:00
Vinícius Garcia
81e60ce57e Fix bug on rpnQueue.front() access 2016-07-09 13:54:42 -03:00
Vinícius Garcia
30176c7b49 Fix unary evaluation for cases like: a+(-b)
Before this fix it would not recognize that the minus was
an unary operator, because it did not raised the lastTokenWasOp flag
for '(' and for '[' as would be expected.
2016-07-09 13:15:40 -03:00
Vinícius Garcia
f24fe12737 Fix catch statement on calculator::calculate
This statement was casting the exception specific types to
type `std::exception` and thus hiding its internal message.

Now we have one catch for each specific exception thrown by
calculator::calculate.
2016-07-09 13:11:56 -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
Vinícius Garcia
1ee61ce997 Add asign function to Scope 2016-07-08 22:16:55 -03:00
Vinícius Garcia
b47ec33d95 Move catch main to separated file so tests compile a lot faster 2016-07-08 20:41:37 -03:00
Vinícius Garcia
2d437c3b28 Change return type of Scope::find() to packToken* 2016-07-08 20:24:39 -03:00
Vinícius Garcia
5a7ccd20ed Remove internal scope attribute from calculator
This commit removes the public attribute calculator::scope,
for 2 reasons:

1. It was not necessary since all functions accept a scope argument.
2. Interpret it as global variable was redundant since dealing with
the scope hierarchy is the job of the Scope class.

For these same reasons the functions that accepted a local and a
global scope as arguments were also updated to receive only one
scope instance as argument.
2016-07-06 18:19:57 -03:00
Brandon Amos
fc18d4bd22 Update example in README. 2016-06-27 16:22:28 -04:00
Brandon Amos
4a67af72b8 Formatting. 2016-06-27 15:17:39 -04:00
Brandon Amos
d0a35c81f3 Formatting. 2016-06-27 15:15:53 -04:00
Brandon Amos
922dc7f10f Remove future work from README. 2016-06-27 15:13:15 -04:00
Brandon Amos
501a83b129 Correct link. 2016-06-27 15:12:05 -04:00
Brandon Amos
8c214ea43b Formatting. 2016-06-27 15:11:37 -04:00
Brandon Amos
bb5c41cbd7 Formatting. 2016-06-27 15:11:23 -04:00
Brandon Amos
ec5051f250 Move acknowledgements and authors from the code to the README. 2016-06-27 15:11:01 -04:00
Brandon Amos
9d84f72b04 Use Google code style. Add cpplint and cppcheck to Travis. 2016-06-27 15:01:44 -04:00
Brandon Amos
07130ffff0 Update tests to use catch. 2016-06-27 13:44:25 -04:00
Brandon Amos
49c71d92d5 Remove trailing whitespace. 2016-06-27 13:13:56 -04:00
Brandon Amos
a8532334cb Merge pull request #19 from VinGarcia/master
Cleaning up bugs and making it more robust
2016-06-27 12:31:47 -04:00
Vinícius Garcia
16193fcf4f Fix all memory leaks =]
There were a memory leak problem when throwing errors.

Now before every throw it makes sure to release all
memory from local structures on functions toRPN() and calculate().
2016-06-26 11:13:28 -03:00
Vinícius Garcia
512dd1a946 Modularize exception management tests 2016-06-26 11:08:53 -03:00
Vinícius Garcia
c62bfa945a Fix a memory leak on Scope::find() 2016-06-26 10:22:50 -03:00
Vinícius Garcia
9ea1e5d3c8 Improve some comments 2016-06-26 10:05:54 -03:00
Vinícius Garcia
4b40f1b6cb Make packToken(TokenBase*) less error prone
packToken(TokenBase*) not only builds a packToken but also
gets responsible for dellocating it, and this is not a normal
class behavior, since it could lead to a error prone code like this:

int main() {
  TokenBase* b = new Token<double>(10, NUM);
  packToken p = b;

    ... some code here ...

  delete b;

    ... some more code ...

  return 0; // Seg fault.
}

This commit makes this function private and explicit,
and only accessible from inside the class calculator which is
now friend of class packToken.

It was done to guarantee that only main developers have need
to fully comprehend this constructor behavior avoiding headaches.
2016-06-26 04:52:29 -03:00
Vinícius Garcia
63030b2aeb Add undefined_operation exception 2016-06-15 23:00:01 -03:00
Vinícius Garcia
865e5e5cdd Fix error message for invalid operands 2016-06-15 13:27:29 -03:00
Vinícius Garcia
8c99ff6222 Fix memory leaking bug on calculator::calculate()
This did not fixed the entire leaking problem,
just the main part of it.
2016-06-15 13:12:28 -03:00
Vinícius Garcia
87cd920682 Add error check for invalid operands 2016-06-14 18:29:10 -03:00
Vinícius Garcia
21565f44cb Add static str() function to packToken 2016-06-05 12:50:54 -03:00
Vinícius Garcia
0d16c842fd Minor fix on error message thrown from toRPN() 2016-06-03 21:35:06 -03:00
Vinícius Garcia
1d8dd499ff Refactor Scope passing for cleanness and efficience
Every time a function was called a new copy of a Scope()
was made, now everything is passed by reference to avoid that
problem.
2016-06-03 21:35:06 -03:00
Vinícius Garcia
33ee3450d7 Add check for operator existence on handle_op()
Now calling: "calculator c("a.[10]") will throw as expected.

(it used to cause a segmentation fault)
2016-06-03 21:35:06 -03:00
Vinícius Garcia
e8edb28fd8 Add syntax error check on calcultor::toRPN()
Now if the expression is like:
- "10 + + -2" or "10 + 2 + +"

A syntax_error is thrown.
2016-06-03 21:35:06 -03:00
Vinícius Garcia
e24a6651fc Update calculator::str() to use packToken::str() 2016-06-03 21:35:06 -03:00
Brandon Amos
99abdb9967 Merge pull request #18 from VinGarcia/simplify
Add Scope concept, and fix some bugs
2016-06-02 17:45:25 -04:00
Vinícius Garcia
ef15c667a1 Fix assignment/copy segmentation fault 2016-06-02 12:57:28 -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
9f6971d0f1 Add default Scope argument to calculators constructor 2016-06-02 12:57:28 -03:00
Vinícius Garcia
60583c22fe Fix memory leak problem 2016-06-02 12:57:28 -03:00
Vinícius Garcia
90ba6bd0ff Add the Scope concept and the Scope Class
Now instead of passing only maps of variables (TokenMap_t*)
to calculator instances you can keep record a Scope hierarchy.

The scope class works as a stack of TokenMap_t*.

When searching for a variable, it searches the
stack on a top-down order and stops as soon as it finds
a match for the search key.

The old API was not modified, everything still works as before,
because the Scope class constructor accepts a TokenMap_t*, so
you can insert a TokenMap_t* anywhere you could use an Scope instance.
2016-06-02 12:57:28 -03:00
Brandon Amos
529fcbe94c Merge pull request #17 from VinGarcia/simplify
Add MAP type and subscript operator: `[]`
2016-04-26 13:09:55 -04:00
Vinícius Garcia
7e178895be Add operator "." for the MAP type 2016-04-25 16:07:57 -03:00