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;
padding: 0;
word-wrap: break-word;
white-space: pre-wrap;
}
.output .no-output {

View File

@ -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;

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
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`<div class="editor fixed block">
<div class="controls">
${this.prog === PROG_DEFAULT ? null :
jdom`<button class="block"
onclick=${this.handleReset}>Reset</button>`}
<button class="block"
onclick=${this.setFibonacci}>Fibonacci sample</button>
<button class="block"
onclick=${this.setFactorial}>Factorial sample</button>
<button class="accent block"
onclick=${this.handleRun}>Run this!</button>
</div>
@ -207,12 +231,7 @@ class App extends Component {
target="_blank">Ink</a>.
</p>
<h2>How much is there?</h2>
<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>
<p>Here's the full list of non-standard keywords that Tabloid currently uses:</p>
<ul>
<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>
@ -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">TOTALLY RIGHT</code> true</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">BEATS / SMALLER THAN</code> greater than / less than</li>
<li><code class="inline fixed block">SHOCKING DEVELOPMENT</code> return from a function</li>