Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Compilers, Introduction / coolc-spim / my-code / list.cl
diff options
context:
space:
mode:
Commitc43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-10-18 18:26:05 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-10-18 18:26:05 +0300
Filesc47c0efa808971c2569bb6ed25707747853b380e /Year 3/Compilers, Introduction/coolc-spim/my-code/list.cl
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.tar
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.tar.gz
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.zip
Parent3ba593c0c4da762969a48a5c89c4a096e93680a5 (diff)
[CI] Added docker setup
1 files changed, 53 insertions, 0 deletions
-rw-r--r--Year 3/Compilers, Introduction/coolc-spim/my-code/list.cl53
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/list.cl b/Year 3/Compilers, Introduction/coolc-spim/my-code/list.cl
new file mode 100644
index 0000000..374e600
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/list.cl
@@ -0,0 +1,53 @@
+(* Although this is written from scratch, it is hard to end up with something
+ much different from what is presented in the original Stanford cs143 course.
+ So credit for this code goes to Alex Aiken, creator of that course. *)
+class List {
+ item: Object;
+ next: List;
+
+ init(i: Object, n: List): List {
+ {
+ item <- i;
+ next <- n;
+ self;
+ }
+ };
+
+ flatten(): String {
+ let string: String <-
+ case item of
+ i: Int => (new A2I).i2a(i);
+ s: String => s;
+ o: Object => { abort(); ""; };
+ esac
+ in
+ if isvoid next
+ then string
+ else string.concat(next.flatten())
+ fi
+ };
+};
+
+class Main inherits IO {
+ main(): Object {
+ let hello: String <- "Hello ",
+ world: String <- "World! ",
+ answer: Int <- 42,
+ newline: String <- "\n",
+ -- if abort is added to the list, program will abort
+ abort: Object <- new Object,
+ nil: List,
+ list: List <- (new List).init(hello,
+ (new List).init(world,
+ (new List).init(answer,
+ (new List).init(newline,
+ -- (new List).init(abort,
+ nil)
+ --)
+ )
+ )
+ )
+ in
+ out_string(list.flatten())
+ };
+};