Skip to content

tco: implements evalFull and trampoline - #9

Merged
kyleterry merged 4 commits into
mainfrom
tco
Apr 15, 2026
Merged

tco: implements evalFull and trampoline#9
kyleterry merged 4 commits into
mainfrom
tco

Conversation

@kyleterry

@kyleterry kyleterry commented Apr 15, 2026

Copy link
Copy Markdown
Owner

These new functions are used to handle deferred evaluation when an expression
returns TailCall. This provides us with tail call optimization. This PR is
lacking proper continuations, but the work for that is ticketed in #4.

goos: linux
goarch: amd64
pkg: go.e64ec.com/glerp
cpu: AMD Ryzen 9 7900X 12-Core Processor
BenchmarkParse-24                        101119             11891 ns/op           25728 B/op        457 allocs/op
BenchmarkTokenize-24                     137892              9037 ns/op           17960 B/op        321 allocs/op
BenchmarkEval/arithmetic-24              460293              2682 ns/op            8948 B/op         72 allocs/op
BenchmarkEval/factorial_10-24            149607              7434 ns/op           13552 B/op        219 allocs/op
BenchmarkEval/fibonacci_15-24              1263            982950 ns/op         1504257 B/op      32575 allocs/op
BenchmarkEval/lambda_closure-24          980734              1275 ns/op            5416 B/op         35 allocs/op
BenchmarkEval/list_operations-24         207481              6176 ns/op           12656 B/op        198 allocs/op
BenchmarkEval/tail_call_1000-24            2466            442444 ns/op          725568 B/op      15043 allocs/op
BenchmarkEval/let_binding-24             355725              3348 ns/op           10720 B/op         99 allocs/op
BenchmarkEnvLookup/shallow-24         150366649             7.978 ns/op               0 B/op          0 allocs/op
BenchmarkEnvLookup/deep_scope-24       14787842             81.36 ns/op               0 B/op          0 allocs/op
BenchmarkNewEnvironment-24                 4377            264467 ns/op          491157 B/op       8563 allocs/op

Closes #2

These new functions are used to handle deferred evaluation when an expression
returns `TailCall`. This provides us with tail call optimization. This PR is
lacking proper continuations, but the work for that is ticketed in #4.
Refactored the TCO implementation to deduplicate evaluation logic and
ensure that tail calls are correctly handled across all evaluation
entry points. This includes the examples and repl main.go.

- deduplicate evalFull and trampoline in tailcall.go
- export EvalFull to allow external packages and embedding apps to
  correctly evaluate tail-recursive expressions
- update macro and syntax-rule expansion to use EvalFull for the
  resulting expressions
- replace all remaining direct .Eval(env) calls with EvalFull across
  the core, CLI, and examples
- other stuff?? I don't think so. enjoy.
@kyleterry
kyleterry merged commit 0c8105e into main Apr 15, 2026
1 check passed
@kyleterry
kyleterry deleted the tco branch April 15, 2026 19:00
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.

implement tail call optimization (tco)

1 participant