Skip to content

Conversation

@murisi
Copy link
Collaborator

@murisi murisi commented Nov 20, 2025

The target branch of this PR implements an interpreter for a Scheme subset. This PR implements a compiler for a variation of that DSL which is easier to compile to C. Specifically, the following has been implemented:

  • A compiler from a Scheme representation using Elixir lists to C code
  • Tail call optimization to guarantee that tail recursion never blows the stack (probably unnecessary because GCC -O3 has been able to detect and optimize all tail calls so far)
  • Moved from using lists of string to lists of symbols to represent Scheme code. Doing this simplified the representation of string literals in the source Scheme programs
  • Implemented variable renaming in order to avoid name clashes and avoid C reserved keywords from being used as variable names in the compiler's C output
  • Anonymous functions (keyword: lambda) are replaced with named functions (keyword: function) to enable (mutually) recursive bindings. Furthermore these functions can be used as top-level definitions.
    • A key limitation of these anonymous functions is that they cannot be used after the function containing it returns because the function pointer essentially becomes a dangling pointer
  • Used lexical scoping instead of dynamic scoping. This is easier to compile because GCC nested functions are also lexically scoped. More generally, lexical scoping is probably easier to compile to fast code because it allows stack frames and variable offsets to be fixed statically.

A way to use the compiler's C output inside RISC Zero is demonstrated at https://github.com/anoma/risc0-scheme . Essentially the C code is copied and pasted into https://github.com/anoma/risc0-scheme/blob/main/methods/guest/src/main.c , though it should be noted that the library functions provided in the compiled environment are slightly different to simplify the compiler outputs.

@murisi murisi requested a review from l4e21 November 20, 2025 16:10
@l4e21 l4e21 force-pushed the jam/feature/elixir-resource-machine branch 5 times, most recently from 2dc0b4c to 60c1de0 Compare November 21, 2025 17:19
@l4e21 l4e21 force-pushed the jam+murisi/feature/elixir-resource-machine branch 3 times, most recently from 123af59 to 77cdb8f Compare November 24, 2025 12:40
@l4e21 l4e21 force-pushed the jam/feature/elixir-resource-machine branch 2 times, most recently from 123af59 to 60c1de0 Compare November 24, 2025 12:45
@l4e21 l4e21 force-pushed the jam+murisi/feature/elixir-resource-machine branch from 1c9bff9 to af1aa31 Compare December 1, 2025 10:56
…ing2' into jam+murisi/feature/elixir-resource-machine
@l4e21 l4e21 force-pushed the jam+murisi/feature/elixir-resource-machine branch from af1aa31 to 751a1a0 Compare December 1, 2025 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants