From e6797461f8372c6b0740d75ee73015f6ec561a68 Mon Sep 17 00:00:00 2001 From: Linus Lee Date: Thu, 24 Sep 2020 08:16:49 -0400 Subject: [PATCH] Add fibonacci sequence sample --- static/css/main.css | 1 - static/js/lang.js | 46 +++++++++++++++++++---------------- static/js/main.js | 58 ++++++++++++++++++++++++++++++--------------- 3 files changed, 65 insertions(+), 40 deletions(-) diff --git a/static/css/main.css b/static/css/main.css index bb63995..0e920b8 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -185,7 +185,6 @@ textarea.editor-input:focus { margin: 0; padding: 0; word-wrap: break-word; - white-space: pre-wrap; } .output .no-output { diff --git a/static/js/lang.js b/static/js/lang.js index 0782e64..a69a843 100644 --- a/static/js/lang.js +++ b/static/js/lang.js @@ -115,10 +115,10 @@ const T = { IsActually: Symbol('IsActually'), And: Symbol('And'), Or: Symbol('Or'), - Add: Symbol('Add'), - Subtract: Symbol('Subtract'), - Multiply: Symbol('Multiply'), - Divide: Symbol('Divide'), + Plus: Symbol('Plus'), + Minus: Symbol('Minus'), + Times: Symbol('Times'), + DividedBy: Symbol('DividedBy'), Modulo: Symbol('Modulo'), Beats: Symbol('Beats'), // > SmallerThan: Symbol('SmallerThan'), // < @@ -130,10 +130,10 @@ const BINARY_OPS = [ T.IsActually, T.And, T.Or, - T.Add, - T.Subtract, - T.Multiply, - T.Divide, + T.Plus, + T.Minus, + T.Times, + T.DividedBy, T.Modulo, T.Beats, T.SmallerThan, @@ -211,20 +211,21 @@ function tokenize(prog) { tokens.push(T.Or); break; } - case 'ADD': { - tokens.push(T.Add); + case 'PLUS': { + tokens.push(T.Plus); break; } - case 'SUBTRACT': { - tokens.push(T.Subtract); + case 'MINUS': { + tokens.push(T.Minus); break; } - case 'MULTIPLY': { - tokens.push(T.Multiply); + case 'TIMES': { + tokens.push(T.Times); break; } - case 'DIVIDE': { - tokens.push(T.Divide); + case 'DIVIDED': { + reader.expect('BY'); + tokens.push(T.DividedBy); break; } case 'MODULO': { @@ -337,6 +338,11 @@ class Parser { while (this.tokens.hasNext()) { nodes.push(this.expr()); } + + if (nodes[nodes.length - 1].type !== N.ProgEndExpr) { + throw new Error('Parsing error: A Tabloid program MUST end with PLEASE LIKE AND SUBSCRIBE'); + } + return nodes; } expectIdentString() { @@ -601,13 +607,13 @@ class Environment { return left && right; case T.Or: return left || right; - case T.Add: + case T.Plus: return left + right; - case T.Subtract: + case T.Minus: return left - right; - case T.Multiply: + case T.Times: return left * right; - case T.Divide: + case T.DividedBy: return left / right; case T.Modulo: return left % right; diff --git a/static/js/main.js b/static/js/main.js index 6dc22d3..debad58 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,14 +1,12 @@ -const PROG_DEFAULT = `YOU WON'T WANT TO MISS 'Hello, World!' +const PROG_FACTORIAL = `YOU WON'T WANT TO MISS 'Hello, World!' DISCOVER HOW TO factorial WITH n WE SAID WHAT IF n IS ACTUALLY 0 - WE SAID SHOCKING DEVELOPMENT 1 - END OF STORY - LIES! WE SAID - SHOCKING DEVELOPMENT n MULTIPLY factorial OF n SUBTRACT 1 - END OF STORY + LIES! + SHOCKING DEVELOPMENT + n TIMES factorial OF n MINUS 1 END OF STORY EXPERTS CLAIM result TO BE factorial OF 10 @@ -17,6 +15,25 @@ YOU WON'T WANT TO MISS result PLEASE LIKE AND SUBSCRIBE`; +const PROG_FIBONACCI = `DISCOVER HOW TO fibonacci WITH a, b, n +WE SAID + WHAT IF n SMALLER THAN 1 + SHOCKING DEVELOPMENT b + LIES! WE SAID + YOU WON'T WANT TO MISS b + SHOCKING DEVELOPMENT + fibonacci OF b, a PLUS b, n MINUS 1 + END OF STORY +END OF STORY + +EXPERTS CLAIM limit TO BE 10 +YOU WON'T WANT TO MISS 'First 10 Fibonacci numbers' +EXPERTS CLAIM nothing TO BE fibonacci OF 1, 1, limit + +PLEASE LIKE AND SUBSCRIBE`; + +const PROG_DEFAULT = PROG_FIBONACCI; + const HEADLINES = [ `You Won't Believe What This Programming Language Can Do!`, `The Best Programming Language You Haven't Heard Of (It Will Surprise You!)`, @@ -42,10 +59,6 @@ class Editor extends Component { this.errors = ''; this.handleRun = () => this.eval(); - this.handleReset = () => { - this.prog = PROG_DEFAULT; - this.render(); - } this.handleInput = evt => { this.prog = evt.target.value; this.render(); @@ -63,6 +76,16 @@ class Editor extends Component { } } } + this.setFactorial = () => { + this.prog = PROG_FACTORIAL; + this.output = this.errors = ''; + this.render(); + } + this.setFibonacci= () => { + this.prog = PROG_FIBONACCI; + this.output = this.errors = ''; + this.render(); + } } eval() { this.output = ''; @@ -85,9 +108,10 @@ class Editor extends Component { compose() { return jdom`
- ${this.prog === PROG_DEFAULT ? null : - jdom``} + +
@@ -207,12 +231,7 @@ class App extends Component { target="_blank">Ink.

How much is there?

-

- Here's the full list of non-standard keywords that Tabloid - currently uses, in addition to standard operators like PLUS and MINUS. -

+

Here's the full list of non-standard keywords that Tabloid currently uses: