2
0

REPL.js 4.6 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. smalltalk.send(smalltalk.send(process,"_stdin",[]),"_destroy",[]);
  11. return self},
  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. self["@interface"]=smalltalk.send(self["@readline"],"_createInterface_stdout_",[smalltalk.send(process,"_stdin",[]),smalltalk.send(process,"_stdout",[])]);
  26. smalltalk.send(self["@interface"],"_on_do_",["line",(function(buffer){
  27. return smalltalk.send(self,"_eval_",[buffer]);
  28. })]);
  29. smalltalk.send(self["@interface"],"_on_do_",["close",(function(){
  30. return smalltalk.send(self,"_close",[]);
  31. })]);
  32. smalltalk.send(self,"_setPrompt",[]);
  33. smalltalk.send(self["@interface"],"_prompt",[]);
  34. return self},
  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 $1,$2;
  49. var result;
  50. $1=smalltalk.send(buffer,"_isEmpty",[]);
  51. if(! smalltalk.assert($1)){
  52. smalltalk.send(self,"_try_catch_",[(function(){
  53. result=smalltalk.send(smalltalk.send((smalltalk.Compiler || Compiler),"_new",[]),"_evaluateExpression_",[buffer]);
  54. result;
  55. return smalltalk.send((smalltalk.Transcript || Transcript),"_show_",[result]);
  56. }),(function(e){
  57. $2=smalltalk.send(e,"_isSmalltalkError",[]);
  58. if(smalltalk.assert($2)){
  59. return smalltalk.send(smalltalk.send((smalltalk.ErrorHandler || ErrorHandler),"_new",[]),"_handleError_",[e]);
  60. } else {
  61. return smalltalk.send(smalltalk.send(process,"_stdout",[]),"_write_",[smalltalk.send(e,"_jsStack",[])]);
  62. };
  63. })]);
  64. };
  65. smalltalk.send(self["@interface"],"_prompt",[]);
  66. return self},
  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. smalltalk.send(self,"_initialize",[],smalltalk.Object);
  81. self["@readline"]=smalltalk.send(require,"_value_",["readline"]);
  82. self["@util"]=smalltalk.send(require,"_value_",["util"]);
  83. return self},
  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 "amber >> ";
  98. },
  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. smalltalk.send(self["@interface"],"_setPrompt_",[smalltalk.send(self,"_prompt",[])]);
  113. return self},
  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. smalltalk.send(smalltalk.send(self,"_new",[]),"_createInterface",[]);
  128. return self},
  129. args: [],
  130. source: "main\x0a\x09self new createInterface",
  131. messageSends: ["createInterface", "new"],
  132. referencedClasses: []
  133. }),
  134. smalltalk.Repl.klass);