Tree @master (Download .tar.gz)
Castile
Version 0.5 | Entry @ catseye.tc | See also: Eightebed ∘ Dieter
This is the reference distribution for Castile, a simple imperative language with union types.
The design of Castile was influenced (in varying degrees) by C, Rust, Eightebed, Python, Ruby, and Erlang. More information on its roots can be found in doc/Design.md.
The reference implementation can both interpret Castile programs and compile them to a variety of targets — JavaScript, Ruby, C, and a generic stack-based VM (included in this distribution).
A rich test suite in Falderal format, which describes the language with many examples, can be found in tests/Castile.md.
Quick Start
Clone this repository, cd into the repo directory and run
bin/castile eg/hello.castile
Alternately, put the bin subdirectory on your executable search path, so
that you can run castile from any directory on your system. castile
has no dependencies besides Python (either Python 2 or Python 3.)
Motivating Example
Here are some functions for creating linked lists, written in Castile:
struct list {
value: integer;
next: list|void;
}
fun empty() {
return null as list|void
}
fun cons(v: integer, l: list|void) {
make list(value:v, next:l) as list|void
}
In this, list|void is a union type. In this case it is expressing
the fact that the value can be either a list or void — the moral
equivalent of "nullable". In order to access any of the concrete types
of a union type, one must use typecase:
fun max(l: list|void) {
u = l;
v = u;
n = 0;
while true {
typecase u is void {
break;
}
typecase u is list {
if u.value > n {
n = u.value
}
v = u.next;
}
u = v;
}
return n
}
This retains type-safety; the code will never unexpectedly be presented with a null value.
Union types can also encourage the programmer follow a Parse, don't validate
approach (which, despite the impression you might get from reading that article,
is not restricted to Haskell or even to functional programming). In the above
code, cons will never return a void, and max is not defined on empty lists.
So ideally, we'd like to tighten their types to exclude those. And we can:
...
fun cons(v: integer, l: list) {
make list(value:v, next:l as list|void)
}
fun singleton(v: integer) {
make list(value:v, next:null as list|void)
}
fun max(l: list) {
u = l as list|void;
v = u;
...
}
Many more examples of Castile programs can be found in tests/Castile.md.
Commit History
@master
git clone https://git.catseye.tc/Castile/
- Clean up AST.aux, allow empty structs in stackmac; all tests pass. catseye 13 years ago
- Types on every AST node; simpler AST structure; AST.copy(). catseye 13 years ago
- AST nodes have tags (names) and types (language-domain.) catseye 13 years ago
- Test for order not mattering in union types and struct creation. catseye 13 years ago
- Fix example programs' syntax. Add another exciting example. catseye 13 years ago
- Demo of "typed enum" and various notes. catseye 13 years ago
- Deal with voids in unions in stackmac. All tests pass! catseye 13 years ago
- Nicer (probably) syntax for type expressions. catseye 13 years ago
- Make grammar less verbose (in eval, tag based on Python type). catseye 13 years ago
- stackmac fixes that suggest passes are in the "wrong" order... catseye 13 years ago
- More builtins (int, str, chr, ord), in evaluator only for now. catseye 13 years ago
- Fix bugs in stackmac be; tag unions; implement typecase. catseye 13 years ago
- Make tests independent of tagged value representation. catseye 13 years ago
- Small fixes to stackmac. catseye 13 years ago
- Fix up stackmac a bit; rewrite tests to not have struct reprs. catseye 13 years ago
- Fix function lifter, examples, stackmac backend. catseye 13 years ago
- Local declarations must be first thing in function body. catseye 13 years ago
- AST transformation: lift all function bodies to toplevel. catseye 13 years ago
- This isn't much tidier. Hardly worth doing except to pass tests. catseye 13 years ago
- stackmac can say "Hello, world!" now, at least. catseye 13 years ago
- Expand on linked list example. catseye 13 years ago
- Flush out struct/union parts of the stackmac backend. catseye 13 years ago
- Tell stackmac the # of globals; "only" 17 fails now. catseye 13 years ago
- Save old baseptr. Don't push/return void (size=0) values. catseye 13 years ago
- Introduce clear_baseptr opcode, callee clears stack for return. catseye 13 years ago
- Read and write locals rel baseptr. "Only" 27 fails for stackmac. catseye 13 years ago
- Inching the number of failures down. Next: local or global? catseye 13 years ago
- Labels in stack machine; pass a small number more tests. catseye 13 years ago
- Stackmac backend now passes some tests. catseye 13 years ago
- Rename stack backend to stackmac; stub of an interpreter for it. catseye 13 years ago