Files
cparse/functions.cpp
Vinícius Garcia 9182625ae1 Simplify operations (#38)
* Greatly simplify declaring new operations

Now declaring new operations looks much more like
declaring a new function. The most important difference
is that its arguments are `TokenBase*` instead of `packToken`s.

* Greatly simplify definition of new Operations

Before this commit defining a new Operation would
mean:

1. Extend a class
2. Overwrite a function
3. Manually build a mask
4. Add it to the correct map

Now the process to define a new operations is a lot simpler:

1. Define a function
2. Call the `add()` function to add it to the correct map

Beside this change there is one more simplification: the
signature of the function that describes the operation behavior
now receives `packToken`s as arguments instead of `TokenBase*`.

This means that our users won't have to learn about `TokenBase*`
and won't need to worry about memory management.

An example of the new syntax follows:

```C++
packToken my_operation(const packToken& left, const std::string& op, const packToken& right) {
  return left == right;
}

class myStartup {
  myStartup() {
    opMap_t opMap& = calculator::opMap();
    opMap.add({ANY_TYPE, "==", ANY_TYPE}, &my_operation);
  }
} myStartup;
```

* Simplify RefToken's constructor

This commit simplifies the RefToken's constructor
so that it can accept packTokens instead of TokenBase* as
the `value` argument.

This allows to remove all references of the TokenBase class from
the built-in operations. Making them good examples for our users
to use as template.

* Move all builtin logic to operations.cpp file

* Rename `operations.cpp` to `builtin-operations.cpp`

* Add `make release` as dependency of `make all`

* Fix Travis Build
2016-10-24 21:04:08 -02:00

82 lines
2.1 KiB
C++

#include "./shunting-yard.h"
#include "./functions.h"
#include "./shunting-yard-exceptions.h"
/* * * * * class Function * * * * */
packToken Function::call(packToken _this, Function* func,
TokenList* args, TokenMap scope) {
// Build the local namespace:
TokenMap kwargs;
TokenMap local = scope.getChild();
args_t arg_names = func->args();
TokenList_t::iterator args_it = args->list().begin();
args_t::const_iterator names_it = arg_names.begin();
/* * * * * Parse named arguments: * * * * */
while (args_it != args->list().end() && names_it != arg_names.end()) {
// If the positional argument list is over:
if ((*args_it)->type == STUPLE) break;
// Else add it to the local namespace:
local[*names_it] = *args_it;
++args_it;
++names_it;
}
/* * * * * Parse extra positional arguments: * * * * */
TokenList arglist;
for (; args_it != args->list().end(); ++args_it) {
// If there is a keyword argument:
if ((*args_it)->type == STUPLE) break;
// Else add it to arglist:
arglist.list().push_back(*args_it);
}
/* * * * * Parse keyword arguments: * * * * */
for (; args_it != args->list().end(); ++args_it) {
packToken& arg = *args_it;
if (arg->type != STUPLE) {
throw syntax_error("Positional argument follows keyword argument");
}
STuple* st = static_cast<STuple*>(arg.token());
if (st->list().size() != 2) {
throw syntax_error("Keyword tuples must have exactly 2 items!");
}
if (st->list()[0]->type != STR) {
throw syntax_error("Keyword first argument should be of type string!");
}
// Save it:
std::string key = st->list()[0].asString();
packToken& value = st->list()[1];
kwargs[key] = value;
}
/* * * * * Set missing arguments to None: * * * * */
for (; names_it != arg_names.end(); ++names_it) {
// If not set by a keyword argument:
if (local.map().count(*names_it) == 0) {
local[*names_it] = packToken::None;
}
}
/* * * * * Set built-in variables: * * * * */
local["this"] = _this;
local["args"] = arglist;
local["kwargs"] = kwargs;
return func->exec(local);
}