From 25bc958e04878195347d27ecb7fe074dc3895911 Mon Sep 17 00:00:00 2001 From: Linus Lee Date: Sat, 24 Oct 2020 13:07:19 -0400 Subject: [PATCH] To make Tabloid truly Turing complete, add a way for Tabloid to take input --- static/js/lang.js | 26 ++++++++++++++++++++++++++ static/js/main.js | 3 +++ 2 files changed, 29 insertions(+) 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) {