Decouple operations (#30)

* Replace all `scope->find(name)` calls for `scope[name]`

These calls were on the built-in functions declarations, and
were used to recover the function arguments.

This change was made because the find() function was not
the correct tool for the job; The find() function looks
on the entire prototype chain of the scope for the variable,
and this is not needed on this case since the function arguments
will always be on the most local scope of the function.

This refactor makes the code more readable and sightly more
efficient.

* Rename `argsList` to `args_t`

* Convert Function::name from a literal string to a getter function

* Simplify CppFunction constructor

This commit adds a new constructor to CppFunction that expects
a `const args_t` as argument.

This way it is possible to define a new function without having
to explicitly give how many arguments it receives, i.e.:

  CppFunction(&my_func, {"arg1", "arg2", "arg3"}, "my_func");

instead of:

  CppFunction(&my_func, 3, {"arg1", "arg2", "arg3"}, "my_func");

* Add new constructor to undefined_operation

* Major refactor: decouple operations from calculator class

Before this commit every new operation would be hard coded
inside the static calculator::calculate() function.

Now it is not required anymore. To add a new operation do as follow:

```C++
struct my_operation : public Operation {
  // This describes the operands expected by this operation:
  const opID_t getMask() { return build_mask(INT, REAL); }

  // This describes the operation behavior:
  TokenBase* exec(TokenBase* left, std::string op, TokenBase* right) {
    if (/* not satisfy condition */) {
      // Ignore this operation and keep looking for a match:
      return 0;
    }

    if (/* erro condition*/) {
      throw my_error("...");
    }

    // if its all ok:
    TokenBase* result = some_code(left, right);

    // Consume both Tokens:
    delete left;
    delete right;

    // Return:
    return result;
  }
// Don't forget to declare and static instance:
} my_operation;

struct my_Startup {
  my_Startup() {
    // Register the operation on the default opMap:
    opMap_t& opMap = calculator::default_opMap();
    opMap[","].push_back(&my_operation);
    opMap[ANY_OP].push_back(&my_other_operation);
  }
} my_Startup;
```

The default operations are on the operations.cpp file.
To declare your own default operations, just make sure
to not link your code to `operations.o`, and add your
own description of your operations wherever you want.

In future work:

* Make an optional Operation function that works with
  packTokens, so the final user will not need to worry about
  memory management.

* Make it possible to overwrite the default_opMap by extending
  the calculator class, creating any number of different
  calculators as required.

* Add `release` recipe to Makefile

The `make release` will output 2 `.o` files:

- core-shunting-yard.o
- operations.o

The `core` bin contain an working calculator with
no defined operations except for asignment and function call,
that are still hard-coded on the system.

The `operations` bin contain all the other default operations.

If a user wants to define adhoc operations, include only
the `core` binary.

* Add tests for declaring adhoc operations

* Simplify Operation definition

Now there are 2 Operation classes: `BaseOperation` which is the
old class with a new name and the `Operation` that inherits from
the former.

The `exec()` pure virtual function from BaseOperation has the following
signature: TokenBase* exec(TokenBase*, std::string, TokenBase*);

The `exec()` pure virtual function from Operation has the following
signature: packToken exec(packToken, std::string, packToken);

This makes the usage of the Operation class easier since
hiding from the user the required memory management.

Advanced users can declare new operations using the `BaseOperation`
class. Working with TokenBase* is more efficient since
it does not require to call the packToken constructor and
destructor several times.

* Make possible to define adhoc opPrecedence.

Now it is possible to define your own operator precedence map
the same way it is done for operations.

The definition of the operator precedence was now moved to the
file `operations.h` and is initialized just before the operations.

If the user want to define its own operations and precedence map,
he just need to compile his code without the file `operations.o`.
After that he can define his own adhoc behavior in a file on his
project.

It is also possible to overwrite opPrecedence on a child calculator
class. The user just need to overwrite the function "opPrecedence()".

* Update README with new features.
This commit is contained in:
Vinícius Garcia
2016-10-04 14:37:35 -03:00
committed by Brandon Amos
parent 72da211394
commit 9bdba2aa4c
11 changed files with 805 additions and 487 deletions

View File

@@ -25,7 +25,7 @@ TokenMap TokenMap::empty = TokenMap(&default_global());
/* * * * * TokenMap built-in functions * * * * */
const char* map_pop_args[] = {"key", "default"};
const args_t map_pop_args = {"key", "default"};
packToken map_pop(TokenMap scope) {
TokenMap map = scope["this"].asMap();
std::string key = scope["key"].asString();
@@ -53,7 +53,7 @@ packToken map_len(TokenMap scope) {
/* * * * * TokenList built-in functions * * * * */
const char* push_args[] = {"item"};
const args_t push_args = {"item"};
packToken list_push(TokenMap scope) {
packToken* list = scope.find("this");
packToken* token = scope.find("item");
@@ -64,7 +64,7 @@ packToken list_push(TokenMap scope) {
return *list;
}
const char* list_pop_args[] = {"pos"};
const args_t list_pop_args = {"pos"};
packToken list_pop(TokenMap scope) {
TokenList list = scope.find("this")->asList();
packToken* token = scope.find("pos");
@@ -99,12 +99,12 @@ packToken list_len(TokenMap scope) {
struct TokenList::Startup {
Startup() {
TokenMap& base_list = calculator::type_attribute_map()[LIST];
base_list["push"] = CppFunction(list_push, 1, push_args, "push");
base_list["pop"] = CppFunction(list_pop, 1, list_pop_args, "pop");
base_list["push"] = CppFunction(list_push, push_args, "push");
base_list["pop"] = CppFunction(list_pop, list_pop_args, "pop");
base_list["len"] = CppFunction(list_len, "len");
TokenMap& base_map = TokenMap::base_map();
base_map["pop"] = CppFunction(map_pop, 2, map_pop_args, "pop");
base_map["pop"] = CppFunction(map_pop, map_pop_args, "pop");
base_map["len"] = CppFunction(map_len, "len");
}
} list_startup;