Midori(ãããĒ)
A Programming Language
Statically-typed programming language featuring algebraic data types, pattern matching, classes, and a powerful module system. Compiles to bytecode for the Midori Virtual Machine with garbage-collected memory management.
Key Features
Static Type System
Strong static typing with type inference and generics. Catch errors at compile time while maintaining clean, concise code.
Pattern Matching
Exhaustive pattern matching on algebraic data types with compiler-enforced coverage, ensuring all cases are handled.
Algebraic Data Types
Express complex data structures with structs (product types) and unions (sum types) for safer, more expressive code.
Classes
Constrained generics for powerful polymorphism. Define behavior contracts and implement them for any type.
Module System
Explicit imports and exports with privacy enforcement. Build large-scale applications with clear module boundaries.
Expression-Oriented
Write more functional, composable code with natural control flow.
Quick Start
import { "../MidoriPrelude/IO.mdr" }
IO::PrintLine("Hello, Midori!");// Simple function
defun square(x: Int) : Int => {
return x * x;
};
// Generic function
defun identity<T>(value: T) : T => value;
// Higher-order function
defun apply<T, R>(
fn: fn(T) -> R,
value: T
) : R => {
return fn(value);
};// Product types (structs)
struct Point {
x: Float,
y: Float,
};
// Sum types (unions)
union Option<T> = Some(T) | None;
union Result<T, E> = Ok(T) | Err(E);
def origin = new Point(0.0, 0.0);
def maybe = new Option::Some(42);Pattern Matching
union Result<T, E> = Ok(T) | Err(E);
defun handle_result<T>(result: Result<T, Text>) : Text => {
return match result with
case Result::Ok(value) => "Success: " ++ (value as Text)
case Result::Err(msg) => "Error: " ++ msg
default => "Unknown"
;
};Classes
// Define a Class
class Show<T> {
show: fn(value: T) -> Text;
};
// Implement for Int
instance Show<Int> {
defun show(value: Int) : Text => {
return value as Text;
};
};
// Use with constraints
defun display<T>(value: T) : Text where T: Show<T> => {
return show(value);
};
def message = display(42); // "42"Advanced Features
defun double(x: Int) : Int => x * 2;
defun add_ten(x: Int) : Int => x + 10;
def result = 5
|> double
|> add_ten
|> double; // 40defun make_counter() : fn() -> Int => {
def count = 0;
return fn() : Int => {
count = count + 1;
return count;
};
};
def counter = make_counter();
def first = counter(); // 1
def second = counter(); // 2Async/Await
async task creates a new lightweight VM with its own heap and garbage collector.Note: Async/Await requires native threads and is not available in the web playground.
import { <IO> }
defun compute_heavy(x: Int) : Int => {
// Computations...
return x * x;
};
// Spawn concurrent task (runs in separate VM)
def task : Future<Int> = async compute_heavy(42);
IO::PrintLine("Task running in background...");
// Await result (blocks until completion)
def result = await task;Language Operators
Arithmetic
+ - * / %
Comparison
== != < > <= >=
Logical
&& || !
Bitwise
& | ^ <~ ~>
String
++ (concatenation)
Functional
|> (pipe)
Example: Recursive Data Structures
union Tree<T> = Leaf(T) | Node(Tree<T>, Tree<T>);
defun height<T>(tree: Tree<T>) : Int => {
return match tree with
case Tree::Leaf(value) => 1
case Tree::Node(left, right) => {
def left_height = height(left);
def right_height = height(right);
return 1 + (if left_height > right_height
then left_height
else right_height);
}
default => 0
;
};union List<T> = Cons(T, List<T>) | Nil;
defun map<A, B>(list: List<A>, f: fn(A) -> B) : List<B> => {
return match list with
case List::Cons(head, tail) =>
new List::Cons(f(head), map(tail, f))
case List::Nil =>
new List::Nil()
default =>
new List::Nil()
;
};Type System
Primitive Types: Int, Float, Bool, Text, Unit
Composite Types: Array<T>, Future<T>, structs, unions
Function Types: fn(T1, T2) -> R
Generic Parameters: Single and multiple type parameters
Type Constraints: Class constraints with where clause
Type Inference: Automatic type deduction at instantiation
Standard Library
IO.mdr
Input/output operations including PrintLine for console output and file I/O utilities.
Math.mdr
Mathematical functions including trigonometry, logarithms, and common operations.
System.mdr
System interaction utilities for command line arguments, environment variables, and more.
DateTime.mdr
Timing and date operations for working with temporal data in your applications.
Foreign Function Interface (FFI)
Text and Array are automatically marshalled between the VM and native code.// Declare external C++ function
foreign "MIDORI_FFI_ReadFile" ReadFile : fn(Text) -> Array<Byte>;
// Use it like a normal function
def bytes = ReadFile("data.bin");Architecture
Frontend: Lexer â Parser â Type Checker
Optimizer: Constant folding, tail call optimization, strength reduction
Backend: Bytecode generator â Linker
Runtime: Stack-based VM with mark-and-sweep garbage collection