1
0

Compiler-Interpreter.deploy.js 8.1 KB

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