Files
five/examples/frb_demo.prg
Charles KWON OhJun 59568f3301 Five v0.9 — Harbour + Go fusion language
- Compiler: PP → Lexer → Parser → Analyzer → Gengo pipeline
- Parser: 232/236 (98%) Harbour compatibility, registry-based dispatch
- RTL: 351 Harbour-compatible functions
- RDD: DBF/NTX/CDX engines with Rushmore bitmap optimization
- Go Interop: IMPORT + pkg.Func() + obj:Method() with FastPath (15M calls/sec)
- HB_FUNC API: Full Harbour C API compatible Go bridge
- Concurrency: SPAWN/LAUNCH/GOROUTINE, <-, WATCH, PARALLEL FOR, ASYNC/AWAIT
- Extensions: Multi-return, DEFER, Slice, f-string, Nil-safe ?:, CONST
- Macro Compiler: Runtime AST parsing and evaluation
- Debugger: TUI debugger with source display, breakpoints, stepping
- FRB: Native + Pcode dual mode runtime binary
- Tests: 13 packages ALL PASS

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 09:41:50 +09:00

103 lines
3.2 KiB
Plaintext

// Five FRB (Five Runtime Binary) Demo
// Shows all FRB capabilities: file-based, in-memory compile, one-shot
// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
//
// Build: five build examples/frb_demo.prg -o frb_demo
// Prep: five frb examples/frb_mathlib.prg -o mathlib.frb
// Run: ./frb_demo
FUNCTION Main()
? "========================================="
? " Five FRB (Five Runtime Binary) Demo"
? "========================================="
? ""
// -----------------------------------------------
// 1. Load pre-compiled FRB from file
// -----------------------------------------------
? "--- 1. File-based FRB ---"
? ""
LOCAL pMath := FrbLoad("mathlib.frb")
IF pMath = NIL
? " (skipped: mathlib.frb not found)"
? " Build it: five frb examples/frb_mathlib.prg -o mathlib.frb"
ELSE
? " Loaded mathlib.frb"
? " CircleArea(5.0) =", FrbDo(pMath, "CIRCLEAREA", 5.0)
? " Fibonacci(10) =", FrbDo(pMath, "FIBONACCI", 10)
? " IsPrime(97) =", FrbDo(pMath, "ISPRIME", 97)
FrbUnload(pMath)
? " Unloaded."
ENDIF
? ""
// -----------------------------------------------
// 2. Compile PRG source at runtime (in-memory)
// -----------------------------------------------
? "--- 2. In-Memory Compilation ---"
? ""
LOCAL cSource := ;
'FUNCTION Reverse(cStr)' + Chr(10) + ;
' LOCAL i, cResult := ""' + Chr(10) + ;
' FOR i := Len(cStr) TO 1 STEP -1' + Chr(10) + ;
' cResult += SubStr(cStr, i, 1)' + Chr(10) + ;
' NEXT' + Chr(10) + ;
' RETURN cResult' + Chr(10) + ;
'FUNCTION Repeat(cStr, n)' + Chr(10) + ;
' RETURN Replicate(cStr, n)' + Chr(10)
? " Compiling PRG source at runtime..."
LOCAL pStr := FrbCompile(cSource)
IF pStr != NIL
? " Reverse('Hello') =", FrbDo(pStr, "REVERSE", "Hello")
? " Repeat('Go!', 3) =", FrbDo(pStr, "REPEAT", "Go!", 3)
FrbUnload(pStr)
? " Unloaded."
ELSE
? " ERROR: Compile failed"
ENDIF
? ""
// -----------------------------------------------
// 3. One-shot: compile + run + unload
// -----------------------------------------------
? "--- 3. One-Shot FrbExec ---"
? ""
LOCAL cProgram := ;
'FUNCTION Main()' + Chr(10) + ;
' LOCAL i, nSum := 0' + Chr(10) + ;
' FOR i := 1 TO 100' + Chr(10) + ;
' nSum += i' + Chr(10) + ;
' NEXT' + Chr(10) + ;
' RETURN nSum' + Chr(10)
? " Sum of 1..100 =", FrbExec(cProgram)
? ""
// -----------------------------------------------
// 4. Dynamic code with goroutine
// -----------------------------------------------
? "--- 4. Dynamic Code + Goroutine ---"
? ""
LOCAL cAsync := ;
'FUNCTION Worker(ch, n)' + Chr(10) + ;
' ChSend(ch, n * n)' + Chr(10) + ;
' RETURN NIL' + Chr(10)
LOCAL pAsync := FrbCompile(cAsync)
IF pAsync != NIL
LOCAL ch := Channel(1)
Go("WORKER", ch, 7)
? " 7^2 from dynamic goroutine =", ChReceive(ch)
FrbUnload(pAsync)
ENDIF
? ""
? "========================================="
? " Done! PRG code compiled at runtime"
? " and executed at native Go speed."
? "========================================="
RETURN NIL