summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Lee <linus@thesephist.com>2020-10-24 13:07:19 -0400
committerLinus Lee <linus@thesephist.com>2020-10-24 13:07:19 -0400
commit25bc958e04878195347d27ecb7fe074dc3895911 (patch)
tree07cde868afd9a1ae3fb0b183a63796554b813d31
parent2435a7cd5eb88ccd0dedd05ce66881c44e523bd4 (diff)
downloadtabloid-fake-closure-25bc958e04878195347d27ecb7fe074dc3895911.tar.gz
tabloid-fake-closure-25bc958e04878195347d27ecb7fe074dc3895911.zip
To make Tabloid truly Turing complete, add a way for Tabloid to take input
-rw-r--r--static/js/lang.js26
-rw-r--r--static/js/main.js3
2 files changed, 29 insertions, 0 deletions
diff --git a/static/js/lang.js b/static/js/lang.js
index 954b44d..50fe098 100644
--- a/static/js/lang.js
+++ b/static/js/lang.js
@@ -112,6 +112,7 @@ const T = {
ExpertsClaim: Symbol('ExpertsClaim'),
ToBe: Symbol('ToBe'),
YouWontWantToMiss: Symbol('YouWontWantToMiss'),
+ LatestNewsOn: Symbol('LatestNewsOn'),
TotallyRight: Symbol('TotallyRight'),
CompletelyWrong: Symbol('CompletelyWrong'),
IsActually: Symbol('IsActually'),
@@ -201,6 +202,12 @@ function tokenize(prog) {
tokens.push(T.YouWontWantToMiss);
break;
}
+ case 'LATEST': {
+ reader.expect('NEWS');
+ reader.expect('ON');
+ tokens.push(T.LatestNewsOn);
+ break;
+ }
case 'IS': {
reader.expect('ACTUALLY');
tokens.push(T.IsActually);
@@ -308,6 +315,7 @@ const N = {
ReturnExpr: Symbol('ReturnExpr'),
ProgEndExpr: Symbol('ProgEndExpr'),
PrintExpr: Symbol('PrintExpr'),
+ InputExpr: Symbol('InputExpr'),
}
class Parser {
@@ -334,6 +342,7 @@ class Parser {
* ReturnExpr
* ProgEndExpr
* PrintExpr
+ * InputExpr
*
*/
parse() {
@@ -485,6 +494,12 @@ class Parser {
type: N.PrintExpr,
val: this.expr(),
}
+ } else if (next === T.LatestNewsOn) {
+ // input expr
+ return {
+ type: N.InputExpr,
+ val: this.expr(),
+ }
}
this.tokens.backstep();
@@ -538,6 +553,7 @@ class Environment {
/**
* Runtime contains the following functions:
* - print(s)
+ * - input(s)
*/
this.runtime = runtime;
this.scopes = [{}]; // begin with global scope
@@ -666,6 +682,16 @@ class Environment {
this.runtime.print(val);
return val;
}
+ case N.InputExpr: {
+ let val = this.eval(node.val);
+ // shim for boolean to-string's
+ if (val === true) {
+ val = 'TOTALLY RIGHT';
+ } else if (val === false) {
+ val = 'COMPLETELY WRONG';
+ }
+ return this.runtime.input(val);
+ }
default:
console.log(JSON.stringify(node, null, 2));
throw new Error(`Runtime error: Unknown AST Node of type ${
diff --git a/static/js/main.js b/static/js/main.js
index 7cd8e26..d09c3c0 100644
--- a/static/js/main.js
+++ b/static/js/main.js
@@ -102,6 +102,9 @@ class Editor extends Component {
this.output += s.toString().toUpperCase() + '!\n';
this.render();
},
+ input: s => {
+ return prompt(s);
+ },
});
env.run(nodes);
} catch (e) {