Get basic REPL loop working

This commit is contained in:
Linus Lee 2020-09-24 05:56:09 -04:00
parent 030094045a
commit aafa74e7bc
2 changed files with 55 additions and 23 deletions

View File

@ -47,7 +47,7 @@ class Reader {
*/
class Wordifier {
constructor(str) {
this.reader = new Reader(prog);
this.reader = new Reader(str);
this.tokens = [];
}
wordify() {
@ -623,7 +623,7 @@ class Environment {
}
case N.PrintExpr: {
const val = this.eval(node.val);
Runtime.print(val);
this.runtime.print(val);
return val;
}
default:

View File

@ -1,4 +1,4 @@
const prog = `
const PROG_DEFAULT = `
DISCOVER HOW TO factorial WITH n
WE SAID
WHAT IF n IS ACTUALLY 0
@ -14,24 +14,7 @@ EXPERTS CLAIM result TO BE factorial OF 10
YOU WON'T WANT TO MISS 'RESULT IS'
YOU WON'T WANT TO MISS result
PLEASE LIKE AND SUBSCRIBE
`;
const Runtime = {
print(s) {
console.log(s.toString());
}
}
// main
try {
const tokens = tokenize(prog);
const nodes = new Parser(tokens).parse();
const env = new Environment(Runtime);
env.run(nodes);
} catch (e) {
console.error(e);
}
PLEASE LIKE AND SUBSCRIBE`;
const {
Component,
@ -39,18 +22,67 @@ const {
class Editor extends Component {
init() {
this.val = '';
this.prog = PROG_DEFAULT;
// script appends to it
this.output = '';
this.errors = '';
this.handleRun = () => this.eval();
this.handleInput = evt => {
this.prog = evt.target.value;
this.render();
}
}
eval() {
this.output = '';
try {
const tokens = tokenize(this.prog);
const nodes = new Parser(tokens).parse();
const env = new Environment({
print: s => {
this.output += s.toString();
this.render();
},
});
env.run(nodes);
} catch (e) {
this.errors = e.toString();
}
this.render();
}
compose() {
return jdom`<div class="editor fixed block">
<div class="controls">
<button class="accent block"
onclick=${this.handleRun}>Run!</button>
</div>
<div class="code">
<textarea cols="30" rows="10"
value=${this.prog}
oninput=${this.handleInput}>
</textarea>
</div>
<div class="result">
<div class="output">
${this.output.split('\n').map(line => jdom`<code>${line}</code>`)}
</div>
<div class="errors">
${this.errors.split('\n').map(line => jdom`<code>${line}</code>`)}
</div>
</div>
</div>`;
}
}
class App extends Component {
init() {
this.editor = new Editor();
}
compose() {
return jdom`<main>
<h1>Tabloid</h1>
<p class="subtitle">The Clickbait Headline Programming Language</p>
${this.editor.node}
</main>`;
}
}