150 lines
2.3 KiB
Markdown
150 lines
2.3 KiB
Markdown
# 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
|
|
|
|
```bash
|
|
git clone https://github.com/meowrly/star.git
|
|
cd star
|
|
opam install . --deps-only
|
|
make build
|
|
make install
|
|
```
|
|
|
|
## Usage
|
|
|
|
Compile to JS:
|
|
```bash
|
|
star compile program.star
|
|
```
|
|
|
|
Compile to Python:
|
|
```bash
|
|
star compile -t python program.star
|
|
```
|
|
|
|
w/ optimisations:
|
|
```bash
|
|
star compile -O -o output.js program.star
|
|
```
|
|
|
|
### REPL
|
|
|
|
```bash
|
|
star repl
|
|
```
|
|
|
|
### Language
|
|
|
|
```star
|
|
let main = fn () =>
|
|
print("Hello, World!")
|
|
```
|
|
|
|
### Functions & lambdas
|
|
|
|
```star
|
|
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
|
|
|
|
```star
|
|
type Option<'a> =
|
|
| Some of 'a
|
|
| None
|
|
|
|
let unwrap_or = fn default opt =>
|
|
match opt with
|
|
| Some(x) => x
|
|
| None => default
|
|
end
|
|
```
|
|
|
|
### ADTs
|
|
|
|
```star
|
|
type Result<'a, 'e> =
|
|
| Ok of 'a
|
|
| Err of 'e
|
|
|
|
type Person = {
|
|
name: string,
|
|
age: int
|
|
}
|
|
|
|
let point: (int, int) = (10, 20)
|
|
```
|
|
|
|
### Lists
|
|
|
|
```star
|
|
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
|
|
|
|
```star
|
|
type Point = {
|
|
x: int,
|
|
y: int
|
|
}
|
|
|
|
let origin = { x = 0, y = 0 }
|
|
|
|
let moved = { origin with x = 10 }
|
|
```
|
|
|
|
### Modules
|
|
|
|
```star
|
|
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
|
|
|
|
```star
|
|
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
|
|
|
|
```star
|
|
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 |