24 Commits

Author SHA1 Message Date
68f9920e88 Update typeSpecificFunctions.inc
Added .contains(...) method for lists and maps
2025-06-09 23:51:53 +03:00
Vinícius Garcia
1f85d973c0 Small improvement on Pan-lj changes 2025-06-08 23:03:19 -03:00
longjiang.pan
06bb52a520 fix segment fault when multi-threading 2025-04-27 17:09:12 +08:00
Enno Rehling
808d12a608 fix clang compilation 2022-11-14 05:53:34 +01:00
Enno Rehling
d8fba93052 missing include 2022-11-13 21:16:50 +01:00
Enno Rehling
2702455fd1 add warning for signed/unsigned comparison.
fix broken negative indexing flagged by tests.
2022-11-13 16:50:16 +01:00
Enno Rehling
a29e7acc1d avoid MSVC warnings 2022-11-13 16:33:07 +01:00
tom_straub
f0dd1cdccd Operator & | ^ added and updated to C++20 precedence 2022-10-31 16:10:08 -03:00
Vinícius Garcia
11ff91ca0c Add unary not operation to builtin operations 2021-10-04 09:11:57 -03:00
Vinícius Garcia
89f9892b37 Make LValues accessible for custom operations
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.
2018-05-17 14:39:35 -03:00
Vinícius Garcia
5d1fa1b44f Add an easy way to initialize cpase inside main() function
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.
2018-04-12 20:32:54 -03:00
Vinícius Garcia
c1682ad7fc Rename builtin-files/*.h to builtin-files/*.inc
Follow google style guide, cpp files meant for inclusion should
have the `.inc` extension since they are not header files.
2018-04-11 12:22:01 -03:00
Vinícius Garcia
2e1e3613fb Improve readability of builtin-features/* files
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.
2018-04-11 12:19:15 -03:00
Vinícius Garcia
3bf5657cf3 Improve builtin packToken::str_custom() implementation
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.
2018-03-04 09:34:57 -03:00
Vinícius Garcia
e4557022f7 Explicitly include cmath in functions.h and operations.h
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.
2018-03-04 00:22:08 -03:00
Vinícius Garcia
3989eb7390 Add redundancies for custom operator parsers ":" and "."
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.
2017-12-10 09:42:15 -02:00
Vinícius Garcia
14e2592686 Move hard-coded parsing of "." operator to custom parser
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`.
2017-12-10 09:30:00 -02:00
Vinícius Garcia
4dea80c5f7 Add custom parser for operator ':' for e.g. {a:10}
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.
2017-12-09 23:08:02 -02:00
Vinícius Garcia
2b4fa5f3b3 Add support for custom unary operators
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.
2017-11-14 18:53:45 -02:00
Vinícius Garcia
ce0df4153e Add a special meaning to attribute __type__ of maps
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`.
2017-05-14 03:05:10 -03:00
Vinícius Garcia
5bccca9a43 Expose an API for customizing packToken::str()
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.
2017-05-14 01:55:06 -03:00
Vinícius Garcia
ae1243c291 Minor fix on Startup class instance's name 2017-05-14 01:54:09 -03:00
Vinícius Garcia
985fe62019 Add type BOOL and related operations
- 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
2017-05-09 08:30:41 -03:00
Vinícius Garcia
3e781ad76c Refactor builtin-features.cpp into multiple files
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.
2017-03-10 02:06:00 -03:00