mirror of
https://github.com/allankoechke/cparse.git
synced 2026-11-30 17:11:31 +00:00
* 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
82 lines
2.1 KiB
C++
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);
|
|
}
|