about summary refs log tree commit diff stats
path: root/subx/apps
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-09-17 22:57:10 -0700
committerKartik Agaram <vc@akkartik.com>2018-09-17 22:57:58 -0700
commitf09280141f18fbe8cef0ed576cf932e12e315666 (patch)
treed00962b07cb013f89d4fdb2fcf19c392afb62b5c /subx/apps
parent0a7b03727a736f73c16d37b22afef8496c60d657 (diff)
downloadmu-f09280141f18fbe8cef0ed576cf932e12e315666.tar.gz
4548: start of a compiler for a new experimental low-level language
Diffstat (limited to 'subx/apps')
-rw-r--r--subx/apps/factorial.k221
1 files changed, 21 insertions, 0 deletions
diff --git a/subx/apps/factorial.k2 b/subx/apps/factorial.k2
new file mode 100644
index 00000000..82c44352
--- /dev/null
+++ b/subx/apps/factorial.k2
@@ -0,0 +1,21 @@
+fn factorial n : int -> result/EAX : int [
+  result/EAX <- copy 1
+  {
+    compare n, 1
+    break-if <=
+    var tmp/EBX : int
+    tmp/EBX <- copy n
+    tmp/EBX <- subtract 1
+    var tmp2/EAX : int
+    tmp2/EAX <- call factorial, tmp/EBX
+    result/EAX <- multiply tmp2/EAX, n
+  }
+  return result/EAX
+]
+
+data structures:
+
+add entry for "factorial" into the Types table, with value (fn int -> int)
+add entry for "factorial" into the address table, with value next available address
+add entry for "factorial" into the size table, with value size of "0b 0a bb ..."
+increase next available address by size of "factorial"
ref='#n151'>151 152 153