🎉 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!

Some Epoch shtuffs

Published September 08, 2008
Advertisement
There's still a handful of TODOs left sprinkled around the code, but higher-order functions are now in place. This makes it possible to pass a function to another function, and... ahh, screw it, here's an example:

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)))}


The increment function is applied to the value 41, which is then written out to the debugger by the operate function. Something similar then happens with decrement and the value 43.

The result, of course, is a shiny little "42/42" output in the debugger window.


Besides being just a plain damn handy feature by itself, higher-order function support will make it trivial to pass functions to external APIs. This means that, shortly, it will be possible to have a callback from an external API into Epoch code, via the marshaling interface of course.

I still have a lot of technical decisions to make about how exactly callbacks will work, but that's a ways down the road; I have to get higher-order functions totally polished off first.

Notably missing is the ability to statically validate a function to make sure it is acceptable. For example, in the above demo, the operate function expects as its second parameter a function that maps an integer onto another integer. Right now, I could pass a function that, say, maps an integer onto null; this would trash the VM's stack and cause chaos.

It's straightforward (but not exactly simple) to validate the function call statically, so that I can't do evil shenanigans like that. Well, to be honest, it is pretty simple - I'm 80% of the way there already - but it requires some refactoring of the way I handle function call validation in general, and I'm too lazy to mess with that right away.



So for now I'll be content with my "42/42" demo program, and we'll tackle static validation another day. I have a feeling there's generally a lot of miscellaneous type work I can do to the VM, so I'll probably sit down and have a hardcore cleanup session sometime in the near future.
0 likes 1 comments

Comments

rip-off
Very nice. This is something I miss most when I go down to lower level languages (even when it is possible with some nasty hackery, *cough* C++ *cough*).
September 09, 2008 08:24 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement