1
0
Fork 0
forked from fun/fun

Added ./scripts/play.fun to interactive run thrue all examples in ./examples/. (0.40.6)

This commit is contained in:
Johannes Findeisen 2026-04-21 01:04:39 +02:00
commit 5eb5cd7348
10 changed files with 275 additions and 141 deletions

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - Building Fun
title: Building Fun
subtitle: How to build Fun with CMake, available targets, and build options (FUN_DEBUG, FUN_USE_MUSL, FUN_WITH_CPP, FUN_WITH_RUST, FUN_WITH_OPENSSL).
description: How to build Fun with CMake, available targets, and build options (FUN_DEBUG, FUN_USE_MUSL, FUN_WITH_CPP, FUN_WITH_RUST, FUN_WITH_OPENSSL).
permalink: /documentation/build/
@ -27,11 +27,13 @@ tags:
This guide describes how to build Fun from source using CMake and the available build options.
## Prerequisites
- A C compiler with C99 support
- CMake 3.20+ (or newer)
- Optional: Rust toolchain with cargo (required when building with `FUN_WITH_RUST=ON`)
## Common targets
- `build` - aggregate target that depends on `fun`, `fun_test`, and `test_opcodes`
- `fun` - the CLI executable
- `fun_test` - unit/feature tests (run with CTest)
@ -40,6 +42,7 @@ This guide describes how to build Fun from source using CMake and the available
These targets are defined by the project; use your configured CMake build directory/profile.
## Build options
Fun exposes several options you can toggle at configure time:
- `FUN_DEBUG` (ON/OFF) - Enables extra assertions and logging in the VM and runtime
@ -49,6 +52,7 @@ Fun exposes several options you can toggle at configure time:
- `FUN_WITH_OPENSSL` (ON/OFF) - Enable OpenSSL-backed helpers (MD5/SHA-256/SHA-512/RIPEMD-160)
### VM configuration constants
You can override internal VM limits at compile time by passing `-D<VAR>=<VALUE>` to CMake:
- `MAX_FRAMES` (default: 128) - Maximum depth of the call stack (frames)
@ -61,45 +65,48 @@ These are defined as `CACHE` variables, so they will persist in your `CMakeCache
When configuring, the build prints a summary like:
See [../vm/](../vm/) for more information.
See [VM](../vm/) for more information.
<pre>==== Fun build options ====
FUN_DEBUG: ENABLED|DISABLED
FUN_USE_MUSL: ENABLED|DISABLED
FUN_WITH_CPP: ENABLED|DISABLED
FUN_WITH_RUST: ENABLED|DISABLED
===========================
</pre>
===========================</pre>
## Example commands
Use the CLion-provided build directories or your own. Typical invocations:
### Debug
<pre>cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug \
-DFUN_DEBUG=ON -DFUN_WITH_RUST=OFF
cmake --build build --target build
</pre>
cmake --build build --target build</pre>
### Release
<pre>cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release \
-DFUN_DEBUG=OFF -DFUN_WITH_RUST=OFF
cmake --build build_release --target build
</pre>
cmake --build build_release --target build</pre>
### Enabling optional extensions
<pre>cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release \
-DFUN_WITH_CPP=ON -DFUN_WITH_RUST=ON -DFUN_WITH_OPENSSL=ON
cmake --build build_release --target build
</pre>
cmake --build build_release --target build</pre>
### Customizing VM limits
<pre>cmake -S . -B build_custom -DSTACK_SIZE=4096 -DMAX_GLOBALS=512
cmake --build build_custom --target fun
</pre>
cmake --build build_custom --target fun</pre>
If `FUN_WITH_RUST` is enabled, ensure `cargo` is available in PATH; the build will invoke it and link the produced static library.
If `FUN_WITH_OPENSSL` is enabled, CMake must detect your system OpenSSL (libcrypto).
## Running
- CLI: run the `fun` executable from your build directory.
- REPL: `fun -i` or just run `fun` without a script, depending on your CLI version (see [../cli/](../cli/)).
- Examples: see [../examples/](../examples/).
- REPL: `fun -i` or just run `fun` without a script, depending on your CLI version (see [CLI](../cli/)).
- Examples: see [Examples](../examples/).
Tip: When running from the repository without installation, set `FUN_LIB_DIR` to the local `./lib` so includes can find the stdlib.

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - Running the Examples
title: Running the Examples
subtitle: How to run the examples and the interactive showcase script, with environment tips.
description: How to run the examples and the interactive showcase script, with environment tips.
permalink: /documentation/examples/
@ -21,7 +21,6 @@ tags:
- tips
---
This page shows how to run the example programs included with the repository and how to use the interactive showcase script.
All commands assume you are in the repository root.
@ -33,33 +32,33 @@ All commands assume you are in the repository root.
Example (Linux/macOS/BSD):
<pre>FUN_LIB_DIR="$(pwd)/lib" ./build/fun examples/include_lib.fun
</pre>
<pre>FUN_LIB_DIR="$(pwd)/lib" ./build/fun examples/include_lib.fun</pre>
Windows (PowerShell):
<pre>$env:FUN_LIB_DIR = "$PWD/lib"
./build/fun.exe .\examples\include_lib.fun
</pre>
./build/fun.exe .\examples\include_lib.fun</pre>
## Interactive showcase: play.fun
The script `./play.fun` discovers all `.fun` files under `./examples` and offers to run them one by one:
The script `./scripts/play.fun` discovers all `.fun` files under `./examples` and offers to run them one by one:
<pre>./scripts/play.fun</pre>
<pre>./play.fun
</pre>
Notes:
- The script auto-picks your interpreter (FUN_BIN env or `fun` in PATH) and ensures `FUN_LIB_DIR=./lib` so examples resolve includes correctly.
- It shows the exit code for each run and summarizes failures at the end.
Tip: you can run specific examples directly too:
<pre>FUN_LIB_DIR="$(pwd)/lib" fun examples/crypto/openssl_md5.fun
</pre>
<pre>FUN_LIB_DIR="$(pwd)/lib" fun examples/crypto/openssl_md5.fun</pre>
## Example categories
Browse the `examples/` tree for areas of interest:
- crypto — crypto demonstrations (e.g., OpenSSL MD5/SHA-256/SHA-512 helpers; requires build with `-DFUN_WITH_OPENSSL=ON`)
- crypto — crypto demonstrations (e.g., OpenSSL MD5/SHA-256/SHA-512/RIPEMD160 helpers; requires build with `-DFUN_WITH_OPENSSL=ON`)
- blocking / interactive — I/O or user-interactive patterns
- error / broken — negative tests and error showcases
- math — numeric operations
@ -70,6 +69,6 @@ Browse the `examples/` tree for areas of interest:
Place your `.fun` files anywhere under `examples/` to have them picked up by `play.fun`. Use quoted includes for project-local helpers and angle brackets for stdlib modules:
<pre>#include "examples/my_lib/common.fun"
#include <io/console.fun>
</pre>
#include <io/console.fun></pre>
If you add an example showcasing a new feature, also consider adding a brief note to the relevant doc (types.md, includes.md, opcodes.md, etc.).

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - FAQ
title: FAQ
subtitle: Frequently asked questions and quick answers.
description: Frequently asked questions and quick answers.
permalink: /documentation/faq/
@ -23,22 +23,22 @@ Answers to common questions.
## I built Fun but includes aren't found
Set `FUN_LIB_DIR` to the repository's `./lib` directory when running without installation:
<pre>FUN_LIB_DIR=./lib ./build/fun examples/hello.fun
</pre>
See [../includes/](../includes/).
<pre>FUN_LIB_DIR=./lib ./build/fun examples/hello.fun</pre>
See [Includes](../includes/).
## How do I start the REPL?
Run `fun -i` (or run `fun` without a script, depending on version). See [../repl/](../repl/).
Run `fun -i` (or run `fun` without a script, depending on version). See [REPL](../repl/).
## Which build target should I use?
Use the aggregate `build` target to build `fun`, `fun_test`, and `test_opcodes`. See [../build/](../build/).
Use the aggregate `build` target to build `fun`, `fun_test`, and `test_opcodes`. See [Build](../build/).
## Where are the standard libraries?
Under [https://git.xw3.org/fun/fun/src/branch/main/lib](https://git.xw3.org/fun/fun/src/branch/main/lib){:class="git"}. See [../stdlib/](../stdlib/) for an overview.
Under [https://git.xw3.org/fun/fun/src/branch/main/lib](https://git.xw3.org/fun/fun/src/branch/main/lib){:class="git"}. See [stdlib](../stdlib/) for an overview.
## Where can I find internals and opcodes?
Browse [https://git.xw3.org/fun/fun/src/branch/main/src/vm](https://git.xw3.org/fun/fun/src/branch/main/src/vm/) and [../internals/](../internals/) / [../opcodes/](../opcodes/).
Browse [https://git.xw3.org/fun/fun/src/branch/main/src/vm](https://git.xw3.org/fun/fun/src/branch/main/src/vm/) and [Internals](../internals/) / [Opcodes](../opcodes/).

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - Fun VM Opcodes Overview
title: VM Opcodes Overview
subtitle: VM opcodes overview grouped by domain with brief behavior/stack notes.
description: VM opcodes overview grouped by domain with brief behavior/stack notes.
permalink: /documentation/opcodes/
@ -176,8 +176,6 @@ This document provides an overview of the available VM opcodes implemented under
- OP_SQLITE_EXEC: Execute statement; pops handle:int, sql:string; pushes rc:int (0=OK).
- OP_SQLITE_QUERY: Run query; pops handle:int, sql:string; pushes array<map<string,any>>.
<!-- libSQL opcodes removed -->
## OS, Time, Processes, Threads, Sockets, Serial
- OP_ENV: Get environment variable; pops key:string; pushes value:string or Nil.
@ -243,35 +241,6 @@ This document provides an overview of the available VM opcodes implemented under
- OP_PCSC_DISCONNECT: Disconnect; pops handle; pushes 1/0.
- OP_PCSC_RELEASE: Release context; pops scope/id; pushes 1/0.
## Notcurses (Terminal UI)
- OP_NC_INIT: Initialize notcurses; pushes handle or 0.
- OP_NC_SHUTDOWN: Shutdown; no args; pushes 1/0.
- OP_NC_CLEAR: Clear screen; pushes 1/0.
- OP_NC_DRAW_TEXT: Draw text at (x,y); pops text, x, y; pushes 1/0.
- OP_NC_GETCH: Get key with timeout; pops timeout_ms:int; pushes int key or -1.
- OP_NC_GET_SIZE: Get stdplane size; pushes [rows:int, cols:int] or -1 if unavailable.
- OP_NC_SET_STYLE: Set stdplane style/colors; pops style:int, bg_rgb:int, fg_rgb:int; pushes 0/-1.
- OP_NC_DRAW_CHAR: Draw a single codepoint at y,x; pops ch:int, x:int, y:int; pushes 0/-1.
- OP_NC_DRAW_HLINE: Draw horizontal line; pops len:int, x:int, y:int, ch:int; pushes 0/-1.
- OP_NC_DRAW_VLINE: Draw vertical line; pops len:int, x:int, y:int, ch:int; pushes 0/-1.
- OP_NC_BOX: Draw a rectangular box; pops x:int, y:int, w:int, h:int, style:int; pushes 0/-1.
- OP_NC_FILL: Fill rectangle with codepoint; pops x:int, y:int, w:int, h:int, ch:int; pushes 0/-1.
- OP_NC_RENDER: Force a render; pushes 0/-1.
<!-- libSQL backend removed -->
## TK (Tcl/Tk UI)
- OP_TK_EVAL: Evaluate Tcl code; pops text:string; pushes result string or error.
- OP_TK_LABEL: Create/update label; pops text, id; pushes 1/0.
- OP_TK_BUTTON: Create/update button; pops text, id; pushes 1/0.
- OP_TK_PACK: Pack widget; pops id; pushes 1/0.
- OP_TK_BIND: Bind event; pops command, event, id; pushes 1/0.
- OP_TK_WM_TITLE: Set window title; pops title; pushes 1/0.
- OP_TK_LOOP: Enter main event loop; no args; blocks until exit.
- OP_TK_RESULT: Retrieve last Tcl result; pushes string.
## Miscellaneous
- OP_KEYS / OP_VALUES: Map utilities (see Maps).

View file

@ -17,6 +17,9 @@ tags:
- history
- launch
- tips
- repl
- cli
- fun_with_repl
---
This document describes the interactive ReadEvalPrint Loop (REPL) for the Fun programming language: how to build/launch it, how input and execution work, line editing and completion, the REPL buffer workflow, commands, debugging helpers, and tips.
@ -95,10 +98,10 @@ History is persisted in a file named .fun_history in your home directory (HOME/U
Tab provides two kinds of completions:
1) :load path completion
- :load path completion
- When typing a :load command, Tab completes filesystem paths, including directories. A trailing slash is handled as expected. Completion attempts to compute common suffixes across candidates.
2) Standard library identifier completion
- Standard library identifier completion
- For general input, Tab attempts to complete identifiers from the standard library symbols scanned from FUN_LIB_DIR (or DEFAULT_LIB_DIR/lib). If there are multiple matches, a menu of candidates is printed; otherwise the identifier is completed in place.
If Tab is pressed in the middle of the line (not at end), the REPL beeps instead of completing.
@ -107,115 +110,115 @@ If Tab is pressed in the middle of the line (not at end), the REPL beeps instead
You typically build code incrementally:
1) Type lines; they accumulate in an internal buffer.
2) Press Enter on a blank line to parse and execute the buffer.
3) Output is printed and the buffer is cleared.
- Type lines; they accumulate in an internal buffer.
- Press Enter on a blank line to parse and execute the buffer.
- Output is printed and the buffer is cleared.
Alternatively, use the :run command to execute the buffer immediately (without needing a blank line), or :run <file> to execute a files contents.
## Timing and profiling
- Toggle a simple elapsed time measurement for executions with :time on|off|toggle.
- Toggle a simple elapsed time measurement for executions with :time on/off/toggle.
- Use :profile to parse+run and report parse time, run time, total, and instruction count.
## Command reference
Type :help to print the built-in command summary. Full list with clarifications:
- :help | :h
- :help | :h<br>
Show the help.
- :quit | :q | :exit
- :quit | :q | :exit<br>
Exit the REPL.
- :reset | :re
- :reset | :re<br>
Reset VM state (clears globals).
- :dump | :du | :globals | :gl
- :dump | :du | :globals | :gl<br>
Dump current globals (indexes and stringified values).
- :globals [pattern] / :vars | :v [pattern]
- :globals [pattern] / :vars | :v [pattern]<br>
Dump globals, filtering by substring match on the value when a pattern is provided.
- :clear | :cl
- :clear | :cl<br>
Clear the current input buffer.
- :print | :pr
- :print | :pr<br>
Show the current buffer content.
- :run | :ru [file]
- :run | :ru [file]<br>
Execute current buffer, or execute the specified file immediately. Parsing errors are reported with caret highlighting.
- :profile | :pf
- :profile | :pf<br>
Execute buffer and show timing for parse and run plus instruction count.
- :save | :sa <file>
- :save | :sa <file><br>
Save the current buffer to a file.
- :load | :lo <file>
- :load | :lo <file><br>
Load a file into the buffer (does not run). Use :run or a blank line to execute afterward.
- :paste | :pa [run]
- :paste | :pa [run]<br>
Enter paste mode to insert multiple lines verbatim. Finish with a single dot line: `.`. If the optional argument `run` (or `exec`) is given, the REPL will run the pasted buffer immediately.
- :history | :hi [N]
- :history | :hi [N]<br>
Show the last N lines of persistent history (default 50).
- :time | :ti on|off|toggle
- :time | :ti on|off|toggle<br>
Toggle/enable/disable timing for subsequent runs.
- :env | :en [NAME[=VALUE]]
- :env | :en [NAME[=VALUE]]<br>
Get or set an environment variable. With NAME only, prints NAME=value. With NAME=VALUE, sets the variable for the current process.
- :backtrace | :bt | :ba
- :backtrace | :bt | :ba<br>
Show a backtrace of VM frames (most recent first), including function name, source file, IP, and line.
- :frame | :fr N
- :frame | :fr N<br>
Select a frame N (0..top) to target with :locals, :list, :disasm and value inspections. By default, the top frame is used.
- :list | :li [±K]
- :list | :li [±K]<br>
Show K lines of source around the current frames line (default 5). The current line is marked with `>`.
- :disasm | :di [±N]
- :disasm | :di [±N]<br>
Disassemble around current frames instruction pointer (default 5 on each side). Shows index, opcode name, and operand.
- :mdump | :md WHAT [offset [len]] [raw] [to <file>]
Dump a VM memory region. WHAT is one of: code | stack | globals | consts. Offset and length are optional; if omitted, a sensible default (up to 256 bytes) is used. With `raw`, write binary bytes. With `to <file>`, write output to a file; otherwise print to stdout as a formatted hexdump.
- :mdump | :md WHAT [offset [len]] [raw] [to &lt;file&gt;]<br>
Dump a VM memory region. WHAT is one of: code, stack, globals and consts. Offset and length are optional; if omitted, a sensible default (up to 256 bytes) is used. With `raw`, write binary bytes. With `to <file>`, write output to a file; otherwise print to stdout as a formatted hexdump.
- :stack | :st [N]
- :stack | :st [N]<br>
Show top N (or all) stack values, stringified.
- :top | :to
- :top | :to<br>
Show the value at the top of the VM stack.
- :locals | :lc [FRAME]
- :locals | :lc [FRAME]<br>
Show non-nil locals for the selected frame (or the provided frame index).
- :printv | :pv WHAT
- :printv | :pv WHAT<br>
Print a specific value: `local[i]`, `stack[i]`, or `global[i]`.
- :break | :br [file:]line
- :break | :br [file:]line<br>
Set a breakpoint. If file is omitted, the current frames source file is used. Prints a numeric breakpoint ID on success.
- :info | :in breaks
- :info | :in breaks<br>
List breakpoints.
- :delete | :de ID
- :delete | :de ID<br>
Delete a breakpoint by ID.
- :clear breaks | :cb
- :clear breaks | :cb<br>
Remove all breakpoints.
- :cont | :co
- :cont | :co<br>
Continue execution. In REPL-on-error/debug stops, this exits the REPL and resumes the program.
- :step | :sp
- :step | :sp<br>
Step a single instruction (REPL-on-error/debug mode).
- :next | :ne
- :next | :ne<br>
Step over in the current frame (REPL-on-error/debug mode).
- :finish | :fi
- :finish | :fi<br>
Run until the current frame returns (REPL-on-error/debug mode).
If an unknown command is entered, the REPL prints a hint to use :help.

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - Standard Library Overview
title: Standard Library Overview
subtitle: Overview of the standard library modules under ./lib with one-line summaries.
description: Overview of the standard library modules under ./lib with one-line summaries.
permalink: /documentation/stdlib/
@ -26,17 +26,17 @@ The stdlib is written in Fun and organized by domain. Below is the current layou
## Top-level modules
- `arrays.fun` helpers for working with array structures.
- `cli.fun` minimal helpers for building CLI tools.
- `hello.fun` simple demonstration helper(s).
- `hex.fun` hexadecimal encode/decode helpers.
- `math.fun` math helpers in Fun.
- `regex.fun` simple regex-related helpers (see also `regex/`).
- `strings.fun` string manipulation utilities.
- `arrays.fun` - helpers for working with array structures.
- `cli.fun` - minimal helpers for building CLI tools.
- `hello.fun` - simple demonstration helper(s).
- `hex.fun` - hexadecimal encode/decode helpers.
- `math.fun` - math helpers in Fun.
- `regex.fun` - simple regex-related helpers (see also `regex/`).
- `strings.fun` - string manipulation utilities.
## Packages
- `crypt/` cryptographic primitives and hashes. See [https://git.xw3.org/fun/fun/src/branch/main/lib/crypt/](https://git.xw3.org/fun/fun/src/branch/main/lib/crypt/){:class="git"}.
- `crypt/` - cryptographic primitives and hashes. See [https://git.xw3.org/fun/fun/src/branch/main/lib/crypt/](https://git.xw3.org/fun/fun/src/branch/main/lib/crypt/){:class="git"}.
- `aes256.fun`
- `crc32.fun`, `crc32c.fun`
- `md5.fun`, `md5_legacy.fun`
@ -47,26 +47,26 @@ The stdlib is written in Fun and organized by domain. Below is the current layou
- `base64.fun`
- `io/` — input/output utilities and system interfaces. See [https://git.xw3.org/fun/fun/src/branch/main/lib/io/](https://git.xw3.org/fun/fun/src/branch/main/lib/io/){:class="git"}.
- `console.fun` console I/O helpers
- `ini.fun` INI parse helpers
- `json.fun` JSON helpers
- `pcsc.fun`, `pcsc2.fun` smart card access (PC/SC)
- `process.fun` spawn and manage subprocesses
- `serial.fun` serial port helpers
- `socket.fun` socket convenience wrappers
- `thread.fun` simple threading utilities
- `xml.fun` XML helpers
- `console.fun` - console I/O helpers
- `ini.fun` - INI parse helpers
- `json.fun` - JSON helpers
- `pcsc.fun`, `pcsc2.fun` - smart card access (PC/SC)
- `process.fun` - spawn and manage subprocesses
- `serial.fun` - serial port helpers
- `socket.fun` - socket convenience wrappers
- `thread.fun` - simple threading utilities
- `xml.fun` - XML helpers
- `net/` networking helpers and example HTTP servers. See [https://git.xw3.org/fun/fun/src/branch/main/lib/net/](https://git.xw3.org/fun/fun/src/branch/main/lib/net/){:class="git"}.
- `cgi.fun` basic CGI helpers
- `http_server.fun` blocking HTTP server
- `http_cgi_server.fun` HTTP server that can execute .fun CGI files
- `http_cgi_lib_server.fun` variant of the HTTP CGI server using the stdlib
- `net/` - networking helpers and example HTTP servers. See [https://git.xw3.org/fun/fun/src/branch/main/lib/net/](https://git.xw3.org/fun/fun/src/branch/main/lib/net/){:class="git"}.
- `cgi.fun` - basic CGI helpers
- `http_server.fun` - blocking HTTP server
- `http_cgi_server.fun` - HTTP server that can execute .fun CGI files
- `http_cgi_lib_server.fun` - variant of the HTTP CGI server using the stdlib
- `regex/` regular expression utilities (PCRE2-based when available). See [https://git.xw3.org/fun/fun/src/branch/main/lib/regex/](https://git.xw3.org/fun/fun/src/branch/main/lib/regex/){:class="git"}.
- `regex/` - regular expression utilities (PCRE2-based when available). See [https://git.xw3.org/fun/fun/src/branch/main/lib/regex/](https://git.xw3.org/fun/fun/src/branch/main/lib/regex/){:class="git"}.
- `pcre2.fun`
- `utils/` small reusable helpers and functional utilities. See [https://git.xw3.org/fun/fun/src/branch/main/lib/utils/](https://git.xw3.org/fun/fun/src/branch/main/lib/utils/){:class="git"}.
- `utils/` - small reusable helpers and functional utilities. See [https://git.xw3.org/fun/fun/src/branch/main/lib/utils/](https://git.xw3.org/fun/fun/src/branch/main/lib/utils/){:class="git"}.
- `datetime.fun`
- `match.fun`
- `math.fun`
@ -74,13 +74,13 @@ The stdlib is written in Fun and organized by domain. Below is the current layou
- `range.fun`
- `result.fun`
Note: Availability of some modules can depend on optional extensions selected at build time (see [../build/](../build/)). For instance, `regex/pcre2.fun` requires PCRE2 support; `ui/*` depends on chosen UI backends.
Note: Availability of some modules can depend on optional extensions selected at build time (see [Build](../build/)). For instance, `regex/pcre2.fun` requires PCRE2 support; `ui/*` depends on chosen UI backends.
## Using modules
<pre>#include &lt;strings.fun
<pre>#include &lt;strings.fun&gt;
s = trim(" hello ")
print(s)</pre>
For search paths and namespacing details, see [../includes/](../includes/) and [../cli/](../cli/) (FUN_LIB_DIR and DEFAULT_LIB_DIR).
For search paths and namespacing details, see [Includes](../includes/) and [CLI](../cli/) (FUN_LIB_DIR and DEFAULT_LIB_DIR).

View file

@ -4,7 +4,7 @@ published: true
noToc: false
noComments: false
noDate: false
title: Fun - VM Configuration Constants
title: VM Configuration Constants
subtitle: Detailed explanation of VM limits and memory management constants.
description: Learn about MAX_FRAMES, MAX_FRAME_LOCALS, MAX_GLOBALS, STACK_SIZE, and OUTPUT_SIZE in the Fun VM.
permalink: /documentation/vm/
@ -19,9 +19,10 @@ The Fun Virtual Machine (VM) uses several fixed-size limits to manage memory and
This document explains what each of these constants does in plain English.
View [../build/](../build/) to see how to set the params at build time.
View [Build](../build/) to see how to set the params at build time.
## `MAX_FRAMES` (Default: 128)
This constant defines the **maximum depth of the call stack**.
When a function is called, the VM creates a "frame" to keep track of that function's execution (where it's at in the code and its local variables). If a function calls another function, a new frame is added on top.
@ -30,6 +31,7 @@ When a function is called, the VM creates a "frame" to keep track of that functi
- **Analogy:** Imagine a stack of dinner plates. `MAX_FRAMES` is the maximum height the stack can reach before it becomes unstable or hits the ceiling.
## `MAX_FRAME_LOCALS` (Default: 64)
This constant limits the **number of local variables** each individual function can have.
Every time a function is called, it gets its own space for variables that only exist within that function.
@ -38,6 +40,7 @@ Every time a function is called, it gets its own space for variables that only e
- **Analogy:** Think of this as the number of pockets in a single person's jacket. You can only carry 64 items in your pockets at once.
## `MAX_GLOBALS` (Default: 128)
This constant defines the **maximum number of global variables** available to the entire program.
Global variables are accessible from anywhere in your code, unlike local variables which belong to a specific function.
@ -46,6 +49,7 @@ Global variables are accessible from anywhere in your code, unlike local variabl
- **Analogy:** This is like a shared community bulletin board. There is only enough room on the board for 128 different notices.
## `STACK_SIZE` (Default: 1024)
This constant sets the size of the **operand stack**.
The VM uses this stack for almost everything it does: adding numbers, comparing values, and passing arguments to functions. Most operations take values from the top of the stack, perform a calculation, and push the result back onto the stack.
@ -54,6 +58,7 @@ The VM uses this stack for almost everything it does: adding numbers, comparing
- **Analogy:** Imagine a workbench where you put tools and materials you are currently working on. `STACK_SIZE` is the area of that workbench. If it's too small, you can't work on complex projects.
## `OUTPUT_SIZE` (Default: 1024)
This constant determines the size of the **output buffer**.
When your program uses commands like `PRINT` or `ECHO`, the results are stored in an internal list before they are displayed or processed further.