Skip to content

Replace muparser with tinyexpr - #4736

Closed
faho wants to merge 32 commits into
fish-shell:masterfrom
faho:tinyexpr
Closed

faho wants to merge 32 commits into
fish-shell:masterfrom
faho:tinyexpr

Conversation

@faho

@faho faho commented Feb 19, 2018 •

Copy link
Copy Markdown
Member

Description

This replaces the rather large muparser library with tinyexpr.

The advantages are:

  • tinyexpr is, as the name implies, much smaller than muparser. It's about 700 lines of C

  • tinyexpr's feature set is small enough that we can actually document it all - this means there are no weird hidden features that we don't know about.

  • I'd imagine it's much more palatable for distributions for us to embed 700 lines of something they don't ship anyway, instead of 4000+ lines of something they do.

The disadvantages:

  • This PR is still a bit incomplete - which is why I've left the tests left broken.

  • I had to add error handling myself, and it's still not fantastic - there's a catch-all "bogus" error that is triggered too often (most notably when no argument is passed to a function)

  • It doesn't have min, max or avg functions, and AFAICT adding them would be quite a bit of work, because tinyexpr doesn't have functions with a variable number of arguments.

  • I removed the "list" feature, which meant that in an expression like a,b b was returned. This was triggered in e.g. sin(a,b), which meant it just computed the sine of the last argument. That doesn't look great.

  • It's actual C, and doesn't compile as C++. So I had to add "C" as linker language for the cmake project.

  • It's not added to the autotools build yet.

I'm mostly opening this PR to let others look at what needs to be done, and because I'm not sure that I'm going to have the time to work on this for the rest of the month.

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.md

faho added 15 commits February 10, 2018 19:46
We no longer use muparser, but tinyexpr.

tinyexpr does not have:

- "Statistical functions" like min, max, avg

- Multiple expressions separated with ","
This allows us to always use "." as radix character, so e.g.

    math 2.5 - 2

is always valid, regardless of locale.
[ci skip]
We don't use them, so we don't need them.
This turns a bunch of ifs on their heads.

We often see this pattern in te:

```c
if (s->type != SOME_TYPE) {
   // error handling
}  else {
   // normal code
}
```

Only, since we want to return the first error, we do

```c
if (s->type == SOME_TYPE) {
    // normal code
} else if (s->type != TOK_ERROR) {
    // Add a new error - if it already has type error
    // this should already be handled.
}
```

One big issue is the comma operator, that means arity-1 functions can
take an arbitrary number of arguments. E.g.

```fish
math "sin(5,9)"
```

will return the value of sin for _9_, since this is read as "5 COMMA
9".
This now looks alright, but

- We use the catch-all "bogus" error waaay too much
- `sin(2,5)` still doesn't error out (because of the crappy "comma" operator)
As it turns out, this was, for some reason, actual wanted behavior.

We _don't_ want it, so just remove the code.
This just read comma-separated expressions and returned the last of
them, which is useless and confusing.
This looked like

    math: Error in expression
    'sin(5,4)'
          ^
    Too many arguments

Now it looks like

    math: Too many arguments
    'sin(5,4)'
          ^
These are only available as a customization point. Since we don't use
that, it's dead code.
As it turns out, these are quite important. Without them, even "2 / 2" breaks.
This reverts commit c6f332b.
faho added 3 commits February 21, 2018 21:56
I just miscomputed an offset.
This allows us to simply call files ".cpp" or ".c" and have cmake use
the right linker language per-file.
Fixes the case where `sin()` reported the generic "bogus" error
instead of "too few arguments".

Also rename the constant to "TE_ERROR_UNKNOWN".
@ridiculousfish

Copy link
Copy Markdown
Member

This is looking fantastic! It sounds like you're not going to have time to finish it this month. I think the thing to do is to merge to a branch on fish-shell/fish-shell so that we can collaborate more easily.

@faho

faho commented Feb 25, 2018

Copy link
Copy Markdown
Member Author

Okay, so, unlike what I said, I've had some more time to work on it.

I am now okay with the errors - in particular "sin()" now reports "too few arguments" instead of "expression is bogus". Unknown function names still trigger the catch-all, but I can live with that for now.

I'd love to be able to convert this to c++, but all the void-pointering to functions is making my head hurt, and the solution stackoverflow suggested is std::function, which is like using a nuclear-powered swiss army laser to dice carrots after refracting on mirrors along the moon, venus, beteigeuze and magnification through Bjarne Stroustrup's glasses. It reminds me of Tom And Jerry, is what I'm saying.

So the one thing I'd put as "must-have" is to add this to the autotools build. Well, and to change the tests.

I'm pushing to fish-shell/fish-shell, feel free to work on it!

faho added 7 commits February 26, 2018 11:17
This shows how it's still weird - "2 - " and "max()" return the same
error.
Should fix the build (*knocks on wood*).
Instead of the catch-all, report too few arguments.
This now reports "TOO_MANY_ARGS" instead of no error (and triggering
an assertion).

We might want to add a new error type or report the missing operator
before, but this is okay for now.
faho added 5 commits February 26, 2018 14:28
This otherwise reads uninitialized memory, which I'm told is a big
no-no.
Document all operators, add some more notes, simplify some language,
correct the modulo comment (this does float modulo in the perhaps most
intuitive way).
@faho

faho commented Feb 26, 2018

Copy link
Copy Markdown
Member Author

@ridiculousfish: I think I'm done, actually. If you'd care to take a look (and excuse my idiocy)?

Otherwise, we'd have issues with people putting math's output back
into math because of "," vs ".".
@faho

faho commented Mar 1, 2018

Copy link
Copy Markdown
Member Author

Merged with some rebasing as fd6a1a4.

@faho faho closed this Mar 1, 2018
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
@faho
faho deleted the tinyexpr branch November 26, 2021 11:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants