Skip to content

Commit

Permalink
Use new line number on error reporting to find a few type errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 24, 2022
1 parent 7179e74 commit bea6ad2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Castile 0.5

### Implementation

* Line numbers are recorded in the AST when parsing, and
reported on type errors when type errors occur.
* Requesting the AST be dumped, will also dump the AST with
type assignments, if an error occurs during type checking.
* Established an abstract base class for compiler backends.
Expand Down
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ stackmac: store tagged values as two values on the stack.
and void types in unions of (void, X) should only be one value.
(structs are still boxed though)

AST nodes should have source line numbers, it would be really nice.

Implement garbage collection of some sort in the C backend. Either that
or implement some kind of resource-awareness in the language itself.

Expand Down
37 changes: 37 additions & 0 deletions eg/assoc2.castile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
struct assoc {
key: string;
value: string;
next: assoc|void;
} for (update, lookup, remove)

fun empty() {
return null as assoc|void
}

fun update(k: string, v: string, a: assoc|void) {
make assoc(key:k, value:v, next:a as assoc|void)
}

lookup : assoc|void, string -> string|void
fun lookup(a: assoc|void, k: string) {
typecase a is void {
return null as string|void
}
typecase a is assoc {
if a.key == k {
return a.value as string|void
}
return lookup(a.next, k)
}
}

fun main() {
a = update("3", "third", empty());
a = update("2", "second", a as assoc|void);
a = update("1", "first", a as assoc|void);
r = lookup((a as assoc|void), "2");
print("um");
typecase r is void { print("NOT FOUND"); }
typecase r is string { print(r); }
print("ya");
}
23 changes: 0 additions & 23 deletions eg/length.castile

This file was deleted.

8 changes: 8 additions & 0 deletions eg/equal-linkedlist.castile → eg/linkedlist-ops.castile
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ fun equal_list(a: list|void, b: list|void) {
return false
}

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

main = fun() {
l1 = cons("first", cons("second", cons("third", empty())));
l2 = cons("first", cons("second", cons("third", empty())));
l3 = cons("first", cons("second", empty()));

print(str(length(l1 as list|void)));

if (equal_list(l1, l2) and not equal_list(l2, l3)) {
print("Yep, story checks out")
}
Expand Down

0 comments on commit bea6ad2

Please sign in to comment.