Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyck committed Jun 30, 2022
1 parent 8689b2d commit aee2009
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Functions pop a certain amount of items off the stack perform an operation, then
`d'` - Set the stack to the top item on the stack
`U'` - Pop the stack
`l'` - Swap the top two items on the stack
`d` - Push each item of list to the stack, (similar to `...` in JS)
`d` - Push each item of list to the stack, (similar to `...` in JS)
`f2` - Exit the program
`l` - Length of `a`
`f` - Floor the top item on the stack
Expand All @@ -76,7 +76,7 @@ Conditionals are written as `x <conditional code> x2 <optional conditional code>
For loops are created by pushing something to iterate to the stack, then pushing the name of your loop variable (as a string) to the stack. You can then use the enclose the code you want to loop in `y` and `y'`. To get the value of the loop variable, push it's name to the stack, then use the `y2` command. To break the loop, use `S2`.

## While Loops
To create a while loop, use `z <condition> z2 <code> z'`, The code will run until the condition is false. Exiting the loop is the same as exiting a for loop - `S2`.
To create a while loop, use `z <condition> z2 <code> z'`, The code will run until the condition is false. Exiting the loop is the same as exiting a for loop - `S2`. To make the loop run forever, leave the condition empty (in which case the `z2` is no longer required) or provide it with a value that will always be truthy.

## A Final Note
If you actually just read all of that, I'm sorry.
6 changes: 3 additions & 3 deletions cubestack/cubestack.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ var compile = (tokens, input = "", options = {}) => {
if (options.platform == "node") {
compiled.push(`process.stdout.write(toPrint + ${JSON.stringify(options.lineEnding)});`);
} else if (options.platform == "web") {
compiled.push(`postMessage(toPrint + ${JSON.stringify(options.lineEnding)});`);
compiled.push(`postMessage({ message: toPrint + ${JSON.stringify(options.lineEnding)}, type: "output" });`);
}
}

Expand Down Expand Up @@ -306,7 +306,7 @@ var compile = (tokens, input = "", options = {}) => {
);
} else if (token.moves == "f2") { // exit the program
if (options.platform == "node") compiled.push(`process.exit(0);`);
else compiled.push(`postMessage("exit");`);
else compiled.push(`postMessage({ message: "exit", type: "control" });`);
} else if (token.moves == "y2") { // get a loop variable
compiled.push(`${options.stackName}.push(loopVars[${options.stackName}.pop()]);`);
} else if (token.moves == "S2") { // break a loop
Expand Down Expand Up @@ -370,7 +370,7 @@ var compile = (tokens, input = "", options = {}) => {
if (options.platform == "node") {
compiled.push(`process.stdout.write("error:\\n ${token.value}\\n ${token.name}\\n");`);
} else if (options.platform == "web") {
compiled.push(`postMessage("error:\\n ${token.name}\\n ${token.value}\\n");`);
compiled.push(`postMessage({ message: "error:\\n ${token.name}\\n ${token.value}\\n", type: "output" });`);
}
break;
}
Expand Down
18 changes: 10 additions & 8 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,24 @@ if (navigator.userAgent.indexOf("Safari") >= 0 && navigator.userAgent.indexOf("C

var run = () => {
var compiled = compile(lex($("#code-display").innerText), $("#input-textarea").value, { platform: "web" });
console.log(`var stack = []; fetch("https://raw.githubusercontent.com/tobyck/cubestack/master/cubestack/stdlib.js").then(response => response.text()).then(text => { eval(text); ${compiled} });`)
var blob = new Blob([ `var stack = []; fetch("https://raw.githubusercontent.com/tobyck/cubestack/master/cubestack/stdlib.js").then(response => response.text()).then(text => {
eval(text);
${compiled}
});` ], { type: "text/javascript" });
var blob = new Blob([ `fetch("https://raw.githubusercontent.com/tobyck/cubestack/master/cubestack/stdlib.js").then(response => response.text()).then(text => { eval(text); ${compiled} });` ], { type: "text/javascript" });
var blobURL = URL.createObjectURL(blob);
var worker = new Worker(blobURL);

setTimeout(() => {
worker.terminate();
}, 5000);

$("#output").innerText = "";
$("#output").innerText = "\n";
var started = false;
worker.onmessage = event => {
if (event.data == "exit") worker.terminate();
else $("#output").innerText += event.data;
if (!started) {
$("#output").innerText = "";
started = true;
}

if (event.data.type == "output") $("#output").innerText += event.data.message;
else if (event.data.message == "exit") worker.terminate();
}
}

Expand Down

0 comments on commit aee2009

Please sign in to comment.