Parcourir la source

Implement &

Implemented the stack register, and added two example to test it. The
second example demonstrates its persistence between stacks.
ApisNecros il y a 1 an
Parent
commit
e00ca8c53e
2 fichiers modifiés avec 15 ajouts et 2 suppressions
  1. 5 1
      examples.txt
  2. 10 1
      starfish.js

+ 5 - 1
examples.txt

@@ -19,4 +19,8 @@ abcdef0983[D3[DIID]I];  prints nothing, and ends with the stack in the same orde
 c22g\           prints "5011012", or what should be read as "50", "110", and "12"
 /g32/
 \nnn;
-  2
+  2
+
+f:&&1+nn;       prints "1615"
+
+abcdefr&r3[D&n; prints "10"

+ 10 - 1
starfish.js

@@ -290,6 +290,15 @@ class CodeBox {
                     if(this.stacks[this.curr_stack].Pop() === 0){ this.Move(); }
                     break;
                 // Stack manipulation
+                case "&": {
+                    if (this.stacks[this.curr_stack].register == null) {
+                        this.stacks[this.curr_stack].register = this.stacks[this.curr_stack].Pop();
+                    }
+                    else {
+                        this.stacks[this.curr_stack].Push(this.stacks[this.curr_stack].register);
+                        this.stacks[this.curr_stack].register = null;
+                    }
+                }
                 case ":":
                     this.stacks[this.curr_stack].Duplicate();
                     break;
@@ -351,7 +360,7 @@ class CodeBox {
                     output = true;
                     break;
                 default:
-                    throw new Error();
+                    throw new Error(`Unknown instruction: ${instruction}`);
             }
         }
         catch(e) {