tomhrr / dale

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

Extend mfor (or just add new versions) #125

Closed porky11 closed 6 years ago

porky11 commented 7 years ago

mfor should be able to expand to forms not starting with do first-node just would have to be set by the user instead of initialized with (mnfv "do") some other thing, that should be possible is using macros on the list-argument. So lists similar to refines-lists would have to be used. Let's call the first name of this list list the normal mfor-macro would look like this then:

(mfor do str (list "asdf" "zxcv" "qwer")
  (printf "%s\n" str))

This way one could write operations of fix-sized sequences like arrays

(def range (macro extern ((start int) (end int))
  …)) ;;return `(list start (+ start 1) … (- end 1))`

(array-of 3 int (mfor array i (range 0 3)
  (+ (@$ arr1 i) (@$ arr2 i))))

this would sum arr1 and arr2 (which may be arbitrary types) and return an array of 3 ints, which may be easily extended for other sizes or array-like types

tomhrr commented 7 years ago

Thanks for this. There is a new version of mfor, named mfor', that evaluates its list argument as a macro, allowing for the use of arbitrary forms in that place.