Created
April 12, 2026 20:39
-
-
Save nooga/cc14fa1db818e5e26c83ddd56583cd21 to your computer and use it in GitHub Desktop.
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
| package main | |
| import ( | |
| _ "github.com/nooga/let-go/pkg/compiler" | |
| "github.com/nooga/let-go/pkg/rt" | |
| "github.com/nooga/let-go/pkg/vm" | |
| ) | |
| func aotWrap(f func([]vm.Value) (vm.Value, error)) vm.Value { | |
| w, _ := vm.NativeFnType.Wrap(func(args []vm.Value) (vm.Value, error) { return f(args) }) | |
| return w | |
| } | |
| func lg_fib_direct(arg0 vm.Value) (vm.Value, error) { | |
| var s [4]vm.Value | |
| _ = s | |
| s[0] = arg0 // LOAD_ARG | |
| s[1] = vm.Int(1) // LOAD_CONST 93 | |
| if ai, ok := s[0].(vm.Int); ok { if bi, ok := s[1].(vm.Int); ok { s[0] = vm.Boolean(int64(ai) <= int64(bi)); goto ip_2_cmp_done } } | |
| { r, err := vm.NumLe(s[0], s[1]); if err != nil { panic(err) }; s[0] = vm.Boolean(r) } | |
| ip_2_cmp_done: | |
| if !vm.IsTruthy(s[0]) { goto ip_11 } // BRANCH_FALSE | |
| s[0] = arg0 // LOAD_ARG | |
| goto ip_30 // JUMP | |
| ip_11: | |
| // LOAD_VAR user/fib (elided — direct call) | |
| s[1] = arg0 // LOAD_ARG | |
| s[2] = vm.Int(1) // LOAD_CONST 93 | |
| if ai, ok := s[1].(vm.Int); ok { if bi, ok := s[2].(vm.Int); ok { s[1] = vm.Int(int64(ai) - int64(bi)); goto ip_3_arith_done } } | |
| { r, err := vm.NumSub(s[1], s[2]); if err != nil { panic(err) }; s[1] = r } | |
| ip_3_arith_done: | |
| { | |
| r, err := lg_fib_direct(s[1]) | |
| if err != nil { return vm.NIL, err } | |
| s[0] = r | |
| } | |
| // LOAD_VAR user/fib (elided — direct call) | |
| s[2] = arg0 // LOAD_ARG | |
| s[3] = vm.Int(2) // LOAD_CONST 88 | |
| if ai, ok := s[2].(vm.Int); ok { if bi, ok := s[3].(vm.Int); ok { s[2] = vm.Int(int64(ai) - int64(bi)); goto ip_4_arith_done } } | |
| { r, err := vm.NumSub(s[2], s[3]); if err != nil { panic(err) }; s[2] = r } | |
| ip_4_arith_done: | |
| { | |
| r, err := lg_fib_direct(s[2]) | |
| if err != nil { return vm.NIL, err } | |
| s[1] = r | |
| } | |
| if ai, ok := s[0].(vm.Int); ok { if bi, ok := s[1].(vm.Int); ok { s[0] = vm.Int(int64(ai) + int64(bi)); goto ip_2_arith_done } } | |
| { r, err := vm.NumAdd(s[0], s[1]); if err != nil { panic(err) }; s[0] = r } | |
| ip_2_arith_done: | |
| ip_30: | |
| return s[0], nil // RETURN | |
| } | |
| func lg_fib(args []vm.Value) (vm.Value, error) { | |
| return lg_fib_direct(args[0]) | |
| } | |
| func main() { | |
| _ = rt.CoreNS // ensure rt is imported | |
| var s [3]vm.Value | |
| _ = s | |
| s[0] = rt.NS("user").LookupOrAdd(vm.Symbol("fib")) // LOAD_CONST 1186 | |
| s[1] = aotWrap(lg_fib) // LOAD_CONST 1187 | |
| s[0].(*vm.Var).SetRoot(s[1]) // SET_VAR | |
| s[0] = rt.NS("core").Lookup(vm.Symbol("println")).(*vm.Var).Deref() // LOAD_VAR core/println | |
| // LOAD_VAR user/fib (elided — direct call) | |
| s[2] = vm.Int(25) // LOAD_CONST 1188 | |
| { | |
| r, err := lg_fib_direct(s[2]) | |
| if err != nil { panic(err) } | |
| s[1] = r | |
| } | |
| { | |
| fn, ok := s[0].(vm.Fn) | |
| if !ok { panic("not a function") } | |
| args := []vm.Value{s[1]} | |
| r, err := fn.Invoke(args) | |
| if err != nil { panic(err) } | |
| s[0] = r | |
| } | |
| _ = s[0] // RETURN (main, discarded) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment