1
0

Compiler-Interpreter.deploy.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. smalltalk.addPackage('Compiler-Interpreter', {});
  2. smalltalk.addClass('ASTInterpreter', smalltalk.NodeVisitor, ['currentNode', 'context', 'shouldReturn'], 'Compiler-Interpreter');
  3. smalltalk.addMethod(
  4. "_context",
  5. smalltalk.method({
  6. selector: "context",
  7. fn: function (){
  8. var self=this;
  9. return smalltalk.withContext(function($ctx1) { return self["@context"];
  10. }, self, "context", [], smalltalk.ASTInterpreter)}
  11. }),
  12. smalltalk.ASTInterpreter);
  13. smalltalk.addMethod(
  14. "_context_",
  15. smalltalk.method({
  16. selector: "context:",
  17. fn: function (aMethodContext){
  18. var self=this;
  19. return smalltalk.withContext(function($ctx1) { self["@context"]=aMethodContext;
  20. return self}, self, "context:", [aMethodContext], smalltalk.ASTInterpreter)}
  21. }),
  22. smalltalk.ASTInterpreter);
  23. smalltalk.addMethod(
  24. "_initialize",
  25. smalltalk.method({
  26. selector: "initialize",
  27. fn: function (){
  28. var self=this;
  29. return smalltalk.withContext(function($ctx1) { smalltalk.NodeVisitor.fn.prototype._initialize.apply(_st(self), []);
  30. self["@shouldReturn"]=false;
  31. return self}, self, "initialize", [], smalltalk.ASTInterpreter)}
  32. }),
  33. smalltalk.ASTInterpreter);
  34. smalltalk.addMethod(
  35. "_interpret_",
  36. smalltalk.method({
  37. selector: "interpret:",
  38. fn: function (aNode){
  39. var self=this;
  40. return smalltalk.withContext(function($ctx1) { var $1;
  41. self["@shouldReturn"]=false;
  42. $1=_st(self)._interpretNode_(aNode);
  43. return $1;
  44. }, self, "interpret:", [aNode], smalltalk.ASTInterpreter)}
  45. }),
  46. smalltalk.ASTInterpreter);
  47. smalltalk.addMethod(
  48. "_interpretNode_",
  49. smalltalk.method({
  50. selector: "interpretNode:",
  51. fn: function (aNode){
  52. var self=this;
  53. return smalltalk.withContext(function($ctx1) { var $1;
  54. self["@currentNode"]=aNode;
  55. $1=_st(self)._visit_(aNode);
  56. return $1;
  57. }, self, "interpretNode:", [aNode], smalltalk.ASTInterpreter)}
  58. }),
  59. smalltalk.ASTInterpreter);
  60. smalltalk.addMethod(
  61. "_messageFromSendNode_",
  62. smalltalk.method({
  63. selector: "messageFromSendNode:",
  64. fn: function (aSendNode){
  65. var self=this;
  66. return smalltalk.withContext(function($ctx1) { var $2,$3,$1;
  67. $2=_st((smalltalk.Message || Message))._new();
  68. _st($2)._selector_(_st(aSendNode)._selector());
  69. _st($2)._arguments_(_st(_st(aSendNode)._arguments())._collect_((function(each){
  70. return smalltalk.withContext(function($ctx2) { return _st(self)._interpretNode_(each);
  71. })})));
  72. $3=_st($2)._yourself();
  73. $1=$3;
  74. return $1;
  75. }, self, "messageFromSendNode:", [aSendNode], smalltalk.ASTInterpreter)}
  76. }),
  77. smalltalk.ASTInterpreter);
  78. smalltalk.addMethod(
  79. "_visitBlockNode_",
  80. smalltalk.method({
  81. selector: "visitBlockNode:",
  82. fn: function (aNode){
  83. var self=this;
  84. return smalltalk.withContext(function($ctx1) { var $1;
  85. $1=(function(){
  86. return smalltalk.withContext(function($ctx2) { return _st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  87. })});
  88. return $1;
  89. }, self, "visitBlockNode:", [aNode], smalltalk.ASTInterpreter)}
  90. }),
  91. smalltalk.ASTInterpreter);
  92. smalltalk.addMethod(
  93. "_visitCascadeNode_",
  94. smalltalk.method({
  95. selector: "visitCascadeNode:",
  96. fn: function (aNode){
  97. var self=this;
  98. return smalltalk.withContext(function($ctx1) { var $1;
  99. $ctx1.locals.receiver=nil;
  100. $ctx1.locals.receiver=_st(self)._interpretNode_(_st(aNode)._receiver());
  101. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  102. return smalltalk.withContext(function($ctx2) { return _st(_st(self)._messageFromSendNode_(each))._sendTo_($ctx1.locals.receiver);
  103. })}));
  104. $1=_st(_st(self)._messageFromSendNode_(_st(_st(aNode)._nodes())._last()))._sendTo_($ctx1.locals.receiver);
  105. return $1;
  106. }, self, "visitCascadeNode:", [aNode], smalltalk.ASTInterpreter)}
  107. }),
  108. smalltalk.ASTInterpreter);
  109. smalltalk.addMethod(
  110. "_visitClassReferenceNode_",
  111. smalltalk.method({
  112. selector: "visitClassReferenceNode:",
  113. fn: function (aNode){
  114. var self=this;
  115. return smalltalk.withContext(function($ctx1) { var $1;
  116. $1=_st(_st((smalltalk.Smalltalk || Smalltalk))._current())._at_(_st(aNode)._value());
  117. return $1;
  118. }, self, "visitClassReferenceNode:", [aNode], smalltalk.ASTInterpreter)}
  119. }),
  120. smalltalk.ASTInterpreter);
  121. smalltalk.addMethod(
  122. "_visitJSStatementNode_",
  123. smalltalk.method({
  124. selector: "visitJSStatementNode:",
  125. fn: function (aNode){
  126. var self=this;
  127. return smalltalk.withContext(function($ctx1) { _st(self)._halt();
  128. return self}, self, "visitJSStatementNode:", [aNode], smalltalk.ASTInterpreter)}
  129. }),
  130. smalltalk.ASTInterpreter);
  131. smalltalk.addMethod(
  132. "_visitReturnNode_",
  133. smalltalk.method({
  134. selector: "visitReturnNode:",
  135. fn: function (aNode){
  136. var self=this;
  137. return smalltalk.withContext(function($ctx1) { var $1;
  138. self["@shouldReturn"]=true;
  139. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  140. return $1;
  141. }, self, "visitReturnNode:", [aNode], smalltalk.ASTInterpreter)}
  142. }),
  143. smalltalk.ASTInterpreter);
  144. smalltalk.addMethod(
  145. "_visitSendNode_",
  146. smalltalk.method({
  147. selector: "visitSendNode:",
  148. fn: function (aNode){
  149. var self=this;
  150. return smalltalk.withContext(function($ctx1) { var $1;
  151. $1=_st(_st(self)._messageFromSendNode_(aNode))._sendTo_(_st(self)._interpretNode_(_st(aNode)._receiver()));
  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($ctx1) { var $1;
  163. var $early={};
  164. try {
  165. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  166. return smalltalk.withContext(function($ctx2) { $ctx2.value=nil;
  167. $ctx2.locals.value=_st(self)._interpretNode_(each);
  168. $ctx2.locals.value;
  169. if(smalltalk.assert(self["@shouldReturn"])){
  170. throw $early=[$ctx2.locals.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($ctx1) { 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($ctx1) { _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($ctx1) { 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($ctx1) { 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($ctx1) { 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($ctx1) { _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($ctx1) { _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);
  262. smalltalk.addMethod(
  263. "_testCascade",
  264. smalltalk.method({
  265. selector: "testCascade",
  266. fn: function (){
  267. var self=this;
  268. return smalltalk.withContext(function($ctx1) { _st(self)._assert_equals_(_st(self)._interpret_("foo ^ OrderedCollection new add: 2; add: 3; yourself"),_st((smalltalk.OrderedCollection || OrderedCollection))._with_with_((2),(3)));
  269. return self}, self, "testCascade", [], smalltalk.ASTInterpreterTest)}
  270. }),
  271. smalltalk.ASTInterpreterTest);