Repository navigation
Conversation
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.
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".
|
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 |
|
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! |
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.
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).
|
@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 ".".
|
Merged with some rebasing as fd6a1a4. |
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,bb 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: