Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Compilers, Introduction / coolc-spim / my-code / factorial2.cl
diff options
context:
space:
mode:
1 files changed, 24 insertions, 0 deletions
-rw-r--r--Year 3/Compilers, Introduction/coolc-spim/my-code/factorial2.cl24
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;
+ }
+ };
+};