From b2d0d0bb7a9e105dcf8770e76df72a837777c9a5 Mon Sep 17 00:00:00 2001 From: PartMan Date: Sun, 9 Apr 2023 15:12:27 +0530 Subject: [PATCH 1/2] Added EVIDENCE SHOWS for variable reassignment --- static/js/lang.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/static/js/lang.js b/static/js/lang.js index 50fe098..f38d98b 100644 --- a/static/js/lang.js +++ b/static/js/lang.js @@ -110,6 +110,7 @@ const T = { LiesBang: Symbol('LiesBang'), EndOfStory: Symbol('EndOfStory'), ExpertsClaim: Symbol('ExpertsClaim'), + EvidenceShows: Symbol('EvidenceShows'), ToBe: Symbol('ToBe'), YouWontWantToMiss: Symbol('YouWontWantToMiss'), LatestNewsOn: Symbol('LatestNewsOn'), @@ -189,6 +190,11 @@ function tokenize(prog) { tokens.push(T.ExpertsClaim); break; } + case 'EVIDENCE': { + reader.expect('SHOWS'); + tokens.push(T.EvidenceShows); + break; + } case 'TO': { reader.expect('BE'); tokens.push(T.ToBe); @@ -477,6 +483,17 @@ class Parser { name, val, } + } else if (next === T.EvidenceShows) { + // assignment + const name = this.expectIdentString(); + this.tokens.expect(T.ToBe); + const val = this.expr(); + return { + type: N.Assignment, + name, + val, + lookup: true, + } } else if (next === T.ShockingDevelopment) { // return return { @@ -613,7 +630,16 @@ class Environment { throw new Error(`Runtime error: Undefined variable "${node.val}"`); } case N.Assignment: { - scope[node.name] = this.eval(node.val); + if (node.lookup) { + let i = this.scopes.length - 1; + while (i >= 0) { + if (node.name in this.scopes[i]) { + return this.scopes[i][node.name] = this.eval(node.val); + } + i --; + } + throw new Error(`Runtime error: Variable "${node.name}" does not exist`); + } else scope[node.name] = this.eval(node.val); return scope[node.name]; } case N.BinaryOp: { From 8d55d6312dc77cb6f37f90027fe61706ad64c22a Mon Sep 17 00:00:00 2001 From: PartMan Date: Sun, 9 Apr 2023 15:15:31 +0530 Subject: [PATCH 2/2] Add docs for EVIDENCE SHOWS --- README.md | 7 +++++++ static/js/main.js | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d8cecb..ca0bacb 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,13 @@ Tabloid supports binary infix operators for arithmetic and logic. `IS ACTUALLY` is a way to test equality, like `==` in most other languages. We can also make comparisons with `X BEATS Y` (`x > y`) and `X SMALLER THAN Y` (`x < y`). +We can initialize and assign variables in the current scope with `EXPERTS CLAIM...TO BE`, and reassign variables using `EVIDENCE SHOWS...TO BE`. + +``` +EXPERTS CLAIM your_mom TO BE 'big' +EVIDENCE SHOWS your_mom TO BE 'massive' +``` + We can print the result of any expression with `YOU WON'T WANT TO MISS`. You won't want to miss what you're printing, and now you never will! ``` diff --git a/static/js/main.js b/static/js/main.js index e14b8a6..1af7975 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -271,7 +271,11 @@ class App extends Component {
  • EXPERTS CLAIM...TO BE - declare or assign to a variable + declare or assign to a variable (in the current scope) +
  • +
  • + EVIDENCE SHOWS...TO BE + reassign a variable
  • YOU WON'T WANT TO MISS