forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.js
More file actions
executable file
·128 lines (116 loc) · 4.96 KB
/
repl.js
File metadata and controls
executable file
·128 lines (116 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$(function () {
var repl = new CodeMirrorREPL('interactive', {
mode: "python",
theme: "solarized dark"
}),
compilableLines = [],
//finds lines starting with "print"
re = new RegExp("\\s*print"),
//finds import statements
importre = new RegExp("\\s*import"),
//finds multuline string constants
mls = new RegExp("'''"),
//finds defining statements
defre = new RegExp("def.*|class.*"),
//test for empty line.
emptyline = new RegExp("^\\s*$");
repl.print("Python 2.6(ish) (skulpt, " + new Date() + ")");
repl.print("[" + navigator.userAgent + "] on " + navigator.platform);
repl.print('Don\'t type "help", "copyright", "credits" or "license" unless you\'ve assigned something to them');
repl.isBalanced = function (code) {
var lines = code.split('\n'),
depth = 0,
mlsopened = false;
for (var l in lines){
if (lines[l].match(/'''/) !== null && lines[l].match(/'''/).length == 1) {
mlsopened = !mlsopened;
}
if (!mlsopened && lines[l].substr(lines[l].length -1) == ":") {
depth++;
}
if (!mlsopened && lines[l] == "" && depth > 0){
depth--;
}
}
return depth == 0 && !mlsopened;
}
//Loop
repl.eval = function (code) {
Sk.configure({
output: function(str) {
//strip out line-feeds
if (str.replace(/\n/g, "") != ""){
repl.print(str);
}
},
read: function (x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
});
//split lines on linefeed
var lines = code.split('\n'),
lines = lines.filter(function(str) { return !emptyline.test(str); }),
//concatenate them to the lines collected up till now
linesToCompile = compilableLines.concat(lines);
//it's a onliner
if (lines.length == 1) {
//if it's a statement that should be printed (not containing an = or def or class or an empty line)
if (lines[0].indexOf('=') == -1 && !defre.test(lines[0]) && !importre.test(lines[0]) && lines[0].length > 0) {
//if it doesn't contain print make sure it doesn't print None
if (!re.test(lines[0])) {
//remove the statement
linesToCompile.pop();
//evaluate it if nessecary
linesToCompile.push("evaluationresult = " + lines[0]);
//print the result if not None
linesToCompile.push("if not evaluationresult == None: print evaluationresult");
}
//make sure it doesnt' end up in the list with lines to compile the next run
lines.pop();
}
}
//filter out empty lines
lines = lines.filter(function(str){ return !emptyline.test(str); });
//don't compile if there isn't anything to compile.
if (linesToCompile.length === 0) { return; }
try {
//Evaluate
Sk.importMainWithBody("repl", false, linesToCompile.join('\n'));
//remove print statements when a block is created that doesn't define anything
var removePrints = false;
compilableLines = compilableLines.concat(lines.map(function (str) {
//non defining block statement
if (str.substr(str.length -1) == ":" && !defre.test(str)) {
removePrints = true;
return str;
}
//end of non defining block statement
if(str == "" && removePrints){
removePrints = false;
return str;
}
if (re.test(str) && removePrints) {
//strip prints from non defining block statements.
return str.replace(/print.*/g, "pass");
} else {
return str;
}
}));
} catch (err) {
repl.print(err);
var index = -1;
//find the line number
if ((index = err.toString().indexOf("on line")) != -1) {
index = parseInt(err.toString().substr(index + 8), 10);
}
var line = 0;
//print the accumulated code with a ">" before the broken line.
//Don't add the last statement to the accumulated code
repl.print(linesToCompile.map(function (str) {
return ++line + (index == line ? ">" : " ") + ": " + str;
}).join('\n'));
}
}
});