From 91b21db725540043640edda6f28b798f4ffcb7c5 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Mon, 23 Jan 2023 15:08:24 +0100 Subject: Add example of small functional language and run time system --- example.txt | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 example.txt (limited to 'example.txt') diff --git a/example.txt b/example.txt new file mode 100644 index 0000000..b83c606 --- /dev/null +++ b/example.txt @@ -0,0 +1,66 @@ +(data (Tuple a b) + (Tuple a b)) +(data (List a) + Nil + (Cons a (List a))) +(type String (List Char)) + +(fun readline : (List Char) + (case getc + '\n' -> Nil + c -> c : readline)) + +(fun length (xs : (List a)) : Int + (length_acc 0 xs)) +(fun length_acc (n : Int) (xs : (List a)) : Int + (case xs + Nil -> n + Cons _ xs -> length_acc (+ n 1) xs)) + +(data Type + (Type String) + (TypeVar String) + (TypeApp Type Type)) + +(data ConstructorDef + (ConstructorDef String (List Type))) + +(data BasicValue + (BVInt Int) + (BVChar Char)) + +(data Pattern + WildCard + (BasicValuePattern BasicValue) + (IdentPattern String) + (ConstructorPattern String (List Pattern))) + +(data CaseAlternative + (CaseAlternative Pattern Expression)) + +(data Expression + (Ident String) + (Case Expression (List CaseAlternative)) + (ExpApp Expression Expression)) + +(data Definition + (DataDef String (List String) (List ConstructorDef)) + (FunDef String (List (Tuple String Type)) Type Expression)) + +(fun list_ast : Definition + (DataDef + (Cons 'L' (Cons 'i' (Cons 's' (Cons 't' Nil)))) + (Cons (Cons 'a' Nil) Nil) + (Cons (ConstructorDef (Cons 'N' (Cons 'i' (Cons 'l' Nil))) Nil) + (Cons (ConstructorDef (Cons 'C' (Cons 'o' (Cons 'n' (Cons 's' Nil)))) (Cons (TypeVar (Cons 'a' Nil)) (Cons (TypeApp (Type (Cons 'L' (Cons 'i' (Cons 's' (Cons 't' Nil))))) (TypeVar (Cons 'a' Nil))) Nil))) + Nil)))) +(fun length_acc_ast : Definition + (FunDef + (Cons 'l' (Cons 'e' (Cons 'n' (Cons 'g' (Cons 't' (Cons 'h' (Cons '_' (Cons 'a' (Cons 'c' (Cons 'c' Nil)))))))))) + (Cons (Tuple (Cons 'n' Nil) (Type (Cons 'I' (Cons 'n' (Cons 't' Nil))))) + (Cons (Tuple (Cons 'x' (Cons 's' Nil)) (TypeApp (Type (Cons 'L' (Cons 'i' (Cons 's' (Cons 't' Nil))))) (TypeVar (Cons 'a' Nil)))) + Nil)) + (Case (Ident (Cons 'x' (Cons 's' Nil))) + (Cons (CaseAlternative (ConstructorPattern (Cons 'N' (Cons 'i' (Cons 'l' Nil))) Nil) (Ident (Cons 'n' Nil))) + (Cons (CaseAlternative (ConstructorPattern (Cons 'C' (Cons 'o' (Cons 'n' (Cons 's' Nil)))) (Cons WildCard (Cons (IdentPattern (Cons 'x' (Cons 's' Nil))) Nil))) ()) + Nil))))) -- cgit v1.2.3