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/
- Fix typo, add link. Chris Pressey 5 years ago
- Add BSD license. Chris Pressey 5 years ago
- Add aside for better framing for anyone who follows the link. Chris Pressey 5 years ago
- Rename the Python test appliances. Chris Pressey 5 years ago
- flake8 cleanups Chris Pressey 5 years ago
- Remove statements that belong in README. Chris Pressey 5 years ago
- Add another "see also" link to README. Chris Pressey 5 years ago
- Split out tests, design from README; rewrite what remains in it. Chris Pressey 5 years ago
- Allow promoting union type to itself, or to a bigger union type. Chris Pressey 5 years ago
- Each of the individual types named in the union type must be unique. Chris Pressey 5 years ago
- Update TODO, add some tests, which show there are things TODO. Chris Pressey 5 years ago
- Struct equality (actually more like "hash-consing") in stackmac. Chris Pressey 5 years ago
- Structural equality testing in all backends except stackmac. Chris Pressey 5 years ago
- Implement `ne` in `stackmac` implementation. Chris Pressey 5 years ago
- Update TODO Chris Pressey 5 years ago
- PEP-8 style cleanups. Chris Pressey 5 years ago
- Fix bug in FunctionLifter that was erasing empty StructDefns. Chris Pressey 5 years ago
- Allocate structs on the heap, not on the stack, in C backend. Chris Pressey 5 years ago
- Minimal name-mangling in Ruby backend. Demo "Parse, don't validate". Chris Pressey 5 years ago
- Get `typecase` finally working in the generated C code. Chris Pressey 5 years ago
- Generated C code with typecase at least compiles now. Chris Pressey 5 years ago
- Instead of this, we should rename the typecasted variable locally. Chris Pressey 5 years ago
- Generated C for typecase is still non-working, but it is prettier. Chris Pressey 5 years ago
- In C backend, generate field initializers in the correct order. Chris Pressey 5 years ago
- Implement `substr` and `len` in C backend. Chris Pressey 5 years ago
- Castile's void type is not the same as C's void type. Chris Pressey 5 years ago
- Slightly prettier code generation in C backend. Chris Pressey 5 years ago
- Disable some tests that fail under the C compiler backend for now. Chris Pressey 5 years ago
- Generate slightly prettier Ruby code in the Ruby backend. Chris Pressey 5 years ago
- Implement str builtin in Ruby, JavaScript, and stackmac backends. Chris Pressey 5 years ago