compiler: add then/2 and tap/2 support for functional pipelines#119
Merged
compiler: add then/2 and tap/2 support for functional pipelines#119
Conversation
Add support for Elixir's then/2 and tap/2 functions in the compiler,
enabling functional pipeline patterns in WASM-compiled code:
- then/2: apply anonymous function to a value and return the result
Compiles to a let-binding in IR: value |> then(fn x -> x * 2 end)
becomes {:block, [{:let, tmp, value_ir}, body_ir]}
- tap/2: apply function for side-effect, return original value
In the pure WASM subset (no side effects), tap simply returns
the original value, allowing tap/2 calls in pipelines to compile
Both local call syntax (then/tap) and Kernel remote call syntax
(Kernel.then/Kernel.tap) are supported. Works with pipe operator
for idiomatic Elixir pipeline chains.
Includes 14 tests covering:
- Basic then/2 and tap/2 compilation
- Pipeline chaining (value |> then(...) |> then(...))
- Mixed tap/then pipelines
- Multi-expression function bodies
- Kernel.then/2 and Kernel.tap/2 remote call syntax
- WASM execution correctness
- Optimization pass interaction (constant folding through then)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add support for Elixir's
then/2andtap/2functions in the Firebird compiler, enabling idiomatic functional pipeline patterns in WASM-compiled code.Changes
then/2— Transform values in pipelinesCompiles to efficient let-bindings in IR, then optimized WASM locals.
tap/2— Pass-through in pipelinesSince the WASM subset is pure (no side effects),
tap/2compiles to an identity — the function body is dead code. This allowstap/2calls in pipelines to compile cleanly.Supported syntax
then(value, fn x -> body end),tap(value, fn x -> body end)Kernel.then(value, fn x -> body end),Kernel.tap(value, fn x -> body end)value |> then(fn x -> body end),value |> tap(fn x -> body end)Tests
14 new tests covering compilation, WASM execution correctness, pipeline chaining, Kernel remote syntax, and optimization pass interaction.