Skip to content

Commit c701cc4

Browse files
committed
started adding support for raw_input in frontend
1 parent 1c94497 commit c701cc4

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer
9696
// whenever the HEIGHT of #dataViz changes
9797
// verticalStack - if true, then stack code display ON TOP of visualization
9898
// (else place side-by-side)
99+
// executeCodeWithRawInputFunc - function to call when you want to re-execute the given program
100+
// with some new user input (somewhat hacky!)
99101
function ExecutionVisualizer(domRootID, dat, params) {
100102
this.curInputCode = dat.code.rtrim(); // kill trailing spaces
101103
this.curTrace = dat.trace;
@@ -108,6 +110,19 @@ function ExecutionVisualizer(domRootID, dat, params) {
108110
// visualization to not show as much redundancy.
109111
this.curTrace = this.curTrace.filter(function(e) {return e.event != 'call';});
110112

113+
// if the final entry is raw_input, then trim it from the trace and
114+
// set a flag to prompt for user input when execution advances to the
115+
// end of the trace
116+
if (this.curTrace.length > 0) {
117+
var lastEntry = this.curTrace[this.curTrace.length - 1];
118+
if (lastEntry.event == 'raw_input') {
119+
this.promptForUserInput = true;
120+
this.userInputPromptStr = lastEntry.prompt;
121+
122+
this.curTrace.pop() // kill last entry so that it doesn't get displayed
123+
}
124+
}
125+
111126
this.curInstr = 0;
112127

113128
this.params = params;
@@ -129,6 +144,8 @@ function ExecutionVisualizer(domRootID, dat, params) {
129144
this.drawParentPointers = (this.params.drawParentPointers == true);
130145
this.textualMemoryLabels = (this.params.textualMemoryLabels == true);
131146

147+
this.executeCodeWithRawInputFunc = this.params.executeCodeWithRawInputFunc;
148+
132149
// cool, we can create a separate jsPlumb instance for each visualization:
133150
this.jsPlumbInstance = jsPlumb.getInstance({
134151
Endpoint: ["Dot", {radius:3}],
@@ -1145,9 +1162,15 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
11451162

11461163
var vcrControls = myViz.domRoot.find("#vcrControls");
11471164

1148-
// to be user-friendly, if we're on the LAST instruction, print "Program terminated"
11491165
if (isLastInstr) {
1150-
if (this.instrLimitReached) {
1166+
if (this.promptForUserInput) {
1167+
vcrControls.find("#curInstr").html('<b><font color="' + brightRed + '">Processing user input ...</font></b>');
1168+
1169+
// don't do smooth transition since prompt() is modal so it doesn't
1170+
// give the animation background thread time to run
1171+
smoothTransition = false;
1172+
}
1173+
else if (this.instrLimitReached) {
11511174
vcrControls.find("#curInstr").html("Instruction limit reached");
11521175
}
11531176
else {
@@ -1373,6 +1396,17 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
13731396
}
13741397
}
13751398

1399+
1400+
if (isLastInstr && this.promptForUserInput) {
1401+
if (this.executeCodeWithRawInputFunc) {
1402+
// blocking prompt dialog!
1403+
var userInput = prompt(this.userInputPromptStr);
1404+
1405+
// after executing, jump back to this.curInstr to give the
1406+
// illusion of continuity
1407+
this.executeCodeWithRawInputFunc(userInput, this.curInstr);
1408+
}
1409+
}
13761410
}
13771411

13781412

0 commit comments

Comments
 (0)