repl.js.bak 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Sk.configure({
  2. output: write,
  3. read: read,
  4. systemexit: true,
  5. retainglobals: true,
  6. inputfun: readline
  7. });
  8. var compilableLines = [],
  9. //finds lines starting with "print"
  10. re = new RegExp("\\s*print"),
  11. //finds import statements
  12. importre = new RegExp("\\s*import"),
  13. //finds multuline string constants
  14. mls = new RegExp("'''"),
  15. //finds defining statements
  16. defre = new RegExp("def.*|class.*"),
  17. //test for empty line.
  18. emptyline = new RegExp("^\\s*$"),
  19. //a regex to check if a line is an assignment
  20. //this regex checks whether or not a line starts with
  21. //an identifier followed with some whitspace and then an = and then some more white space.
  22. //it also checks if the identifier is a tuple.
  23. assignment= /^((\s*\(\s*(\s*((\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*)|(\s*\(\s*(\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*,)*\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*\)\s*))\s*,)*\s*((\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*)|(\s*\(\s*(\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*,)*\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*\)\s*))\s*\)\s*)|(\s*\s*(\s*((\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*)|(\s*\(\s*(\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*,)*\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*\)\s*))\s*,)*\s*((\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*)|(\s*\(\s*(\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*,)*\s*((\s*[_a-zA-Z]\w*\s*)|(\s*\(\s*(\s*[_a-zA-Z]\w*\s*,)*\s*[_a-zA-Z]\w*\s*\)\s*))\s*\)\s*))\s*\s*))=/,
  24. lines = [];
  25. print("Python 2.6(ish) (skulpt, " + new Date() + ")");
  26. print("[v8: " + version() + "] on a system");
  27. print('Don\'t type "help", "copyright", "credits" or "license" unless you\'ve assigned something to them');
  28. function isBalanced(lines) {
  29. 'use strict';
  30. var depth = 0,
  31. mlsopened = false,
  32. l;
  33. for (l = 0; l < lines.length; l = l + 1) {
  34. if (lines[l] !== undefined) {
  35. if (lines[l].match(/'''/) !== null && lines[l].match(/'''/).length === 1) {
  36. mlsopened = !mlsopened;
  37. }
  38. if (!mlsopened && lines[l].substr(lines[l].length - 1) === ":") {
  39. depth = depth + 1;
  40. }
  41. if (!mlsopened && lines[l] === "" && depth > 0) {
  42. depth = 0;
  43. }
  44. }
  45. }
  46. return depth === 0 && !mlsopened;
  47. }
  48. //Loop
  49. while (true) {
  50. write(isBalanced(lines) ? '>>> ' : '... ');
  51. //Read
  52. lines.push(readline());
  53. //See if it is ready to be evaluated;
  54. if (!isBalanced(lines)) { continue; }
  55. //it's a onliner
  56. if (lines.length === 1) {
  57. //if it's a statement that should be printed (not containing an = or def or class or an empty line)
  58. if (!assignment.test(lines[0]) && !defre.test(lines[0]) && !importre.test(lines[0]) && lines[0].length > 0) {
  59. //if it doesn't contain print make sure it doesn't print None
  60. if (!re.test(lines[0])) {
  61. //remove the statement
  62. //evaluate it if nessecary
  63. lines.push("evaluationresult = " + lines.pop());
  64. //print the result if not None
  65. lines.push("if not evaluationresult == None: print repr(evaluationresult)");
  66. }
  67. }
  68. }
  69. try {
  70. //Evaluate
  71. if (!lines || /^\s*$/.test(lines)) {
  72. continue
  73. }
  74. else {
  75. Sk.importMainWithBody("repl", false, lines.join('\n'));
  76. }
  77. } catch (err) {
  78. if (err instanceof Sk.builtin.SystemExit) {
  79. quit();
  80. }
  81. print(err);
  82. var index = -1;
  83. //find the line number
  84. if ((index = err.toString().indexOf("on line")) !== -1) {
  85. index = parseInt(err.toString().substr(index + 8), 10);
  86. }
  87. var line = 0;
  88. //print the accumulated code with a ">" before the broken line.
  89. //Don't add the last statement to the accumulated code
  90. print(lines.map(function (str) {
  91. return ++line + (index === line ? ">" : " ") + ": " + str;
  92. }).join('\n'));
  93. } finally {
  94. lines = [];
  95. }
  96. }