SuperC documentation

⚠️ This is a PROPOSAL DRAFT. Become is currently not implemented. Syntax may change

The become keyword

When a function is called recursively, a stack frame is allocated each time the function is called. So if you call a function 5 times, you will have 5 stack frames allocated, but if you call a function millions of times, you end up with an error because you run out of usable RAM.

The solution was invented decades ago, it’s tail call.

Instead of creating a new stack frame, you can reuse the previous one, so 5 recursive calls will only use 1 stack frame, and millions of recursive calls will use only 1 stack frame.

Use Case 1: Recursion

The following program seems fine, but it will crash because sum overflows the stack with a million of recursive calls.

You can see how it’s solved in the SuperC version.

Use Case 2: Function Forwarding (Dispatchers)

In complex systems like network stacks (e.g., TCP connection parsing) or video game logic (e.g., AI behaviors), developers can cleanly encapsulate every discrete state into its own function. become removes the penalty for doing so.

Use Case 3: Finite State Machines

Because the compiler guarantees that the current stack frame is obsolete when become is executed, arguments passed to the next function are simply shifted into the correct CPU registers. Memory access is entirely bypassed during the state transition.

Notes


Previous

(DRAFT) Generics meta-programming
Playground