REPL.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. smalltalk.addPackage('REPL', {});
  2. smalltalk.addClass('Repl', smalltalk.Object, ['readline', 'interface', 'util'], 'REPL');
  3. smalltalk.addMethod(
  4. "_close",
  5. smalltalk.method({
  6. selector: "close",
  7. category: 'actions',
  8. fn: function (){
  9. var self=this;
  10. return smalltalk.withContext(function($ctx1) { _st(_st(process)._stdin())._destroy();
  11. return self}, function($ctx1) {$ctx1.fill(self,"close",{}, smalltalk.Repl)})},
  12. args: [],
  13. source: "close\x0a\x09process stdin destroy",
  14. messageSends: ["destroy", "stdin"],
  15. referencedClasses: []
  16. }),
  17. smalltalk.Repl);
  18. smalltalk.addMethod(
  19. "_createInterface",
  20. smalltalk.method({
  21. selector: "createInterface",
  22. category: 'actions',
  23. fn: function (){
  24. var self=this;
  25. return smalltalk.withContext(function($ctx1) { self["@interface"]=_st(self["@readline"])._createInterface_stdout_(_st(process)._stdin(),_st(process)._stdout());
  26. _st(self["@interface"])._on_do_("line",(function(buffer){
  27. return smalltalk.withContext(function($ctx2) { return _st(self)._eval_(buffer);
  28. }, function($ctx2) {$ctx2.fillBlock({buffer:buffer},$ctx1)})}));
  29. _st(self["@interface"])._on_do_("close",(function(){
  30. return smalltalk.withContext(function($ctx2) { return _st(self)._close();
  31. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  32. _st(self)._setPrompt();
  33. _st(self["@interface"])._prompt();
  34. return self}, function($ctx1) {$ctx1.fill(self,"createInterface",{}, smalltalk.Repl)})},
  35. args: [],
  36. source: "createInterface\x0a\x09\x22No completion for now\x22\x0a\x09interface := readline createInterface: process stdin stdout: process stdout.\x0a\x09interface on: 'line' do: [:buffer | self eval: buffer].\x0a\x09interface on: 'close' do: [self close].\x0a\x09self setPrompt.\x0a\x09interface prompt",
  37. messageSends: ["createInterface:stdout:", "stdin", "stdout", "on:do:", "eval:", "close", "setPrompt", "prompt"],
  38. referencedClasses: []
  39. }),
  40. smalltalk.Repl);
  41. smalltalk.addMethod(
  42. "_eval_",
  43. smalltalk.method({
  44. selector: "eval:",
  45. category: 'actions',
  46. fn: function (buffer){
  47. var self=this;
  48. var result;
  49. return smalltalk.withContext(function($ctx1) { var $1,$2;
  50. $1=_st(buffer)._isEmpty();
  51. if(! smalltalk.assert($1)){
  52. _st(self)._try_catch_((function(){
  53. return smalltalk.withContext(function($ctx2) { result=_st(_st((smalltalk.Compiler || Compiler))._new())._evaluateExpression_(buffer);
  54. result;
  55. return _st((smalltalk.Transcript || Transcript))._show_(result);
  56. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),(function(e){
  57. return smalltalk.withContext(function($ctx2) { $2=_st(e)._isSmalltalkError();
  58. if(smalltalk.assert($2)){
  59. return _st(_st((smalltalk.ErrorHandler || ErrorHandler))._new())._handleError_(e);
  60. } else {
  61. return _st(_st(process)._stdout())._write_(_st(e)._jsStack());
  62. };
  63. }, function($ctx2) {$ctx2.fillBlock({e:e},$ctx1)})}));
  64. };
  65. _st(self["@interface"])._prompt();
  66. return self}, function($ctx1) {$ctx1.fill(self,"eval:",{buffer:buffer,result:result}, smalltalk.Repl)})},
  67. args: ["buffer"],
  68. source: "eval: buffer\x0a\x09| result |\x0a\x09buffer isEmpty ifFalse: [\x0a\x09\x09self try: [\x0a\x09\x09\x09result := Compiler new evaluateExpression: buffer.\x0a\x09\x09\x09Transcript show: result]\x0a\x09\x09catch: [:e |\x0a\x09\x09\x09e isSmalltalkError\x0a\x09\x09\x09 ifTrue: [ErrorHandler new handleError: e]\x0a\x09\x09\x09 ifFalse: [process stdout write: e jsStack]]].\x0a\x09interface prompt",
  69. messageSends: ["ifFalse:", "try:catch:", "evaluateExpression:", "new", "show:", "ifTrue:ifFalse:", "handleError:", "write:", "jsStack", "stdout", "isSmalltalkError", "isEmpty", "prompt"],
  70. referencedClasses: ["Compiler", "Transcript", "ErrorHandler"]
  71. }),
  72. smalltalk.Repl);
  73. smalltalk.addMethod(
  74. "_initialize",
  75. smalltalk.method({
  76. selector: "initialize",
  77. category: 'initialization',
  78. fn: function (){
  79. var self=this;
  80. return smalltalk.withContext(function($ctx1) { smalltalk.Object.fn.prototype._initialize.apply(_st(self), []);
  81. self["@readline"]=_st(require)._value_("readline");
  82. self["@util"]=_st(require)._value_("util");
  83. return self}, function($ctx1) {$ctx1.fill(self,"initialize",{}, smalltalk.Repl)})},
  84. args: [],
  85. source: "initialize\x0a\x09super initialize.\x0a\x09readline := require value: 'readline'.\x0a\x09util := require value: 'util'",
  86. messageSends: ["initialize", "value:"],
  87. referencedClasses: []
  88. }),
  89. smalltalk.Repl);
  90. smalltalk.addMethod(
  91. "_prompt",
  92. smalltalk.method({
  93. selector: "prompt",
  94. category: 'accessing',
  95. fn: function (){
  96. var self=this;
  97. return smalltalk.withContext(function($ctx1) { return "amber >> ";
  98. }, function($ctx1) {$ctx1.fill(self,"prompt",{}, smalltalk.Repl)})},
  99. args: [],
  100. source: "prompt\x0a\x09^'amber >> '",
  101. messageSends: [],
  102. referencedClasses: []
  103. }),
  104. smalltalk.Repl);
  105. smalltalk.addMethod(
  106. "_setPrompt",
  107. smalltalk.method({
  108. selector: "setPrompt",
  109. category: 'actions',
  110. fn: function (){
  111. var self=this;
  112. return smalltalk.withContext(function($ctx1) { _st(self["@interface"])._setPrompt_(_st(self)._prompt());
  113. return self}, function($ctx1) {$ctx1.fill(self,"setPrompt",{}, smalltalk.Repl)})},
  114. args: [],
  115. source: "setPrompt\x0a\x09interface setPrompt: self prompt",
  116. messageSends: ["setPrompt:", "prompt"],
  117. referencedClasses: []
  118. }),
  119. smalltalk.Repl);
  120. smalltalk.addMethod(
  121. "_main",
  122. smalltalk.method({
  123. selector: "main",
  124. category: 'not yet classified',
  125. fn: function (){
  126. var self=this;
  127. return smalltalk.withContext(function($ctx1) { _st(_st(self)._new())._createInterface();
  128. return self}, function($ctx1) {$ctx1.fill(self,"main",{}, smalltalk.Repl.klass)})},
  129. args: [],
  130. source: "main\x0a\x09self new createInterface",
  131. messageSends: ["createInterface", "new"],
  132. referencedClasses: []
  133. }),
  134. smalltalk.Repl.klass);