Skip to content

Instantly share code, notes, and snippets.

@tom-huntington
Last active October 21, 2021 04:51
Show Gist options
  • Save tom-huntington/fb912df4d5f9f1239cffc12d62b75c27 to your computer and use it in GitHub Desktop.
Save tom-huntington/fb912df4d5f9f1239cffc12d62b75c27 to your computer and use it in GitHub Desktop.
Meta Macros in Porth

As the macro system has become so fundamental to Porth, Alexey sould double down with meta macros.

Meta macros would expand generators, providing similar functionality to while loops but for macros.

New keywords gen and meta would be introduced, and intrinsic generator iota:

gen enum_names A B C end

meta 
macro enum_names iota end
end

would expand to the code:

macro A 0 end
macro B 1 end
macro C 2 end

This would allow also you to define structures. For example, we will rewrite this structure in the meta macro system

macro fd mem end
macro statbuf fd 8 + end
macro content statbuf sizeof(stat) + end
macro line content sizeof(Str) + end

// meta macro rewrite

gen fields fd statbuf content line end
gen fields0 mem fields end
gen types void ptr stat Str end

meta
macro fields fields0 sizeof({types}) + end
end

Of course you would also have to define

macro sizeof(void) 0 end
macro sizeof(ptr) 8 end

Maybe it would be best to rename the gen keyword fields, and the meta keyword struct.


Also please rerwite collapse OS in porth!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment