Before this commit it was not possible to create operations
that altered the original value of their operands such as:
- `a = b`
- `a++`
- `a *= 5`
The only one that existed was the assignment operator "=" and it
was hardcoded so no one could alter its behavior.
Now it is possible not only to add new operations of that type
but also to alter the behavior of the default assignemnt "="
operator.
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.
Before this commit the Startup class instances had the same
name of their respective classes which caused some confusion.
Now they use the name "_Startup" instead to make the difference
easier to notice.
This commit also normalizes all the names since the instance
of the functions startup class had a different name than the
others.
Before this commit the packToken::str() function when working
on a MAP would check if the map had a "__str__" attribute,
and if yes it would use it to stringify the MAP.
However, there was no way to do a simillar specialization for
other types such as user defined types.
Now if you add a type specific function for a user defined type
named "__str__" packToken::str() will use it to stringify any
instances of this type.
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.
Just add character parsers for ':' and '.' so that
if someone uses it like: `a:@10` where `@` could be a custom
operator it would still process ':' correctly because
the operator `:@` would not be recognized.
For the "." operator to work it was necessary to convert the token
that came just after it to a string type instead of a variable type.
I.e. `{a: 10} . a` needs to be equivalent to `{a: 10} . 'a'`
This was being done inside the calculator class with
a hard-coded `if`.
Now with the custom parsers available it is not necessary anymore
so it was moved to `builtin-features/reservedWords.h`.
Now if an operator ':' is preceeded by a variable name
it will instead convert this variable to a string token,
so that:
- this `{a: 10}` will be equivalent to `{'a': 10}`
- and this `pow(2, exp:2)` will be equivalent to `pow(2, 'exp':2)`
Thus, this is nothing more than a syntactic sugar.
But this will make it easier to use the keyword arguments feature.
Now it is possible to define your own unary operators in simillar
way to how it was done before for binary operators:
```C++
// Add it to the opPrecedence struct:
opp.addUnary("+", 3); opp.addUnary("-", 3);
// And add a function for handling the operations:
opMap.add({UNARY, ANY_OP, NUM}, &UnaryNumeralOperation);
```
The `UnaryNumeralOperation` function is exactly like the binary
operator's functions, the only difference is that the left token
is useless and of type UNARY.
Note: This commit only adds support for left unary operators and
not right unary operators, i.e. you cant add a `i++` operator yet,
but can add a `++i` operator.
If the user wanted to use a map to define a custom new class
the `default_type()` would always return "map" as the type name.
Now it is possible for the user to add its own type name, e.g.:
```C++
my_type_TokenMap["__type__"] = "my_type_name";
```
This adds a special meaning to any attributes named __type__, but
it is removable if the user prefers, since the code of the
default_type function is on `builtin-function/functions.h`.
Now it is possible to set `packToken::str_custom()`
to a function pointer with a compatible signature:
```C++
std::string (*strFunc_t)(const TokenBase* base, uint32_t nest);
```
This function may have 2 types of return values:
- Empty string: Give up and allow the normal `packToken::str()`
to finish buiding the string.
- Any other string will be used as the
string representation for that Token.
To illustrate this an example was added to:
- `builtin-features/functions.h`
That checks for an "__str__" attribute on every TokenMap
and if it exists and is a function it uses that function
to build the string.
- Add a bool constructor to the packToken
- Add a BOOL case to:
+ packToken::str()
+ packToken::asReal()
+ packToken::asInt()
+ built-in function `type()`
- Add packToken::asBool() function.
- Add tests to check the changes
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.