From bf63701dfaa06f0f6d7d60111278fbce8e6d2ac1 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Tue, 15 Feb 2022 12:00:21 +0000 Subject: [PATCH] Checkpoint. --- eg/length.castile | 23 +++++++++++++++++++++++ src/castile/main.py | 8 +++++++- tests/Castile.md | 28 ++++++++++++++-------------- 3 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 eg/length.castile diff --git a/eg/length.castile b/eg/length.castile new file mode 100644 index 0000000..1a1a02f --- /dev/null +++ b/eg/length.castile @@ -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))); +} diff --git a/src/castile/main.py b/src/castile/main.py index 3efd9cd..849c760 100644 --- a/src/castile/main.py +++ b/src/castile/main.py @@ -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) diff --git a/tests/Castile.md b/tests/Castile.md index 855f14b..428d856 100644 --- a/tests/Castile.md +++ b/tests/Castile.md @@ -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) @@ -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) @@ -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)