tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

Add minus with single argument #119

Closed porky11 closed 7 years ago

porky11 commented 7 years ago

(- 0.0 x) also works instead of (- x) but only works for x as floating point type

porky11 commented 7 years ago

how this could be implemented for every function itself or as a macro like these:

(def - (macro extern (i)
  (* i (minus-one i))))

(def - (macro extern (i)
  (qq - (zero i) i)))

I'd prefer the second macro approach. (zero i) just returns a zero-value for type of i.

tomhrr commented 7 years ago

The *' operator could also be used:

(import cstdio)

(def main (fn extern-c int (void)
  (let ((a int   10)
        (b int16 10)
        (c float 10.0))
    (printf "%d\n" (*' a -1))
    (printf "%d\n" (*' b -1))
    (printf "%f\n" (*' c -1))
    0)))
porky11 commented 7 years ago

But this way, no unary minus can be implemented generally, since -1 cannot be cast to some kind of Vector type or similar

tomhrr commented 7 years ago

*' is overloaded on the first type, such that extra definitions could be added, but it's true that that would be pretty awkward for a type for which unary minus is defined but subtraction is not. Unary minus is now defined over the standard numeric types.