Summary Files Commits Branches @commit @diff @stats
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
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.tar
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.tar.gz
uni-projects-c43a57130601e63ccdd70b0dfd0a5d2bf5bef3a6.zip
Parent3ba593c0c4da762969a48a5c89c4a096e93680a5 (diff)
[CI] Added docker setup
diff --git a/.gitignore b/.gitignore
index 6dcc188..52aca6e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,8 @@ Year 2/Implementation of Programming Languages/*.tab.*
Year 2/Implementation of Programming Languages/**/*.tab.*
Year 2/Implementation of Programming Languages/*.ghc
Year 2/Implementation of Programming Languages/**/*.ghc
+Year 3/Compilers, Introduction/coolc-spim/class-code/
+Year 3/Compilers, Introduction/coolc-spim/**/*.s
# ---
# C++
diff --git a/Year 3/Compilers, Introduction/coolc-spim/README.md b/Year 3/Compilers, Introduction/coolc-spim/README.md
new file mode 100644
index 0000000..4dc3459
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/README.md
@@ -0,0 +1,6 @@
+This directory contains a docker container description, example programs in
+COOL, and supporting code. Install docker, make sure dockerd is up and running,
+and use run.sh to start the container. You now have a shell into the container.
+Print the README.md inside there for more instructions.
+
+Once you leave the shell, stop the container and remove the image via stop.sh.
diff --git a/Year 3/Compilers, Introduction/coolc-spim/docker-container/Dockerfile b/Year 3/Compilers, Introduction/coolc-spim/docker-container/Dockerfile
new file mode 100644
index 0000000..86a00d9
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/docker-container/Dockerfile
@@ -0,0 +1,16 @@
+FROM debian:latest
+
+RUN apt-get update && \
+ apt-get upgrade -y && \
+ apt-get clean
+
+RUN apt install spim build-essential -y
+
+RUN mkdir /usr/class
+RUN mkdir /usr/code
+
+ENV PATH="/usr/class/cs143/bin:${PATH}"
+
+WORKDIR /usr/code
+
+CMD /bin/bash
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/README.md b/Year 3/Compilers, Introduction/coolc-spim/my-code/README.md
new file mode 100644
index 0000000..6b78621
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/README.md
@@ -0,0 +1,36 @@
+This is a shared directory between the docker container and the hosting machine.
+Edit the files at your convenience on the host machine and then compile and run
+from the container.
+
+To try the included code, do the following:
+
+Copy the `atoi.cl` file provided by the course materials
+(`/usr/class/cs143/assignments/PA1/atoi.cl`) in the same working directory as
+where you copy these three files and run them as follows:
+
+```
+$ # This runs the first implementation of factorial
+$ coolc factorial1.cl atoi.cl
+$ spim factorial1.s
+```
+
+The program waits for a number input and then outputs the factorial.
+
+```
+$ # This runs the second implementation of factorial
+$ coolc factorial2.cl atoi.cl
+$ spim factorial2.s
+```
+
+The UI is the same.
+
+```
+$ # This runs the program implementing a linked list
+$ coolc list.cl atoi.cl
+$ spim list.s
+```
+
+The program waits for no input, but prints "Hello World! 42".
+
+If `list.cl` is modified to uncomment the two lines that add the `abort`
+variable to the list, running the program will abort and not print anything.
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial1.cl b/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial1.cl
new file mode 100644
index 0000000..31fdb2a
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial1.cl
@@ -0,0 +1,18 @@
+(* 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 Main inherits A2I {
+ main(): Object {
+ (new IO).out_string(
+ i2a(
+ fact(
+ a2i((new IO).in_string())
+ )
+ ).concat("\n")
+ )
+ };
+
+ fact(i: Int): Int {
+ if i = 0 then 1 else i * fact(i - 1) fi
+ };
+};
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial2.cl b/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial2.cl
new file mode 100644
index 0000000..699acfc
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/factorial2.cl
@@ -0,0 +1,24 @@
+(* 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 Main inherits A2I {
+ main(): Object {
+ (new IO).out_string(
+ i2a(
+ fact(
+ a2i((new IO).in_string())
+ )
+ ).concat("\n")
+ )
+ };
+
+ fact(i: Int): Int {
+ let fact: Int <- 1 in {
+ while not (i = 0) loop {
+ fact <- fact * i;
+ i <- i - 1;
+ } pool;
+ fact;
+ }
+ };
+};
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())
+ };
+};
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/test.cl b/Year 3/Compilers, Introduction/coolc-spim/my-code/test.cl
new file mode 100644
index 0000000..dbe8274
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/test.cl
@@ -0,0 +1,7 @@
+class Main {
+ io: IO <- new IO;
+
+ main(): Object {
+ io.out_string("Hello World\n")
+ };
+};
diff --git a/Year 3/Compilers, Introduction/coolc-spim/run.sh b/Year 3/Compilers, Introduction/coolc-spim/run.sh
new file mode 100755
index 0000000..d0eff12
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/run.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+set -x
+
+printf "\033[1;32mMake sure dockerd is running before running this script.\n\033[00m"
+pushd docker-container
+docker build . -t coolc-spim
+popd
+docker run --name coolc-spim -v $(pwd)/class-code:/usr/class -v $(pwd)/my-code:/usr/code -d coolc-spim tail -f /dev/null
+docker exec -it coolc-spim bash
diff --git a/Year 3/Compilers, Introduction/coolc-spim/stop.sh b/Year 3/Compilers, Introduction/coolc-spim/stop.sh
new file mode 100755
index 0000000..eec83f7
--- /dev/null
+++ b/Year 3/Compilers, Introduction/coolc-spim/stop.sh
@@ -0,0 +1 @@
+docker stop coolc-spim && docker remove coolc-spim