Commit Graph

25 Commits

Author SHA1 Message Date
Fabian Homborg
2d08b5ee8a [math] Add tests for runtime errors
And fix "isinfinite" - it's called "isinf".
2018-03-07 18:13:26 +01:00
Fabian Homborg
9fc3d1215b [math] Check for runtime errors
When number is infinite, not a number, larger than LONG_MAX or smaller
than LONG_MIN, print a corresponding error and return STATUS_CMD_ERROR.

This should fix the worst of the problems, by at least making them clear.

Fixes #4479.
Fixes #4768.
2018-03-07 18:03:44 +01:00
Fabian Homborg
0da8022081 [math] Fail without arguments
See #4768.
2018-03-01 22:27:24 +01:00
ridiculousfish
b49e1d7703 Minor cleanup
Mark a function as static and use a std::string instead of malloc.
2018-03-01 21:35:49 +01:00
Fabian Homborg
84043e425d [math] Keep LC_NUMERIC=C for output as well
Otherwise, we'd have issues with people putting math's output back
into math because of "," vs ".".
2018-03-01 13:09:35 +01:00
Fabian Homborg
642a1db4fc [math] Add two error strings as fallback 2018-03-01 13:09:35 +01:00
Fabian Homborg
0d9123ec91 [math] Return STATUS_CMD_ERROR when an error occured 2018-03-01 13:09:35 +01:00
Fabian Homborg
d90d0ee08e [tinyexpr] Let specific errors take precedence over generic ones
Fixes the case where `sin()` reported the generic "bogus" error
instead of "too few arguments".

Also rename the constant to "TE_ERROR_UNKNOWN".
2018-03-01 13:09:35 +01:00
Fabian Homborg
4f56ce6d33 [math] Improve error formatting
This looked like

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

Now it looks like

    math: Too many arguments
    'sin(5,4)'
          ^
2018-03-01 13:09:35 +01:00
Fabian Homborg
7b9c75094c [tinyexpr] Add error handling
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".
2018-03-01 13:09:35 +01:00
Fabian Homborg
ce28891c76 [math] Set LC_NUMERIC to C
This allows us to always use "." as radix character, so e.g.

    math 2.5 - 2

is always valid, regardless of locale.
2018-03-01 13:09:35 +01:00
Fabian Homborg
b75c3b968c Replace muparser with tinyexpr 2018-03-01 13:09:35 +01:00
ridiculousfish
11e6cfeb82 [math] Remove exception handling in builtin_math
This handles errors explicitly instead of catching them.
2017-12-18 23:01:16 -08:00
ridiculousfish
91c28449aa [muparser] Parser mathematical functions to return errors instead of throw
Remove exceptions from Parser functions such as sqrt, min, and others.
2017-12-18 23:01:16 -08:00
ridiculousfish
9649b132bd [muparser] Continue adopting ValueOrError 2017-12-18 23:01:15 -08:00
ridiculousfish
5655f255ef [muparser] Add a muParser ValueOrError type
First steps towards removing exceptions from muParser.
2017-12-18 23:01:15 -08:00
ridiculousfish
81dd4a4536 [math] Remove more bare variable support
Prior to this fix, a "bare variable" in math like 'x + 1' would be
looked up in the environment, i.e. equivalent to '$x + 1'. This appears
to have been done for performance. However this breaks the orthogonality
of fish; performance is not a sufficient justification to give math this
level of built-in power, especially because the performance of math is
not a bottleneck. The implementation is also ugly.

Remove this feature so that variables must be prefixed with the dollar
sign and undergo normal variable expansion. Reading 'git grep' output
does not show any uses of this in fish functions or completions.

Also added to changelog.

Fixes #4393
2017-12-17 12:40:09 -08:00
ridiculousfish
95162ef19d Revert "Cache math expressions"
This reverts commit 56d9134534.

An LRU cache in the shell for math seems like overkill.
2017-09-01 00:25:40 -07:00
ridiculousfish
3d40292c00 Switch env_var to using maybe_t
This eliminates the "missing" notion of env_var_t. Instead
env_get returns a maybe_t<env_var_t>, which forces callers to
handle the possibility that the variable is missing.
2017-09-01 00:14:42 -07:00
ridiculousfish
4baada25b9 Use move semantics instead of swap in env_set
This commit backs out certain optimizations around setting environment
variables, and replaces them with move semantics. env_set accepts a
list,  by value, permitting callers to use std::move to transfer
ownership.
2017-08-30 00:59:45 -07:00
Kurtis Rader
56d9134534 Cache math expressions
This implements an LRU cache of recently seen math expressions. When
executing math inside loops and the like this can provide a 33% decrease
in the time to execute the `math` command.
2017-08-24 12:18:39 -07:00
Kurtis Rader
c95b9f06e1 Implement support for bare vars by math
This change allows you to type `math x + 3` rather than `math $x + 3`.

Another step to resolving issue #3157.
2017-08-23 20:41:45 -07:00
Kurtis Rader
b816cd6d50 Change how math rounds integer results
We need our `math` builtin to behave like `bc` with respect to rounding
floating point values to integer to avoid breaking to many existing
uses. So when scale is zero round down to the nearest integer.

Another change for #3157.
2017-08-23 17:31:04 -07:00
Kurtis Rader
24d251ff4b Implement support for multiple math expressions
The MuParser supports the concept of multiple expressions separated by
commas. This implements support for that so that you can do things like
this:

    set results (math '1+1, 4*2, 9^2')
2017-08-23 17:14:54 -07:00
Kurtis Rader
41a7b9457c Implement bare minimum builtin math command
This is the second baby step in resolving #3157. Implement a bare minimum
builtin `math` command. This is solely to ensure that fish can be built
and run in the Travis build environments. This is okay since anyone running
`builtin math` today is already getting an error response.

Also, more work is needed to support bare var references, multiple result
values, etc.
2017-08-23 14:43:45 -07:00