mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
Before this commit you could pass keyword arguments to a function
and it would be accessible inside a variable `kwargs` when the
function was executed.
However, if the keyword argument had the same name as a positional
argument it should instead replace the argument as in Python.
It is now fixed and it is possible to set positional arguments
by name as it was originally designed to be, e.g.:
```C++
// Passing arguments to pow by name:
packToken result = calculator::calculate("pow('exp': 3, 'number': 2)");
std::cout << result << std::endl; // 8
// Works independently from arguments order:
packToken result = calculator::calculate("pow('exp': 3, 'number': 2)");
std::cout << result << std::endl; // 8
```
112 lines
3.0 KiB
C++
112 lines
3.0 KiB
C++
#include <string>
|
|
|
|
#include "./shunting-yard.h"
|
|
#include "./functions.h"
|
|
#include "./shunting-yard-exceptions.h"
|
|
|
|
/* * * * * class Function * * * * */
|
|
packToken Function::call(packToken _this, const 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 positional 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 positional arguments: * * * * */
|
|
|
|
for (; names_it != arg_names.end(); ++names_it) {
|
|
// If not set by a keyword argument:
|
|
auto kw_it = kwargs.map().find(*names_it);
|
|
if (kw_it == kwargs.map().end()) {
|
|
local[*names_it] = packToken::None();
|
|
} else {
|
|
local[*names_it] = kw_it->second;
|
|
kwargs.map().erase(kw_it);
|
|
}
|
|
}
|
|
|
|
/* * * * * Set built-in variables: * * * * */
|
|
|
|
local["this"] = _this;
|
|
local["args"] = arglist;
|
|
local["kwargs"] = kwargs;
|
|
|
|
return func->exec(local);
|
|
}
|
|
|
|
/* * * * * class CppFunction * * * * */
|
|
|
|
CppFunction::CppFunction(packToken (*func)(TokenMap), const args_t args,
|
|
std::string name)
|
|
: func(func), _args(args) {
|
|
this->_name = name;
|
|
}
|
|
|
|
CppFunction::CppFunction(packToken (*func)(TokenMap), unsigned int nargs,
|
|
const char** args, std::string name)
|
|
: func(func) {
|
|
this->_name = name;
|
|
// Add all strings to args list:
|
|
for (uint32_t i = 0; i < nargs; ++i) {
|
|
this->_args.push_back(args[i]);
|
|
}
|
|
}
|
|
|
|
// Build a function with no named args:
|
|
CppFunction::CppFunction(packToken (*func)(TokenMap), std::string name)
|
|
: func(func) {
|
|
this->_name = name;
|
|
}
|