Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 15, 2022
1 parent 784e5e6 commit bf63701
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
23 changes: 23 additions & 0 deletions eg/length.castile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct list {
value: string;
next: list|void;
} for (cons, singleton, len)

fun cons(v: string, l: list) {
make list(value:v, next:l as list|void)
}

fun singleton(v: string) {
make list(value:v, next:null as list|void)
}

length : list|void -> integer
fun length(l: list|void) {
typecase l is void { return 0 }
typecase l is list { return 1 + length(l.next) }
}

fun main() {
l = cons("first", cons("second", singleton("third")));
print(str(length(l as list|void)));
}
8 changes: 7 additions & 1 deletion src/castile/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def main(argv):
if options.typecheck:
t = TypeChecker()
t.collect_structs(ast)
t.type_of(ast)
try:
t.type_of(ast)
except Exception:
if options.show_ast:
print(ast.pprint(0))
print("-----")
raise
if options.compile_to is not None:
x = FunctionLifter()
ast = x.lift_functions(ast)
Expand Down
28 changes: 14 additions & 14 deletions tests/Castile.md
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ is accomplished.
| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, empty)
| } for (cons, singleton, len)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand All @@ -1272,22 +1272,22 @@ is accomplished.
| make list(value:v, next:null as list|void)
| }
|
| fun empty(l: list|void) {
| a = "no";
| typecase l is void { a = "yes"; }
| return a;
| len : list|void -> integer
| fun length(l: list|void) {
| typecase l is void { return 0 }
| typecase l is list { return 1 + length(l.next) }
| }
|
| fun main() {
| l = cons("first", cons("second", singleton("third")));
| print(empty(l));
| print(str(length(l as list|void)));
| }
= no
= 3

| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, empty)
| } for (cons, singleton, len)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand All @@ -1297,22 +1297,22 @@ is accomplished.
| make list(value:v, next:null as list|void)
| }
|
| fun empty(l: list|void) {
| a = "no";
| typecase l is void { a = "yes"; }
| return a;
| len : list|void -> integer
| fun length(l: list|void) {
| typecase l is void { return 0 }
| typecase l is list { return 1 + length(l.next) }
| }
|
| fun main() {
| l = make list(value:"first", next:null);
| print(empty(l));
| print(str(length(l)));
| }
? make

| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, empty)
| } for (cons, singleton, len)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand Down

0 comments on commit bf63701

Please sign in to comment.