1
0

Compiler-Interpreter.deploy.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. smalltalk.addPackage('Compiler-Interpreter', {});
  2. smalltalk.addClass('ASTInterpreter', smalltalk.NodeVisitor, ['currentNode', 'context', 'shouldReturn'], 'Compiler-Interpreter');
  3. smalltalk.addMethod(
  4. "_blockValue_",
  5. smalltalk.method({
  6. selector: "blockValue:",
  7. fn: function (anASTBlockClosure){
  8. var self=this;
  9. var $1;
  10. $1=smalltalk.send(self,"_interpret_",[smalltalk.send(smalltalk.send(smalltalk.send(anASTBlockClosure,"_astNode",[]),"_nodes",[]),"_first",[])]);
  11. return $1;
  12. }
  13. }),
  14. smalltalk.ASTInterpreter);
  15. smalltalk.addMethod(
  16. "_context",
  17. smalltalk.method({
  18. selector: "context",
  19. fn: function (){
  20. var self=this;
  21. return self["@context"];
  22. }
  23. }),
  24. smalltalk.ASTInterpreter);
  25. smalltalk.addMethod(
  26. "_context_",
  27. smalltalk.method({
  28. selector: "context:",
  29. fn: function (aMethodContext){
  30. var self=this;
  31. self["@context"]=aMethodContext;
  32. return self}
  33. }),
  34. smalltalk.ASTInterpreter);
  35. smalltalk.addMethod(
  36. "_initialize",
  37. smalltalk.method({
  38. selector: "initialize",
  39. fn: function (){
  40. var self=this;
  41. smalltalk.send(self,"_initialize",[],smalltalk.NodeVisitor);
  42. self["@shouldReturn"]=false;
  43. return self}
  44. }),
  45. smalltalk.ASTInterpreter);
  46. smalltalk.addMethod(
  47. "_interpret_",
  48. smalltalk.method({
  49. selector: "interpret:",
  50. fn: function (aNode){
  51. var self=this;
  52. var $1;
  53. self["@shouldReturn"]=false;
  54. $1=smalltalk.send(self,"_interpretNode_",[aNode]);
  55. return $1;
  56. }
  57. }),
  58. smalltalk.ASTInterpreter);
  59. smalltalk.addMethod(
  60. "_interpretNode_",
  61. smalltalk.method({
  62. selector: "interpretNode:",
  63. fn: function (aNode){
  64. var self=this;
  65. var $1;
  66. self["@currentNode"]=aNode;
  67. $1=smalltalk.send(self,"_visit_",[aNode]);
  68. return $1;
  69. }
  70. }),
  71. smalltalk.ASTInterpreter);
  72. smalltalk.addMethod(
  73. "_send_to_arguments_",
  74. smalltalk.method({
  75. selector: "send:to:arguments:",
  76. fn: function (aSelector,anObject,aCollection){
  77. var self=this;
  78. var $1;
  79. $1=smalltalk.send(anObject,"_perform_withArguments_",[aSelector,aCollection]);
  80. return $1;
  81. }
  82. }),
  83. smalltalk.ASTInterpreter);
  84. smalltalk.addMethod(
  85. "_visitBlockNode_",
  86. smalltalk.method({
  87. selector: "visitBlockNode:",
  88. fn: function (aNode){
  89. var self=this;
  90. var $1;
  91. $1=(function(){
  92. return _st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  93. });
  94. return $1;
  95. }
  96. }),
  97. smalltalk.ASTInterpreter);
  98. smalltalk.addMethod(
  99. "_visitReturnNode_",
  100. smalltalk.method({
  101. selector: "visitReturnNode:",
  102. fn: function (aNode){
  103. var self=this;
  104. var $1;
  105. self["@shouldReturn"]=true;
  106. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  107. return $1;
  108. }
  109. }),
  110. smalltalk.ASTInterpreter);
  111. smalltalk.addMethod(
  112. "_visitSendNode_",
  113. smalltalk.method({
  114. selector: "visitSendNode:",
  115. fn: function (aNode){
  116. var self=this;
  117. var $1;
  118. var receiver;
  119. var arguments;
  120. receiver=_st(self)._interpretNode_(_st(aNode)._receiver());
  121. arguments=_st(_st(aNode)._arguments())._collect_((function(each){
  122. return _st(self)._interpretNode_(each);
  123. }));
  124. $1=_st(self)._send_to_arguments_(_st(aNode)._selector(),receiver,arguments);
  125. return $1;
  126. }
  127. }),
  128. smalltalk.ASTInterpreter);
  129. smalltalk.addMethod(
  130. "_visitSequenceNode_",
  131. smalltalk.method({
  132. selector: "visitSequenceNode:",
  133. fn: function (aNode){
  134. var self=this;
  135. var $1;
  136. var $early={};
  137. try {
  138. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  139. var value;
  140. value=_st(self)._interpretNode_(each);
  141. value;
  142. if(smalltalk.assert(self["@shouldReturn"])){
  143. throw $early=[value];
  144. };
  145. }));
  146. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._last());
  147. return $1;
  148. }
  149. catch(e) {if(e===$early)return e[0]; throw e}
  150. }
  151. }),
  152. smalltalk.ASTInterpreter);
  153. smalltalk.addMethod(
  154. "_visitValueNode_",
  155. smalltalk.method({
  156. selector: "visitValueNode:",
  157. fn: function (aNode){
  158. var self=this;
  159. var $1;
  160. $1=smalltalk.send(aNode,"_value",[]);
  161. return $1;
  162. }
  163. }),
  164. smalltalk.ASTInterpreter);
  165. smalltalk.addClass('ASTInterpretorTest', smalltalk.TestCase, [], 'Compiler-Interpreter');
  166. smalltalk.addMethod(
  167. "_analyze_forClass_",
  168. smalltalk.method({
  169. selector: "analyze:forClass:",
  170. fn: function (aNode,aClass){
  171. var self=this;
  172. _st(_st((smalltalk.SemanticAnalyzer || SemanticAnalyzer))._on_(aClass))._visit_(aNode);
  173. return aNode;
  174. }
  175. }),
  176. smalltalk.ASTInterpretorTest);
  177. smalltalk.addMethod(
  178. "_interpret_",
  179. smalltalk.method({
  180. selector: "interpret:",
  181. fn: function (aString){
  182. var self=this;
  183. var $1;
  184. $1=_st(_st((smalltalk.ASTInterpreter || ASTInterpreter))._new())._interpret_(_st(_st(_st(self)._parse_forClass_(aString,(smalltalk.Object || Object)))._nodes())._first());
  185. return $1;
  186. }
  187. }),
  188. smalltalk.ASTInterpretorTest);
  189. smalltalk.addMethod(
  190. "_parse_",
  191. smalltalk.method({
  192. selector: "parse:",
  193. fn: function (aString){
  194. var self=this;
  195. var $1;
  196. $1=_st(_st((smalltalk.Smalltalk || Smalltalk))._current())._parse_(aString);
  197. return $1;
  198. }
  199. }),
  200. smalltalk.ASTInterpretorTest);
  201. smalltalk.addMethod(
  202. "_parse_forClass_",
  203. smalltalk.method({
  204. selector: "parse:forClass:",
  205. fn: function (aString,aClass){
  206. var self=this;
  207. var $1;
  208. $1=_st(self)._analyze_forClass_(_st(self)._parse_(aString),aClass);
  209. return $1;
  210. }
  211. }),
  212. smalltalk.ASTInterpretorTest);
  213. smalltalk.addMethod(
  214. "_testBinarySend",
  215. smalltalk.method({
  216. selector: "testBinarySend",
  217. fn: function (){
  218. var self=this;
  219. _st(self)._assert_equals_(_st(self)._interpret_("foo 2+3+4"),(9));
  220. return self}
  221. }),
  222. smalltalk.ASTInterpretorTest);
  223. smalltalk.addMethod(
  224. "_testBlockLiteral",
  225. smalltalk.method({
  226. selector: "testBlockLiteral",
  227. fn: function (){
  228. var self=this;
  229. _st(self)._assert_(false);
  230. return self}
  231. }),
  232. smalltalk.ASTInterpretorTest);