Welcome to LunaVM, a unique virtual machine built from scratch in C, designed to execute MoonlightScript programs, manage memory efficiently, and act as a foundation for a lightweight operating system or a virtualized platform. π
LunaVM serves as the runtime environment for MoonlightScript, a statically typed, lightweight programming language designed to create system-level applications for LunarVM. MoonlightScript compiles to LunarVM opcodes, which the VM executes, providing a seamless bridge between high-level scripting and low-level execution.
Key features of MoonlightScript:
- Static typing with type inference.
- Clean, intuitive syntax for systems programming.
- Compiles to binary opcodes for LunarVM (
.lvmb
format). - MoonlightScript programs can manipulate memory, render graphics, and even build multi-app systems.
LunaVM uses a custom-built Starlock allocator for dynamic memory management. Unlike traditional malloc
-based allocation, Starlock:
- Allocates memory in arenas to prevent fragmentation.
- Tracks ownership and lifetimes of memory blocks to minimize memory leaks.
- Ensures optimal performance for LunarVM programs.
LunaVM interprets opcodes generated by the MoonlightScript Compiler (MLSC) and executes them in a secure, isolated environment. The VM provides:
- A fetch-decode-execute cycle to handle instructions.
- A stack-based and register-based hybrid architecture for flexibility.
- Built-in support for key opcodes like
MOV
,ADD
,PRINT
,HLT
.
Future versions of LunaVM will support Raylib to enable rendering of 2D/3D graphics directly from MoonlightScript programs, making it possible to:
- Create windowed or fullscreen applications with dynamic content.
- Develop interactive 3D games or simulations.
- Bind MoonlightScript functions to Raylibβs rendering pipeline.
The development pipeline for LunaVM applications looks like this:
-
Write MoonlightScript Code
Exampleexample.mls
:fun main() (Void) { var message: String = "Hello, Lunar World!"; print(message); }
-
Compile with MoonlightScript Compiler (MLSC)
- Generates
.lvmb
(binary) and.lvs
(source-debug) files:.lvmb
: Binary file of opcodes for LunarVM..lvs
: Optional source file for debugging and introspection.
- Generates
-
Run on LunaVM
- Load the
.lvmb
file into LunaVMβs memory and execute:lunarvm example.lvmb
- Load the
-
Opcodes LunaVM currently supports the following opcodes:
MOV
: Move data into registers.ADD
: Perform arithmetic.PRINT
: Display strings or numbers.HLT
: Halt execution.
Example LunarVM program in assembly-like pseudocode:
0x01 0x01 0x10 // MOV R1, 0x10 0x02 0x01 0x01 // ADD R1, R1 0x03 // PRINT R1 0xFF // HLT
-
Memory Management
- Starlock provides efficient allocation with ownership models and arena-based allocation.
- LunarVMβs memory can be extended dynamically via Starlock, enabling applications to manage memory flexibly.
-
Planned Multitasking
- LunaVM will eventually support multiple loaded programs in separate memory spaces, enabling multitasking.
The long-term goal of LunaVM is to evolve into a general-purpose virtual operating system capable of running multitasking applications, integrating features like:
- A basic task scheduler to manage multiple processes.
- Networking capabilities for communication between applications.
- A file management system for
.lvmb
binaries and data.
-
MoonlightScript Application Development
Write applications like games, utilities, or simulations using MoonlightScript. -
Virtualized Operating System
Build a lightweight OS using MoonlightScript to run multiple.lvmb
programs concurrently on LunarVM. -
Graphics and Game Development
Integrate Raylib to build interactive games or dynamic simulations in MoonlightScript.
This project is a personal learning and exploration journey, so contributions are not currently accepted. However, feel free to fork and explore the code!
Thanks for checking out LunaVM and supporting this exciting journey into virtual machines, memory management, and custom programming languages. π
Let me know if youβd like to tweak anything further!