Преглед изворни кода

Add functionality for the initial stack

Added the ability for users to place a value in the initial stack. On
run, this value is parsed and placed onto the stack.
ApisNecros пре 1 година
родитељ
комит
cf777bdf6d
2 измењених фајлова са 28 додато и 1 уклоњено
  1. 4 1
      examples.txt
  2. 24 0
      starfish.js

+ 4 - 1
examples.txt

@@ -29,4 +29,7 @@ abcdefr&r3[D&n; 	prints "10"
 
 s":"m":"hnonon;		prints whatever your current time is
 
-aa*Sfn;				prints "15" after 10 seconds
+aa*Sfn;				prints "15" after 10 seconds
+
+    r\        prints the string in the initial stack
+o;!?l<

+ 24 - 0
starfish.js

@@ -135,9 +135,33 @@ class CodeBox {
         this.maxBoxWidth = maxRowLength - 1;
         this.maxBoxHeight = this.box.length - 1;
 
+        if (this.initialStackDOM.value != "") {
+            this.ParseInitialStack();
+        }
+
         this.Run();
     }
 
+    /**
+     * Parse the value provided for the stack at run time
+     */
+    ParseInitialStack() {
+        const separator = /(["'].+?["']|\d+)/g;
+        const stackValues = this.initialStackDOM.value.split(separator).filter((v) => v.trim().length);
+
+        for (const val of stackValues) {
+            const intVal = parseInt(val);
+            if (!Number.isNaN(intVal)) {
+                this.stacks[this.curr_stack].Push(intVal);
+            }
+            else {
+                let chars = val.substr(1, val.length - 2).split('');
+                chars = chars.map((c) => dec(c));
+                this.stacks[this.curr_stack].Push(chars);
+            }
+        }
+    }
+
     /**
      * Make all the rows in the code box the same length
      *