Browse Source

Implement &

Implemented the stack register, and added two example to test it. The
second example demonstrates its persistence between stacks.
ApisNecros 1 year ago
parent
commit
e00ca8c53e
2 changed files with 15 additions and 2 deletions
  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"
 c22g\           prints "5011012", or what should be read as "50", "110", and "12"
 /g32/
 /g32/
 \nnn;
 \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(); }
                     if(this.stacks[this.curr_stack].Pop() === 0){ this.Move(); }
                     break;
                     break;
                 // Stack manipulation
                 // 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 ":":
                 case ":":
                     this.stacks[this.curr_stack].Duplicate();
                     this.stacks[this.curr_stack].Duplicate();
                     break;
                     break;
@@ -351,7 +360,7 @@ class CodeBox {
                     output = true;
                     output = true;
                     break;
                     break;
                 default:
                 default:
-                    throw new Error();
+                    throw new Error(`Unknown instruction: ${instruction}`);
             }
             }
         }
         }
         catch(e) {
         catch(e) {