Implement BST
authorAlyssa Rosenzweig <alyssa@backtick.town>
Thu, 23 Jun 2016 16:02:25 +0000 (09:02 -0700)
committerAlyssa Rosenzweig <alyssa@backtick.town>
Thu, 23 Jun 2016 16:02:25 +0000 (09:02 -0700)
src/CPU/build-cpu.js

index 82a3b42..58dac1d 100644 (file)
@@ -19,16 +19,37 @@ var emission = [
     "set tmp to join \"0x\" (M)"
 ];
 
-var sources = table.map(function(x) {
+var sources = table.map(function(x, i) {
     if(x) {
-        return "legit";
+        return "legit " + i;
     } else {
-        return "illegit";
-    });
+        return "illegit " + i;
+    }
+});
 
-// dump out a 8 level deep BST
-console.log(bst(sources, 0, 256));
+// dump out an 8 level deep BST
+console.log(bst(sources, 0, 7));
 
 function bst(sources, start, end) {
-    
+    console.log(start + " - " + end);
+    if(start == end)
+        return [sources[start]];
+
+    if(start + 1 == end)
+        return [
+            "if tmp = " + start + " then",
+                sources[start],
+            "else",
+                sources[end]
+            ];
+
+    var emission = [
+        "if tmp < " + (start+end+1)/2 + " then",
+            bst(sources, start, start + (end-start-1) / 2),
+        "else",
+            bst(sources, start + (end-start+1) / 2, end),
+        "end"
+    ];
+
+    return emission;
 }
This page took 0.023027 seconds and 4 git commands to generate.