🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Higher order functions

Published September 07, 2008
Advertisement
As I mentioned a while back, my big goal for R5 of Epoch is first-class function support - specifically, the ability to work with higher-order functions.

So this afternoon I started hacking away on that. The parser recognizes the syntax and sends off alerts to the parse analyzer, which currently ignores them; so there's obviously still some progress to be made there [grin]

On the back end, I'm about halfway done writing the support infrastructure for handling function signatures, which will be a major component to passing functions around. Once that is finished, I just need to write a function pointer system that actually passes the function references between calls on the stack. That should be trivial, as I already have a mechanism that does something similar for reference parameters.

The final piece then will be linking the parser front-end to the back-end on the VM, which is done via the parse analyzer. In brief, the idea is for the analyzer to set up the VM with the requisite metadata so that the running program can (A) pass functions as parameters cleanly and (B) do type validation on those parameters using function signatures.


Once all this is done, we'll see the following program running successfully in Epoch:

//// FCF.EPOCH//// Example of first-class functions and higher-order functions//entrypoint : () -> (){	operate(41, increment)	operate(43, decrement)}increment : (integer(number)) -> (integer(result, 0)){	assign(result(add(number, 1)))}decrement : (integer(number)) -> (integer(result, 0)){	assign(result(subtract(number, 1)))}operate : (integer(number), function op : (integer)->(integer)) -> (){	debugwritestring(cast(string, op(number)))}



Work may or may not get in the way this week, so I have no idea how long it'll be before this is actually finished. Overall though adding higher-order function support has gone a lot more smoothly than I originally guessed.

Combined with some parser refactorings and a couple of minor tweaks to the analyzer, R5 should have a pretty healthy set of improvements.
0 likes 2 comments

Comments

Telastyn
Yeah, getting function passing working was quite smooth for me as well. I'm mildly curious why more languages don't do it (well). If it's simply the result of planning for it from the beginning or some historical momentum ( "if C didn't have it,...").
September 07, 2008 08:40 PM
Kylotan
It seems like a bit of an awkward thing to do if you don't have experience of it. It certainly isn't covered in your typical entry-level parsing/interpreting texts. Things like closures seem to operate in ways that prevent you using some of the usual methods that keep scope handling clean in your typical language parser, for example.
September 17, 2008 05:29 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement