Skip to content

Commit

Permalink
added interpreter, debug mode, interactive mode
Browse files Browse the repository at this point in the history
currently debugging features and debug non-interactive mode do not work
yet
  • Loading branch information
btzy committed Apr 18, 2017
1 parent bd92203 commit d6f86c3
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 60 deletions.
4 changes: 2 additions & 2 deletions bf.css
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ body>.wrapper>*{

#ioblock .combined>.iocontent{
flex:1 1 0px;
height:100%;
width:100%;
overflow:hidden;
}

#ioblock .combined>.iocontent .terminal{
Expand All @@ -450,6 +449,7 @@ body>.wrapper>*{
user-select: initial;
font:16px/normal 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
padding:0 4px;
box-sizing:border-box;
}

body>.wrapper>.horizontal-spacer{
Expand Down
161 changes: 132 additions & 29 deletions bf.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ window.addEventListener("load",function(){
codeCompiled=false;
});

var compilemodes_changed=function(){
executemodes_changed();
if(processHandlerTerminator){
processHandlerTerminator();
processHandlerTerminator=undefined;
}
compilationSpan.firstChild.nodeValue="";
codeCompiled=false;
isCompiling=false;
};

var executemodes_changed=function(){
if(runTerminator){
runTerminator();
runTerminator=undefined;
compilemodes_changed();
return;
}
executionSpan.firstChild.nodeValue="";
if(interactive){
interactiveConsole.clear();
}
else{
outputEditor.setValue("");
}
}

compilebutton.addEventListener("click",function(){
if(processHandlerTerminator){
processHandlerTerminator();
Expand All @@ -123,17 +150,18 @@ window.addEventListener("load",function(){
processHandler.initialize(function(){
if(!to_terminate){
executionSpan.firstChild.nodeValue="";
compilationSpan.firstChild.nodeValue="Compiling…";
compilationSpan.firstChild.nodeValue="";
if(compilemode!=="debug")compilationSpan.firstChild.nodeValue="Compiling…";
var start_time=Date.now();
processHandler.compile(codeEditor.getValue(),{},function(message){
processHandler.compile(codeEditor.getValue(),{debug:(compilemode==="debug")},function(message){
if(!to_terminate){
isCompiling=false;
processHandlerTerminator=undefined;
if(message.success){
codeCompiled=true;
var end_time=Date.now();
console.log("Compiled in "+Math.round(end_time-start_time)+" ms.");
compilationSpan.firstChild.nodeValue="Compiled in "+Math.round(end_time-start_time)+" ms.";
if(compilemode!=="debug")compilationSpan.firstChild.nodeValue="Compiled in "+Math.round(end_time-start_time)+" ms.";
if(toRunAfterCompiling){
toRunAfterCompiling=false;
runbutton.click();
Expand All @@ -148,6 +176,9 @@ window.addEventListener("load",function(){
});
});

var breakpointBuffer=undefined;
var globalPauseBuffer=undefined;

runbutton.addEventListener("click",function(){
if(!codeCompiled){
toRunAfterCompiling=true;
Expand All @@ -171,8 +202,8 @@ window.addEventListener("load",function(){
};
executionSpan.firstChild.nodeValue="Executing…";
var start_time=Date.now();
if(radio_interactive_no.checked){
processHandler.execute(inputEditor.getValue(),{},function(message){
if(!interactive){
processHandler.execute(inputEditor.getValue(),{debug:(compilemode==="debug")},function(message){
if(!to_terminate){
runTerminator=undefined;
if(message.success){
Expand All @@ -188,31 +219,71 @@ window.addEventListener("load",function(){
});
}
else{
interactiveConsole.clear();
var interactiveObj=processHandler.executeInteractive({},function(){
if(!to_terminate){
interactiveConsole.read(function(text){
interactiveObj.inputAddedCallback(text);
});
}
},function(outputText){
if(!to_terminate){
interactiveConsole.write(outputText);
}
},function(message){
if(!to_terminate){
runTerminator=undefined;
if(message.success){
var end_time=Date.now();
outputEditor.setValue(message.output,1);
console.log("Executed in "+Math.round(end_time-start_time)+" ms.");
executionSpan.firstChild.nodeValue="Executed in "+Math.round(end_time-start_time)+" ms.";
if(compilemode!=="debug"){
interactiveConsole.clear();
interactiveConsole.focus();
var interactiveObj=processHandler.executeInteractive({debug:(compilemode==="debug")},function(){
if(!to_terminate){
interactiveConsole.read(function(text){
interactiveObj.inputAddedCallback(text);
});
}
},function(outputText){
if(!to_terminate){
interactiveConsole.write(outputText);
}
},function(message){
if(!to_terminate){
runTerminator=undefined;
if(message.success){
var end_time=Date.now();
outputEditor.setValue(message.output,1);
console.log("Executed in "+Math.round(end_time-start_time)+" ms.");
executionSpan.firstChild.nodeValue="Executed in "+Math.round(end_time-start_time)+" ms.";
}
else{
executionSpan.firstChild.nodeValue="Execution failed.";
}
}
});
}
else{
interactiveConsole.clear();
interactiveConsole.focus();
breakpointBuffer=new SharedArrayBuffer(codeEditor.getValue().length);
globalPauseBuffer=new SharedArrayBuffer(1);
var interactiveObj=processHandler.executeInteractive({debug:(compilemode==="debug"),sourcecode:codeEditor.getValue(),breakpointBuffer:breakpointBuffer,globalPauseBuffer:globalPauseBuffer},function(){
if(!to_terminate){
interactiveConsole.read(function(text){
interactiveObj.inputAddedCallback(text);
});
}
},function(outputText){
if(!to_terminate){
interactiveConsole.write(outputText);
}
},function(message){
if(!to_terminate){
runTerminator=undefined;
if(message.success){
var end_time=Date.now();
outputEditor.setValue(message.output,1);
console.log("Executed in "+Math.round(end_time-start_time)+" ms.");
executionSpan.firstChild.nodeValue="Executed in "+Math.round(end_time-start_time)+" ms.";
}
else{
executionSpan.firstChild.nodeValue="Execution failed.";
}
}
},function(options){
if(options.breakpoint){
alert("breakpoint hit");
}
else{
executionSpan.firstChild.nodeValue="Execution failed.";
alert("paused");
}
}
});
});
}
}
}
});
Expand All @@ -228,12 +299,16 @@ window.addEventListener("load",function(){
radio_interactive_yes.addEventListener("change",function(){
separate_ioblock.classList.remove("selected");
combined_ioblock.classList.add("selected");
localStorage.setItem("option-interactive","yes");
interactive="yes";
localStorage.setItem("option-interactive",interactive);
executemodes_changed();
});
radio_interactive_no.addEventListener("change",function(){
combined_ioblock.classList.remove("selected");
separate_ioblock.classList.add("selected");
localStorage.setItem("option-interactive","no");
interactive="no";
localStorage.setItem("option-interactive",interactive);
executemodes_changed();
});

var interactive=localStorage.getItem("option-interactive");
Expand All @@ -246,6 +321,34 @@ window.addEventListener("load",function(){
radio_interactive_yes.dispatchEvent(new Event("change"));
}

if(!window.SharedArrayBuffer){
radio_interactive_yes.disabled=true;
}

var radio_compilemode_debug=document.getElementById("radio-compilemode-debug");
var radio_compilemode_release=document.getElementById("radio-compilemode-release");

radio_compilemode_debug.addEventListener("change",function(){
compilemode="debug";
localStorage.setItem("option-compilemode",compilemode);
compilemodes_changed();
});
radio_compilemode_release.addEventListener("change",function(){
compilemode="release";
localStorage.setItem("option-compilemode",compilemode);
compilemodes_changed();
});

var compilemode=localStorage.getItem("option-compilemode");
if(compilemode==="debug"){
radio_compilemode_debug.checked=true;
radio_compilemode_debug.dispatchEvent(new Event("change"));
}
else{
radio_compilemode_release.checked=true;
radio_compilemode_release.dispatchEvent(new Event("change"));
}


// splitters
Array.prototype.forEach.call(document.getElementById("ioblock").getElementsByClassName("vertical-spacer"),function(el){
Expand Down
60 changes: 58 additions & 2 deletions interactive-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var InteractiveConsole=function(el){
this.flushEveryChar=false;
this.newLineChar='\n';
this.wrappingElement.setAttribute("tabindex","0");
this.wrappingElement.style.setProperty("overflow-y","auto");
this.lineDivs=[];
this.inputBuffer="";
var that=this;
Expand All @@ -27,6 +28,17 @@ var InteractiveConsole=function(el){
}
}
});
this.wrappingElement.addEventListener("focus",function(){
if(this.caret)this.showCaret();
});

this.wrappingElement.addEventListener("blur",function(){
if(this.caret){
if(this.caretTimeout!==undefined)window.clearTimeout(this.caretTimeout);
this.caretTimeout=undefined;
this.caret.firstChild.nodeValue=" ";
}
});

this.clear();

Expand All @@ -35,22 +47,53 @@ var InteractiveConsole=function(el){
InteractiveConsole.prototype.makeNewLine=function(){
var lineDiv=document.createElement("div");
lineDiv.classList.add("interactive-console-line");
lineDiv.style.setProperty("white-space","pre");
lineDiv.appendChild(document.createTextNode(""));
return lineDiv;
}

InteractiveConsole.prototype.makeInputSpan=function(){
var lineDiv=document.createElement("span");
lineDiv.classList.add("interactive-console-input");
lineDiv.style.setProperty("white-space","pre");
lineDiv.appendChild(document.createTextNode(""));
return lineDiv;
}

InteractiveConsole.prototype.showCaret=function(){
if(!this.caretTimeout){
this.caret.firstChild.nodeValue="█";
var blink=function(){
if(document.body.contains(this.caret)&&this.caretTimeout!==undefined){
if(this.caret.firstChild.nodeValue==="█"){
this.caret.firstChild.nodeValue=" ";
}
else{
this.caret.firstChild.nodeValue="█";
}
this.caretTimeout=window.setTimeout(blink,500);
}
};
this.caretTimeout=window.setTimeout(blink,500);
}
};

InteractiveConsole.prototype.makeCaret=function(){
var lineDiv=document.createElement("span");
lineDiv.classList.add("interactive-console-caret");
lineDiv.appendChild(document.createTextNode(" "));

return lineDiv;
}

InteractiveConsole.prototype.read=function(callback){
var lastLineDiv=this.lineDivs[this.lineDivs.length-1];
this.inputSpan=this.makeInputSpan();
this.caret=this.makeCaret();
this.inputCallback=callback;
lastLineDiv.appendChild(this.inputSpan);
lastLineDiv.appendChild(this.caret);
this.showCaret();
this.notifyReader();
};

Expand All @@ -60,20 +103,26 @@ InteractiveConsole.prototype.notifyReader=function(){
this.inputBuffer=this.inputBuffer.substr(1);
if(this.flushEveryChar||newChar===this.newLineChar){
var callbackText=this.inputSpan.firstChild.nodeValue+newChar;
if(this.caretTimeout!==undefined)window.clearTimeout(this.caretTimeout);
this.caretTimeout=undefined;
this.inputSpan.parentNode.removeChild(this.inputSpan);
this.inputSpan===undefined;
this.caret.parentNode.removeChild(this.caret);
this.inputSpan=undefined;
this.caret=undefined;
this.write(callbackText);
this.inputCallback(callbackText);
}
else{
this.inputSpan.firstChild.nodeValue+=newChar;
}
this.wrappingElement.scrollTop=this.wrappingElement.scrollHeight;
}
};

InteractiveConsole.prototype.attemptBackspace=function(){
if(this.inputSpan&&this.inputSpan.firstChild.nodeValue.length>0){
this.inputSpan.firstChild.nodeValue=this.inputSpan.firstChild.nodeValue.slice(0,-1);
this.wrappingElement.scrollTop=this.wrappingElement.scrollHeight;
}
}

Expand All @@ -88,15 +137,22 @@ InteractiveConsole.prototype.write=function(text){
var lastLineDiv=this.lineDivs[this.lineDivs.length-1];
lastLineDiv.firstChild.nodeValue+=lines[i];
}
this.wrappingElement.scrollTop=this.wrappingElement.scrollHeight;
};

InteractiveConsole.prototype.clear=function(){
this.lineDivs=[];
this.inputBuffer="";
this.inputSpan=undefined;
this.inputCallback=undefined;
this.caret=undefined;
this.caretTimeout=undefined;
while(this.wrappingElement.firstChild)this.wrappingElement.removeChild(this.wrappingElement.firstChild);
var lineDiv=this.makeNewLine();
this.lineDivs.push(lineDiv);
this.wrappingElement.appendChild(lineDiv);
}
}

InteractiveConsole.prototype.focus=function(){
this.wrappingElement.focus();
};
Loading

0 comments on commit d6f86c3

Please sign in to comment.