Commit graph

23 commits

Author SHA1 Message Date
Bruno BELANYI 8201523d8b project: add MIT license 2020-12-12 15:31:15 +01:00
Bruno BELANYI 77a7bdfddd evalexpr: parse: do not use operator kind directly
This allows for having different tokens mapping to the same mathematical
operator, with potentially different semantics. For example, we can add
`$` as another notation for factorial, but allowing it to be chained:
meaning we can evaluate `3$$` to `720`, and still keep `3!!` as a syntax
error.

To do so, we simply need to add the following line to our operator
table:

```c
POSTOP(UNOP_FACT, 5, ASSOC_LEFT, '$', 0)
```
2020-11-02 16:13:33 +01:00
Bruno BELANYI da868be598 evalexpr: parse: parse all multi-char operators 2020-11-02 16:13:33 +01:00
Bruno BELANYI b12c3562ec evalexpr: parse: parse multi-char prefix operators 2020-11-02 16:13:33 +01:00
Bruno BELANYI 10c7a96deb evalexpr: parse: prepare for multi-char operators 2020-11-02 16:13:33 +01:00
Bruno BELANYI 0ac03f94d0 evalexpr: tests: reduce repetition using X macros 2020-11-02 16:13:33 +01:00
Bruno BELANYI d82bf6e77b evalexpr: parse: simplify operators table 2020-11-02 16:13:33 +01:00
Bruno BELANYI 793a6c6f6f evalexpr: parse: reorder operator table
This change, by not breaking anything, shows that the order of the table
*probably* does not matter to the parsing code.

This also enables a future refactoring to only use the parts of the
tables that are *actually* needed, such as isolating the prefix, infix,
or postfix operators when using the table to check a subset of them
2020-11-02 16:13:33 +01:00
Bruno BELANYI 3b51111ff5 evalexpr: parse: clean-up character conversion
Differentiate the postfix and prefix cases to allow overlap in operators
used for each kind of operation
2020-11-02 16:13:33 +01:00
Bruno BELANYI 44bf3d98af evalexpr: parse: clean-up using new enum
Instead of using characters for the different precedence related
functions, use the actual operator value.
2020-11-02 16:13:33 +01:00
Bruno BELANYI f7c8413e3c evalexpr: introduce single op_kind enum
This will allow making the precendence climbing cleaner
2020-11-02 16:13:33 +01:00
Bruno BELANYI 32d17134cd evalexpr: parse: parse operand more correctly
I noted that the `right_prec` and `next_prec` functions taking a
character only work because prefix operators come after infix operators
in the table. We should compare the *actual* operator kind, which would
probably need a single operator enum to make sure there are no
collisions.
2020-11-02 15:48:30 +01:00
Bruno BELANYI b5912f508e evalexpr: parse: improve precedence climbing
The previous version of the algorithm did not take into account the fact
that the main loop should only run while we have postfix or infix
operators. This happened to work because of the small amount of
operators used in the grammar.

The previous version also had prefix operators hard-coded in the operand
parsing function.
2020-11-02 15:48:30 +01:00
Bruno BELANYI 88b7f50a3a evalexpr: parse: use single 'ops' table 2020-11-02 15:48:30 +01:00
Bruno BELANYI 81f7d30ddd ci: add Drone CI 2020-11-02 15:48:30 +01:00
Bruno BELANYI 665e05401f git: ignore testsuite binary 2020-11-02 15:48:30 +01:00
Bruno BELANYI f9719e9c9b evalexpr: tests: add Criterion testsuite 2020-11-02 15:48:30 +01:00
Bruno BELANYI b5b2744a1e git: ignore evalexpr binary 2020-11-02 15:48:30 +01:00
Bruno BELANYI 23c94b1d06 doc: add README 2020-11-02 15:48:30 +01:00
Bruno BELANYI 92ae5e3ab9 evalexpr: allow choosing parser at build time 2020-11-02 15:48:30 +01:00
Bruno BELANYI 9a6da7b19a evalexpr: parse: add precedence climbing parser 2020-11-02 15:48:29 +01:00
Bruno BELANYI ad56ea521a evalexpr: parse: rename recursive parsing function 2020-11-02 15:48:29 +01:00
Bruno BELANYI a92e39dbec evalexpr: initial recursive descent parser 2020-10-28 15:40:57 +01:00