Skip to content
bl1tmapsPublic

About

A cool programming language, implemented in C, that uses a VM and has cool syntax; with inline C capability.

Topics

Resources

Stars

7 stars

Watchers

0 watching

Forks

Latest commit

 

History

333 Commits

Folders and files

Repository files navigation

A Derpy Logo

Mylo

Mylo is an experimental language implemented in C, that uses a simple VM and has cool syntax. It has almost seamless C-interoperability. Like a sausage dog, it is not serious, but it is cool.

🦗 Mylo is alpha, so expect bugs.

Check out the Language Reference.

Hello Mylo

Here is hello.mylo:

print("Hello Mylo")

We can run it like this:

> ./mylo hello.mylo

Hello Mylo

Something a bit more complex

Here is fib.mylo:

fn fib(n: i32) -> i32 {
  if (n < 2) {
    ret n
  }
  // Recursively call fib
  ret fib(n - 1) + fib(n - 2)
}
print("Calculating Fib(10)...")
result: i32 = fib(10)
// With string interpolation
print(f"result : {result}")

Here's the ouput

> mylo fib.mylo
Calculating Fib(10)...
result : 55

Running a Mylo program

With the interpreter

> ./mylo my_program.mylo

Something more complicated

Mylo Language v0.2.0
A lightweight, embeddable language.                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                          
USAGE:                                                                                                                                                                                                                                                                                                                    
mylo [flags] [file]                                                                                                                                                                                                                                                                                                       

FLAGS:
  --repl          Start the interactive Read-Eval-Print Loop.                                                                                                                                                                                                                                                             
  --version       Display the current version information.
  --help          Show this help message.
  --dap           Enable DAP debugging.
  --db            Debug mode, load the code and jump into an interactive debugger.
  --trace         Run as normal but print every VM state change
  --build         Build and generate a .c bootstrapping source file.
  --bind          Generate a .c source file binding for later interpreted or compiled dynamic linking
  --bundle        Compile Mylo application and output mylo_exe (bundle bytecode and VM interpreter).
  --dump          Dump the generated bytecode instructions.
  --config FILE   Read VM settings from FILE instead of looking for mylovm.conf.
  -X key=value    Override one VM setting, e.g. -X stack_size=64k. Can be repeated.
  --print-config  Show the VM settings that would be used, and where they came from.
  --write-config  Write the current VM settings to mylovm.conf (or the file given next).
  --help-config   Describe every VM setting.
  --unbuffered    Write output immediately.

EXAMPLES:
  Run a script:                                                                                                                                                                                                                                                                                                           
    ./mylo script.mylo                                                                                                                                                                                                                                                                                                    

  Start REPL with debug mode:
    ./mylo --repl                                                                                                                                                                                                                                                                                                         

  Build a native application (with or without inline C Code):
    ./mylo --build my_file.mylo                               <-- Produced out.c                                                                                                                                                                                                                                          
     cc out.c src/vm.c src/mylolib.c -o mylo_app -Isrc -lm    <-- Uses GNU / C Compiler to build native Executable                                                                                                                                                                                                        
    ./mylo_app                                                <-- Runs like any other executable, with bundled VM.                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                          
  Dynamically or Statically Use C Code:                                                                                                                                                                                                                                                                                   
    ./mylo --bind test_lib.mylo                                  <-- Contains C blocks, this outputs test_lib.mylo_bind.c                                                                                                                                                                                                 
    cc -shared -fPIC test_lib.mylo_bind.c -Isrc -o test_lib.so   <-- Produced a shared object which can be dynamically imported                                                                                                                                                                                           
  Then inside your mylo applications:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    import native test_lib.mylo        <-- Mylo will look for test_lib.so and load compiled C functions                                                                                                                                                                                                                   
    print(my_cool_c_func())            <-- C functions from shared objects can be run in the interpreter, or compiled.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Mylo IDE

mylo-ide is a lightweight editor and debugger for Mylo. It has completions, breakpoints, stepping, variables and watches, a run console with input, a viewer for the VM's stack, heap, strings and bytecode, and a panel for the VM settings.

> ./mylo-ide my_program.mylo

It runs each script in its own mylo process, so a crashing script can't take the editor down. See docs/ide.md.

VM settings (mylovm.conf)

Limits such as the stack size and the number of strings can be raised per project, much like the options for a Java VM. Put them in a mylovm.conf beside the script, or beside mylo for every script:

stack_size = 64k
max_strings = 100k

Or set one for a single run with mylo -X stack_size=64k my_program.mylo. When a program hits a limit, the error names the setting to raise. See docs/mylovm_conf.md for every setting.

Building Mylo

The simple way, with no modules

 # Call the c compiler with source, and link math.
