bug-make
[Top][All Lists]

## math expressions (was: Re: Tail call elimination)

 From: Paul Smith Subject: math expressions (was: Re: Tail call elimination) Date: Tue, 19 May 2020 12:13:27 -0400

```On Tue, 2020-05-19 at 15:21 +0100, Tim Murphy wrote:
> A question would be do we want to use GMP or are 64 bit ints enough?
> I'm inclined to say ints are ok of they are wide.

Straight 64bit integer arithmetic is fine IMO.  I'm definitely not
interested in adding another library dependency.

> So for me an add function that took signed numbers would be enough if
> it came with < > = since we already have "or" and "and". e.g.
> X:=\$(add \$(X),-1) gives you decrement.
>
> If one is minimal then there is less chance of creating a carbuncle.

No, I don't like this.  Perhaps addition is sufficient for your use-
case but others may need multiplication or division and I certainly
"mult", etc.  I prefer a single mathematical expression function.

These are the things that need to be decided from what I can tell:

Name: I'm using expr here just for convenience.  Is there a better
name?

Whitespace: do we have to put whitespace around every operator, like
expr(1), or is the function smart enough to understand \$(expr 1+1+4)?

Grouping: do we try to implement expression grouping with () (e.g.,
\$(expr (1 + 1) * 4) or is it good enough to just say people need to
nest expr functions: \$(expr \$(expr 1 + 1) * 4)?

Precedence: do we try to implement standard mathematical precedence
(multiplication/division before addition/subtraction) or do we just
evaluate left to right and require explicit grouping?  It would
definitely be a source of ongoing user confusion to not follow normal
precedence rules, but supporting it would make the implementation more
complex.

Increment/decrement: do we try to implement += / -= and/or ++ --?  It
would be something like the symbol on the left would be interpreted as
a variable name, so \$(expr foo += 5) would add 5 to the variable foo
(and expand to the new value of \$(foo)).  If we support ++/-- do we
support both pre- and post- operators?  This isn't needed in places
where you can just say "foo = \$(expr \$(foo) + 5)", but allowing
assignment within an expression could be very helpful.  It could be
done anyway with: \$(eval foo := \$(expr \$(foo) + 5))\$(foo) ... is it
worthwhile to add syntactic sugar for this?

If we support increment/decrement do we go farther and support variable
interpolation a la shell \$((...)), where a symbol is assumed to be the
value of the variable (that is, \$(expr foo + 10) is equivalent to
\$(expr \$(foo) + 10)?  I wouldn't bother implementing this by itself but
it becomes a question if increment/decrement needs it anyway.

```