9 Commits

Author SHA1 Message Date
Vinícius Garcia
5d1fa1b44f Add an easy way to initialize cpase inside main() function
Before this commit cparse operators, functions and reserved words
were initialized at static initialization time and there was
no option to change this.

This could be a problem in some systems because of the
static initialization fiasco:

- https://isocpp.org/wiki/faq/ctors#static-init-order

Now it is possible to initialize cparse inside main() which
sould solve any problems caused by the problem mentioned above.

To do it it's necessary to:

1. Don't add `builtin-features.o` to your compilation files.
2. Add the following code to your `main.cpp` file:

   ```C++
   #include "cparse/builtin-features.inc"

   int main() {
     cparse_startup();

     // ... Your code goes here ...
   }
   ```

This function will initialize all functions normally run at
static initialization time when you include `builtin-features.o`
in your code.
2018-04-12 20:32:54 -03:00
Vinícius Garcia
c1682ad7fc Rename builtin-files/*.h to builtin-files/*.inc
Follow google style guide, cpp files meant for inclusion should
have the `.inc` extension since they are not header files.
2018-04-11 12:22:01 -03:00
Vinícius Garcia
e4557022f7 Explicitly include cmath in functions.h and operations.h
The files:

- builtin-features/functions.h
- builtin-features/operations.h

Would cause an error if they were copied to be customized by a
user of the framework because they depended on the cmath lib
that is not included by the `shunting-yard.h` file.

To make it easier to the user this lib is now #included on both
files.
2018-03-04 00:22:08 -03:00
Vinícius Garcia
3e781ad76c Refactor builtin-features.cpp into multiple files
Before this commit all built-in features were kept not very well
organized inside the `builtin-features.cpp` file.

Now 4 extra files have been created:

1. `builtin-features/functions.h`
2. `builtin-features/operations.h`
3. `builtin-features/reservedWords.h`
4. `builtin-features/typeSpecificFunctions.h`

The `builtin-features.cpp` file still exists but now it only
contains `#includes "..."` of the other 4 files.

This was done to make it easier to understand each file and
easier to find an specific function if required.
2017-03-10 02:06:00 -03:00
Vinícius Garcia
96704a4872 Add some small improvements (#49)
* Add string strip() and string split() functions

* Add list `join()` function.

* Make const version of packToken::token() function

* Add const modifier to Iterable::getIterator() function

* Fix static initialization order bug with packToken::None

Before this commit there was a hidden bug on packToken::None.

If it was called from a static initialized constructor it might
or might not cause a bug.

Now packToken::None is no longe a packToken, but instead a function
that returns a packToken when called.

To adapt old code to conform with this change you should replace
any occurency of `packToken::None` for `packToken::None()`.

* Group together: OppMap_t, rWordMap_t and opMap_t

Now these 3 containers were grouped into a a bigger one: Config_t

This change was done for all the 3 contained configuration data.
And this data had to be passed along on many functions.

Now only 1 argument is required to be passed.

An extra reason is for when a user wants to create his own calculator,
Before this he would need to overwrite 3 virtual functions, one
for each of those configuration containers, now 1 is enough.

The old static variables:

- calculator::default_opPrecedence
- calculator::default_opMap
- calculator::default_rWordMap

Were replaced by the Default static variable:

- Config_t calculator::Default

To access them now it is possible like this:

- calculator::Default().opPrecedence
- calculator::Default().opMap
- calculator::Default().rWordMap

* Small refactor + Add the concept of reserved character

rWordMap_t was replaced by the struct parserMap_t.

This was the small refactor. To add new reserved words the
syntax has changed a little:

```C++
parserMap_t& parser = calculator::Default().parserMap;

parser.add("op", &op_function);

// Before it was like this:
rWordMap_t& rmap = calculator::Default().rWordMap;

rmap["op"] = &op_function;
``

With this change we could add the concept of a reserved character.

To add a reserved character you can do in a very simillar way
to the way you do with reserved words, just make sure to use
simple quotes around the character:

```C++
parserMap_t& parser = calculator::Default().parserMap;

parser.add('=', &assignment);
```

Considerations:

1. Reserved characters work even if the current token contain
   more than one character such the token `=-` on the expression:

   - "a =-10"

   It will consider the minus sign a separated token like: "a = -10".

2. In situations where the current token have meaning such as:

   - "a == 10"

   The operator meaning will have precedence, and the character
   parser will not be called, thus allowing for the correct behavior.

   The same is valid if "==" were a reserved, it also takes precedence
   in relation to the reserved character.

This concept is complicated. However necessary. With this feature
it will be possible to remove the hardcoded operations:

- Assignment: `=`
- Subscripting: `.`
- Bracketing: `(`, `[` and `{`

And write them on `builtin-features.cpp`.

* Fix built-in type casts for `real` and `int`

* Update output of built-in function `type()`

Before this the output for the type REAL was `float`.

Now it will return the string `real` instead, making it more
coherent with the actual name of the type.

* Fix Travis Build
2017-03-09 22:36:39 -03:00
Vinícius Garcia
f49fb05f03 Op Associativity API + Minor fixes, refactors and updates (#45)
* Add error check for TokenList::operator[]

* Add `push` and `pop` functions to TokenList class

Now it is possible to use the TokenList class without accessing
its internal `std::vector<packToken>` as it was before.

* Make possible for functions to work as left operands

Before this commit the calculator would try to execute the
hard-coded operator `()` whenever the left operand was a
function, thus causing an error if the operator was not
in fact the call operator `()`:

```C++
calculator::calculate("pow, 'any thing else'");
// Undefined operation with operands: [Function: pow] and 'any thing else'
```

Now the operator `()` will only be called when it is the actual
operator being called. Thus allowing other operators to work
with functions as their left operand.

* Expose API for setting OP associativity

Before this commit you would register an operator like this:

```C++
OppMap_t& opp = calculator::default_opPrecedence();
opp["+"] = 2;
```

And it was not possible to define the operator associativity.
Now it is possible, for that you just need to define the
operator precedence with a negative sign for Right to Left
precedence, meaning Left to Right otherwise, e.g.:

```C++
OppMap_t& opp = calculator::default_opPrecedence();
opp.add("+", 2);   // Left to right associativity
opp.add("=", -2);  // Right to left associativity
```

* Make `handle_op` to set `lastTokenWasOp` flag

* Add `rpnBuilder::handle_token(TokenBase*)` function

This function is responsible for adding a token to the rpn queue
and also setting the flags `lastTokenWasOp` and `lastTokenWasUnary`
to false. So that our user will not have to handle these flags anymore.

* Simplify `toRPN` detection of the operator `()`
2016-11-28 21:39:22 -02:00
Vinícius Garcia
1336553282 A lot of refactors, some optimizations and {} and [] JSON constructors (#44)
* Simplify `handle_op()` and `handle_unary()` functions

Now both receive an rpnBuilder as argument, instead of receiving
several different variables.

`handle_unary()` now writes to `data->lastTokenWasUnary()` instead
of returning a true/false argument to be written on it outside
the function as it was before. The new return type of the
function is void.

* Join the functions `handle_unary` and `handle_op`

Since they were always called together and did almost the same
thing there was no reason to keep them separated.

* Minor refactor - Move `opPrecedence` into `rpnData`

Now the function `handle_op` requires only 2 arguments:

- const char* expr
- rpnBuilder* data

* Expose `handle_op` to API

`handle_op()` function was exposed to API so that it could be used
on custom reserved word parsers defined by our users.

* Add the builtin map constructor operator: '{}'

Now it is possible to create maps just by writing:

```C++
  calculator::calculate("M = { 'my':'map' }");
```

* Copy `map` and `list` constructors to `objects.*`

These constructors were defined as CppFunctions on
`builtin-features.cpp`, now that we are defining
the implicit constructors for map: `{}` and list: `[]` it
was necessary to define it on someplace the user could not change.

The original code on `builtin-features.cpp` is unchanged. The user
can still overwrite his own definition of the builtin `map` and `list`
functions, as he wish.

* Add list implicit constructor `[1,2,3]`

Now you can declare a new list as you would in JavaScript:

```C++
  calculator::calculate("L = [1,2,3]");
```

* Add logo to repository

* Add move constructor to packToken class

* Move some functions into the rpnBuilder class

The functions:

- `handle_op()`
- `handle_unary()`
- `cleanRPN()`

were moved inside the rpnBuilder class, allowing now to
use `data.handle_op("+")` instead of `handle_op("+", &data)`.

More functions and functionalities should be moved into this class
in future, since we can use it to decouple the rpn building process
from the calculator class.

* Minor refactor: Move open/close bracket code to functions

* Minor refactor - move 2 functions in `builtin-features.cpp`

* Make public the static functions: toRPN and calculate

These 2 functions are useful to be used on the user defined
reserved word parsers, as such they are now available
to be called outside the calculator class.

* Refactor: Decouple internal variables of calculate()

Now the internal variables of the calculate function are
joined together on a struct called "evaluationData".

It can then be exposed for the user to access it in future commits.

* Refactor: Expose calculator variables to Operation::exec()

Now the `Operation::exec` function's signature is:

```C++
  packToken exec(const packToken& left, const packToken& right,
                 const evaluationData* data);
```

The std::string op (that was before this commit the second argument
of the exec() function) is now available inside the evaluationData type.

This struct also contains several other internal variables used
by the calculator::calculate() function. This commit thus allows
more power and flexibility to user Operations.
2016-11-23 15:54:19 -02:00
Vinícius Garcia
46a9ccdc1c Fix bugs with sub-parsers on toRPN() function
Before this commit if a sub-parser left an empty
expression it would cause a segmentation fault.

Also the `LineComment` sub-parser defined on the `builtin-features.cpp`
file would consume the '\n' on the end of the expression, making
it impossible for this character to be used as delimiter by the
calculator. The updated version fixes it.
2016-11-08 11:04:08 -03:00
Vinícius Garcia
a412190cbe Rename builtin-operations.cpp to builtin-features.cpp (#43)
The reason for this change is just because the name `features`
fits a lot better the meaning of this file.

Also this change should be done now, as early as possible
while we still have few users.
2016-11-08 11:57:59 -02:00