When hitting the tab key, actually indent in the editor. Also add boolean literal support

This commit is contained in:
Linus Lee 2020-09-24 07:53:59 -04:00
parent a0163315d3
commit 6c67ca1a04
2 changed files with 18 additions and 6 deletions

View File

@ -534,12 +534,10 @@ class Environment {
const scope = this.scopes[this.scopes.length - 1]; const scope = this.scopes[this.scopes.length - 1];
switch (node.type) { switch (node.type) {
case N.NumberLiteral: { case N.NumberLiteral:
case N.StringLiteral:
case N.BoolLiteral:
return node.val; return node.val;
}
case N.StringLiteral: {
return node.val;
}
case N.FnDecl: { case N.FnDecl: {
scope[node.name] = node; scope[node.name] = node;
return node; return node;

View File

@ -50,6 +50,19 @@ class Editor extends Component {
this.prog = evt.target.value; this.prog = evt.target.value;
this.render(); this.render();
} }
this.handleKeydown = evt => {
if (evt.key === 'Tab') {
evt.preventDefault();
const idx = evt.target.selectionStart;
if (idx !== null) {
const front = this.prog.substr(0, idx);
const back = this.prog.substr(idx);
this.prog = front + ' ' + back;
this.render();
evt.target.setSelectionRange(idx + 4, idx + 4);
}
}
}
} }
eval() { eval() {
this.output = ''; this.output = '';
@ -85,7 +98,8 @@ class Editor extends Component {
</div> </div>
<textarea class="editor-input" cols="30" rows="10" <textarea class="editor-input" cols="30" rows="10"
value=${this.prog} value=${this.prog}
oninput=${this.handleInput}> oninput=${this.handleInput}
onkeydown=${this.handleKeydown}>
</textarea> </textarea>
</div> </div>
<div class="output"> <div class="output">