This is the first post of a series where I'll talk about the different techniques I've used to create a parser and un evaluator for the Litil™ programming language in Java. However, please keep the following in mind:
This is still work in progress, this a a bare-bones languages: no REPL, no tools, no language spec, nothing but code, and even that isn't that pretty to look at not particularly clean or organized.
Here's a quick overview of the Litil language
- A functional language heavily inspired by ML, (O)Caml, Haskell and Roy
- Full Hindley Milner type inference
- Python like use of indentation to define blocs
- Supported types: numerics, strings, characters and boolean, tuples, records and ADTs
- pattern matching
- curried functions by default
- closures
- exceptions (try/catch/throw)
Now, some teaser examples to give you a feel for the language:
let fact n =
if n <= 2 then
2
else
n * fact (n-1)
let f5 = fact 5
let x = (5, "a")
let person = {name: "lpe", age: 12}
let (a, b) = (4, "d")
let d = ((4, true), ("test", 'c', a))
let ((_, bool), (_, _, _)) = d
data Option a = Some a | None
let o = Some "thing"
data List a = Cons a (List a) | Nil
let l = Cons 5 (Cons 6 Nil)
data Tree a = Null | Leaf a | Node (Tree a) a (Tree a)
let t = Node (Leaf 5) 4 (Leaf 3)
let len l =
match l
[] => 0
_ :: t => 1 + len t
len [1, 2, 3]
let add x y = x + y
let inc = add 1
let three = inc 2
let map f xs =
match xs
[] => Nil
h :: t => (f h) :: (map f t)
let l = [1, 2]
let double x = 2 * x
-- pass a function by name
let l2 = map double l
-- or simply a lambda
let l2 = map (\x => 2 * x) l
let a = 4
let f = \x => a * x -- f captures the lexical value of a, i.e. 4
let a = 5
f 5
See LICENSE
for details (hint: it's MIT).