|
|
||
|---|---|---|
| lib | ||
| src | ||
| test | ||
| dune | ||
| dune-project | ||
| makefile | ||
| readme.md | ||
readme.md
star
Star is a statically typed functional programming language with type inference, ADTs, pattern matching and a module system. It transpiles to idiomatic JavaScript ES6 and Python 3.10+ w/ full source map support
Prerequisites
- OCaml >= 4.14.0
- opam
- dune >= 3.12
Build from Source
git clone https://github.com/meowrly/star.git
cd star
opam install . --deps-only
make build
make install
Usage
Compile to JS:
star compile program.star
Compile to Python:
star compile -t python program.star
w/ optimisations:
star compile -O -o output.js program.star
REPL
star repl
Language
let main = fn () =>
print("Hello, World!")
Functions & lambdas
let add = fn x y => x + y
let double = fn x => x * 2
let compose = fn f g => fn x => f(g(x))
let add5 = add(5)
Pattern matching
type Option<'a> =
| Some of 'a
| None
let unwrap_or = fn default opt =>
match opt with
| Some(x) => x
| None => default
end
ADTs
type Result<'a, 'e> =
| Ok of 'a
| Err of 'e
type Person = {
name: string,
age: int
}
let point: (int, int) = (10, 20)
Lists
let numbers = [1, 2, 3, 4, 5]
let doubled = map(fn x => x * 2, numbers)
let evens = filter(fn x => x % 2 == 0, numbers)
let sum = foldl(fn acc x => acc + x, 0, numbers)
Records
type Point = {
x: int,
y: int
}
let origin = { x = 0, y = 0 }
let moved = { origin with x = 10 }
Modules
module Math = struct
let pi = 3.14159
let circle_area = fn r =>
pi * r * r
end
let area = Math.circle_area(5.0)
Type annotations
let identity: 'a -> 'a = fn x => x
let map: ('a -> 'b) -> list<'a> -> list<'b> =
fn f list =>
match list with
| [] => []
| x :: xs => f(x) :: map(f, xs)
end
Type Inference
let add = fn x y => x + y
let cons = fn x xs => x :: xs
let map = fn f list => ...
References
- Pierce, B. C. (2002). Types and Programming Languages
- Appel, A. W. (1998). Modern Compiler Implementation in ML
- Leroy, X. et al. The OCaml System
- Diehl, S. Write You a Haskell