Add fibonacci sequence sample

This commit is contained in:
Linus Lee 2020-09-24 08:16:49 -04:00
parent 528eb9c74d
commit e6797461f8
3 changed files with 65 additions and 40 deletions

View File

@ -185,7 +185,6 @@ textarea.editor-input:focus {
margin: 0; margin: 0;
padding: 0; padding: 0;
word-wrap: break-word; word-wrap: break-word;
white-space: pre-wrap;
} }
.output .no-output { .output .no-output {

View File

@ -115,10 +115,10 @@ const T = {
IsActually: Symbol('IsActually'), IsActually: Symbol('IsActually'),
And: Symbol('And'), And: Symbol('And'),
Or: Symbol('Or'), Or: Symbol('Or'),
Add: Symbol('Add'), Plus: Symbol('Plus'),
Subtract: Symbol('Subtract'), Minus: Symbol('Minus'),
Multiply: Symbol('Multiply'), Times: Symbol('Times'),
Divide: Symbol('Divide'), DividedBy: Symbol('DividedBy'),
Modulo: Symbol('Modulo'), Modulo: Symbol('Modulo'),
Beats: Symbol('Beats'), // > Beats: Symbol('Beats'), // >
SmallerThan: Symbol('SmallerThan'), // < SmallerThan: Symbol('SmallerThan'), // <
@ -130,10 +130,10 @@ const BINARY_OPS = [
T.IsActually, T.IsActually,
T.And, T.And,
T.Or, T.Or,
T.Add, T.Plus,
T.Subtract, T.Minus,
T.Multiply, T.Times,
T.Divide, T.DividedBy,
T.Modulo, T.Modulo,
T.Beats, T.Beats,
T.SmallerThan, T.SmallerThan,
@ -211,20 +211,21 @@ function tokenize(prog) {
tokens.push(T.Or); tokens.push(T.Or);
break; break;
} }
case 'ADD': { case 'PLUS': {
tokens.push(T.Add); tokens.push(T.Plus);
break; break;
} }
case 'SUBTRACT': { case 'MINUS': {
tokens.push(T.Subtract); tokens.push(T.Minus);
break; break;
} }
case 'MULTIPLY': { case 'TIMES': {
tokens.push(T.Multiply); tokens.push(T.Times);
break; break;
} }
case 'DIVIDE': { case 'DIVIDED': {
tokens.push(T.Divide); reader.expect('BY');
tokens.push(T.DividedBy);
break; break;
} }
case 'MODULO': { case 'MODULO': {
@ -337,6 +338,11 @@ class Parser {
while (this.tokens.hasNext()) { while (this.tokens.hasNext()) {
nodes.push(this.expr()); 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; return nodes;
} }
expectIdentString() { expectIdentString() {
@ -601,13 +607,13 @@ class Environment {
return left && right; return left && right;
case T.Or: case T.Or:
return left || right; return left || right;
case T.Add: case T.Plus:
return left + right; return left + right;
case T.Subtract: case T.Minus:
return left - right; return left - right;
case T.Multiply: case T.Times:
return left * right; return left * right;
case T.Divide: case T.DividedBy:
return left / right; return left / right;
case T.Modulo: case T.Modulo:
return left % right; return left % right;

View File

@ -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 DISCOVER HOW TO factorial WITH n
WE SAID WE SAID
WHAT IF n IS ACTUALLY 0 WHAT IF n IS ACTUALLY 0
WE SAID
SHOCKING DEVELOPMENT 1 SHOCKING DEVELOPMENT 1
END OF STORY LIES!
LIES! WE SAID SHOCKING DEVELOPMENT
SHOCKING DEVELOPMENT n MULTIPLY factorial OF n SUBTRACT 1 n TIMES factorial OF n MINUS 1
END OF STORY
END OF STORY END OF STORY
EXPERTS CLAIM result TO BE factorial OF 10 EXPERTS CLAIM result TO BE factorial OF 10
@ -17,6 +15,25 @@ YOU WON'T WANT TO MISS result
PLEASE LIKE AND SUBSCRIBE`; 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 = [ const HEADLINES = [
`You Won't Believe What This Programming Language Can Do!`, `You Won't Believe What This Programming Language Can Do!`,
`The Best Programming Language You Haven't Heard Of (It Will Surprise You!)`, `The Best Programming Language You Haven't Heard Of (It Will Surprise You!)`,
@ -42,10 +59,6 @@ class Editor extends Component {
this.errors = ''; this.errors = '';
this.handleRun = () => this.eval(); this.handleRun = () => this.eval();
this.handleReset = () => {
this.prog = PROG_DEFAULT;
this.render();
}
this.handleInput = evt => { this.handleInput = evt => {
this.prog = evt.target.value; this.prog = evt.target.value;
this.render(); 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() { eval() {
this.output = ''; this.output = '';
@ -85,9 +108,10 @@ class Editor extends Component {
compose() { compose() {
return jdom`<div class="editor fixed block"> return jdom`<div class="editor fixed block">
<div class="controls"> <div class="controls">
${this.prog === PROG_DEFAULT ? null : <button class="block"
jdom`<button class="block" onclick=${this.setFibonacci}>Fibonacci sample</button>
onclick=${this.handleReset}>Reset</button>`} <button class="block"
onclick=${this.setFactorial}>Factorial sample</button>
<button class="accent block" <button class="accent block"
onclick=${this.handleRun}>Run this!</button> onclick=${this.handleRun}>Run this!</button>
</div> </div>
@ -207,12 +231,7 @@ class App extends Component {
target="_blank">Ink</a>. target="_blank">Ink</a>.
</p> </p>
<h2>How much is there?</h2> <h2>How much is there?</h2>
<p> <p>Here's the full list of non-standard keywords that Tabloid currently uses:</p>
Here's the full list of non-standard keywords that Tabloid
currently uses, in addition to standard operators like <code
class="inline fixed block">PLUS</code> and <code class="inline
fixed block">MINUS</code>.
</p>
<ul> <ul>
<li><code class="inline fixed block">DISCOVER HOW TO...WITH</code> declare a function</li> <li><code class="inline fixed block">DISCOVER HOW TO...WITH</code> declare a function</li>
<li><code class="inline fixed block">WE SAID</code> begin a block scope</li> <li><code class="inline fixed block">WE SAID</code> begin a block scope</li>
@ -223,6 +242,7 @@ class App extends Component {
<li><code class="inline fixed block">YOU WON'T WANT TO MISS</code> print output</li> <li><code class="inline fixed block">YOU WON'T WANT TO MISS</code> print output</li>
<li><code class="inline fixed block">TOTALLY RIGHT</code> true</li> <li><code class="inline fixed block">TOTALLY RIGHT</code> true</li>
<li><code class="inline fixed block">COMPLETELY WRONG</code> false</li> <li><code class="inline fixed block">COMPLETELY WRONG</code> false</li>
<li><code class="inline fixed block">PLUS / MINUS / TIMES / DIVIDED BY / MODULO</code> the obvious arithmetic operations</li>
<li><code class="inline fixed block">IS ACTUALLY</code> is equal to</li> <li><code class="inline fixed block">IS ACTUALLY</code> is equal to</li>
<li><code class="inline fixed block">BEATS / SMALLER THAN</code> greater than / less than</li> <li><code class="inline fixed block">BEATS / SMALLER THAN</code> greater than / less than</li>
<li><code class="inline fixed block">SHOCKING DEVELOPMENT</code> return from a function</li> <li><code class="inline fixed block">SHOCKING DEVELOPMENT</code> return from a function</li>