> cc src/*.c -o mylo -lm

Using CMake (also builds the tests and Mylo IDE)

> cmake -B build -DCMAKE_BUILD_TYPE=Release
> cmake --build build --config Release
> ctest --test-dir build -C Release

CMake downloads raylib 5.5 for the IDE. Pass -DMYLO_BUILD_IDE=OFF to build only the language tools.

Every pull request is built and tested on Windows, macOS and Linux by .github/workflows/build.yml. Merges to main keep the zipped builds as workflow artifacts, and publishing a GitHub release attaches them to it.

Using CMake to build all modules

cmake -B build -DCMAKE_BUILD_TYPE=Release -DMODULES=ON
cd build
make -j8

The 'modules' directory

Modules, that contain .mylo wrappers and binary shared libraries are stored next to the mylo executable. Mylo will search here for these files at runtime, or during bundling.

Debugging in VSCode

Mylo supports the DAP protocol. This can be used with any tool that supports DAP, using the --dap flag. There is a VSCode plugin, in editors/mylo-vscode that configures Mylo automatically.

The adapter supports breakpoints in imported files, step over, into and out, pause, the full call stack, expandable arrays, structs and maps, and hover and watch expressions. A runtime error stops on the failing line. Set "stopOnEntry": false in the launch configuration to run straight to the first breakpoint.

VSCode Screenshot

Debugging in CLI

Mylo supports debugging in the command line using the --db flag. The Mylo program will launch and stop at the first line.

  SOURCE CODE                                                                                                                                                                                                                                                                                                             
➤    1 │ system_thread("sleep 2 && echo Done", "my_job")                                                                                                                                                                                                                                                                  
     2 │                                                                                                                                                                                                                                                                                                                  
     3 │ forever {                                                                                                                                                                                                                                                                                                        
     4 │     res: any = get_job("my_job")                                                                                                                                                                                                                                                                                  
     5 │     if (res == 1) {                                                                                                                                                                                                                                                                                              
     6 │         print("Working...")                                                                                                                                                                                                                                                                                      
     7 │     } else {                                                                                                                                                                                                                                                                                                     
     8 │         print(f"Result: {res[0]}")                                                                                                                                                                                                                                                                               
     9 │         break                                                                                                                                                                                                                                                                                                    
    10 │     }                                                                                                                                                                                                                                                                                                            
    11 │ }                                                                                                                                                                                                                                                                                                                
  VM STATE                                                                                                                                                                                                                                                                                                                
  FP: 0     SP: -1                                                                                                                                                                                                                                                                                                        
  -- Globals --                                                                                                                                                                                                                                                                                                           
    res         : 0                                                                                                                                                                                                                                                                                                       
  -- Locals --                                                                                                                                                                                                                                                                                                            
    (None)                                                                                                                                                                                                                                                                                                                
  mylo-db >
    // Commands:
    // n          Next line
    // s          Step instruction
    // c          Continue
    // b <line>   Set breakpoint
    // p <var>    Print variable
    // set <x> v  Set variable x to v
    // q          Quit

Strict Type System & Scoped Arenas

Mylo is strictly and statically typed without var or generic num. Variables are declared with explicit annotations:

count: i32 = 42
pi: f64 = 3.14159265359
active: bool = true
buffer: u8[] = [0, 128, 255]
range_arr: i32[] = [0...10]

Supported Types

  • Integers: u8 (or byte), u16, u32, u64, i8, i16, i32, i64
  • Floating Point: f32, f64
  • Text & Binary: str (or string), bytes
  • Typed Arrays: T[] (e.g. i32[], u8[], f32[], f64[])
  • Containers: array, map
  • Structures: Custom typed struct definitions

Generational Scoped Arenas

Mylo eliminates garbage collection pauses by using bump-allocated memory arenas with generational handles:

Lexical Scoped Blocks:

region {
    temp: i32[] = [0...1000]
    // Memory inside region is automatically reclaimed at scope exit
}

Persistent Named Regions & Explicit Lifecycles:

region cache
cache::data: i32[] = [10, 20, 30]

print(cache::data[0]) // 10

// Reclaim all memory in 'cache' deterministically
clear cache

But everything cool is written in C, how do I use those libraries?

Seamlessly call C (cfn)

Mylo allows C functions to be called directly by declaring them with cfn (c-function).

// 'cfn' defines a C function prototype (e.g. from the standard C library)
cfn atoi(x: str) -> f32

// Prints 7
print(f"{atoi("7")}")

Automatic Runtime Dynamic Execution

When running in interpreted mode (./mylo script.mylo), Mylo dynamically looks up and binds cfn symbols from the running host process or linked libraries at startup.

  • If the symbol is found, it executes immediately in the interpreter without a C compile step.
  • If the symbol is missing, Mylo prints a helpful error indicating the missing symbol along with compilation commands:
    error: native function <name> was not found, either build mylo with these libraries, or provide shared libraries
    Error: This program contains Native C blocks and no shared objects are found, so it cannot be interpreted.
    Please compile it to bytecode using: mylo --build script.mylo
    

Building a native binary with inline C?

Here is some Mylo code jumping into C and back. Mylo uses C() {} syntax to embed anonymous C function code directly, and it can access variables from Mylo and return them:

// Explicit return: f64
result: f64 = C(val: i32 = 25) -> f64 {
    double res = sqrt(val);
    printf("Inside C: sqrt(%d) = %f\n", (int)val, res);
    return res;
}

print(f"Back in Mylo: Result is {result}")

For more information, read the FFI section of the reference.

We build it by producing a C source file with embedded VM bytecode and wrapper code pre-generated:

# This produces an out.c file
> mylo --build example.mylo

We then just compile it, like any other C file, including the Mylo VM source and headers:

# This produces mylo_app
> gcc out.c src/vm.c src/mylolib.c -o mylo_app -Isrc -lm  

Our application can then be executed natively:

# Run
> ./mylo_app
  Inside C: sqrt(25) = 5.000000
  Back in Mylo: Result is 5

Integration with CMake

Mylo applications can be built, C applications bound, and integrated into other applications with CMake. There are docs here.

Mylo REPL

Mylo can be launched in interpreter mode to evaluate commands from the CLI. It can also be used to load native dlls and to import other mylo modules.

Launch the Mylo repl with mylo --repl. An example can be seen below:

Mylo REPL v0.3.6
Type 'exit' to quit.
> a: i32[] = [0...5]
> print(a)
[0, 1, 2, 3, 4, 5]
> 

About

A cool programming language, implemented in C, that uses a VM and has cool syntax; with inline C capability.

Topics

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages