rtulip / haystack

Haystack is a compiled, statically typed, stack-based language with opt-in variable assignment.
MIT License
25 stars 2 forks source link

Function pointers #194

Open rtulip opened 1 year ago

rtulip commented 1 year ago

Right now, there's no way to pass functions as parameters on the stack.

Semantics:

We should be able to use the existing unary & expression to push a function pointer on the stack:

fn foo() -> [u64] 12345

fn main() {
    &foo // pushes fn() -> [u64] onto stack.
}

Calling can be done by "de-referencing" the function pointer:

fn main() {
    &foo @ println
}

As a type, it can be represented by something like:

fn for_each(&Vec<T>: v fn(T) -> []: func) {
    0 while dup v Vec.get dup Option.take_is_some do {
        Option.unwrap func @
        1 +
    } drop drop
}