Compiler-Interpreter.deploy.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. smalltalk.addPackage('Compiler-Interpreter', {});
  2. smalltalk.addClass('AIContext', smalltalk.NodeVisitor, ['outerContext', 'pc', 'locals', 'receiver', 'selector'], 'Compiler-Interpreter');
  3. smalltalk.addMethod(
  4. "_initializeFromMethodContext_",
  5. smalltalk.method({
  6. selector: "initializeFromMethodContext:",
  7. fn: function (aMethodContext){
  8. var self=this;
  9. return smalltalk.withContext(function($ctx1) { var $1;
  10. _st(self)._pc_(_st(aMethodContext)._pc());
  11. _st(self)._receiver_(_st(aMethodContext)._receiver());
  12. _st(self)._selector_(_st(aMethodContext)._selector());
  13. $1=_st(aMethodContext)._outerContext();
  14. if(($receiver = $1) == nil || $receiver == undefined){
  15. $1;
  16. } else {
  17. _st(self)._outerContext_(_st(_st(self)._class())._fromMethodContext_(_st(aMethodContext)._outerContext()));
  18. };
  19. _st(_st(aMethodContext)._locals())._keysAndValuesDo_((function(key,value){
  20. return smalltalk.withContext(function($ctx2) { return _st(_st(self)._locals())._at_put_(key,value);
  21. })}));
  22. return self}, self, "initializeFromMethodContext:", [aMethodContext], smalltalk.AIContext)}
  23. }),
  24. smalltalk.AIContext);
  25. smalltalk.addMethod(
  26. "_locals",
  27. smalltalk.method({
  28. selector: "locals",
  29. fn: function (){
  30. var self=this;
  31. return smalltalk.withContext(function($ctx1) { var $2,$1;
  32. $2=self["@locals"];
  33. if(($receiver = $2) == nil || $receiver == undefined){
  34. self["@locals"]=_st((smalltalk.Dictionary || Dictionary))._new();
  35. $1=self["@locals"];
  36. } else {
  37. $1=$2;
  38. };
  39. return $1;
  40. }, self, "locals", [], smalltalk.AIContext)}
  41. }),
  42. smalltalk.AIContext);
  43. smalltalk.addMethod(
  44. "_outerContext",
  45. smalltalk.method({
  46. selector: "outerContext",
  47. fn: function (){
  48. var self=this;
  49. return smalltalk.withContext(function($ctx1) { var $1;
  50. $1=self["@outerContext"];
  51. return $1;
  52. }, self, "outerContext", [], smalltalk.AIContext)}
  53. }),
  54. smalltalk.AIContext);
  55. smalltalk.addMethod(
  56. "_outerContext_",
  57. smalltalk.method({
  58. selector: "outerContext:",
  59. fn: function (anAIContext){
  60. var self=this;
  61. return smalltalk.withContext(function($ctx1) { self["@outerContext"]=anAIContext;
  62. return self}, self, "outerContext:", [anAIContext], smalltalk.AIContext)}
  63. }),
  64. smalltalk.AIContext);
  65. smalltalk.addMethod(
  66. "_pc",
  67. smalltalk.method({
  68. selector: "pc",
  69. fn: function (){
  70. var self=this;
  71. return smalltalk.withContext(function($ctx1) { var $2,$1;
  72. $2=self["@pc"];
  73. if(($receiver = $2) == nil || $receiver == undefined){
  74. self["@pc"]=(0);
  75. $1=self["@pc"];
  76. } else {
  77. $1=$2;
  78. };
  79. return $1;
  80. }, self, "pc", [], smalltalk.AIContext)}
  81. }),
  82. smalltalk.AIContext);
  83. smalltalk.addMethod(
  84. "_pc_",
  85. smalltalk.method({
  86. selector: "pc:",
  87. fn: function (anInteger){
  88. var self=this;
  89. return smalltalk.withContext(function($ctx1) { self["@pc"]=anInteger;
  90. return self}, self, "pc:", [anInteger], smalltalk.AIContext)}
  91. }),
  92. smalltalk.AIContext);
  93. smalltalk.addMethod(
  94. "_receiver",
  95. smalltalk.method({
  96. selector: "receiver",
  97. fn: function (){
  98. var self=this;
  99. return smalltalk.withContext(function($ctx1) { var $1;
  100. $1=self["@receiver"];
  101. return $1;
  102. }, self, "receiver", [], smalltalk.AIContext)}
  103. }),
  104. smalltalk.AIContext);
  105. smalltalk.addMethod(
  106. "_receiver_",
  107. smalltalk.method({
  108. selector: "receiver:",
  109. fn: function (anObject){
  110. var self=this;
  111. return smalltalk.withContext(function($ctx1) { self["@receiver"]=anObject;
  112. return self}, self, "receiver:", [anObject], smalltalk.AIContext)}
  113. }),
  114. smalltalk.AIContext);
  115. smalltalk.addMethod(
  116. "_selector",
  117. smalltalk.method({
  118. selector: "selector",
  119. fn: function (){
  120. var self=this;
  121. return smalltalk.withContext(function($ctx1) { var $1;
  122. $1=self["@selector"];
  123. return $1;
  124. }, self, "selector", [], smalltalk.AIContext)}
  125. }),
  126. smalltalk.AIContext);
  127. smalltalk.addMethod(
  128. "_selector_",
  129. smalltalk.method({
  130. selector: "selector:",
  131. fn: function (aString){
  132. var self=this;
  133. return smalltalk.withContext(function($ctx1) { self["@selector"]=aString;
  134. return self}, self, "selector:", [aString], smalltalk.AIContext)}
  135. }),
  136. smalltalk.AIContext);
  137. smalltalk.addMethod(
  138. "_fromMethodContext_",
  139. smalltalk.method({
  140. selector: "fromMethodContext:",
  141. fn: function (aMethodContext){
  142. var self=this;
  143. return smalltalk.withContext(function($ctx1) { var $2,$3,$1;
  144. $2=_st(self)._new();
  145. _st($2)._initializeFromMethodContext_(aMethodContext);
  146. $3=_st($2)._yourself();
  147. $1=$3;
  148. return $1;
  149. }, self, "fromMethodContext:", [aMethodContext], smalltalk.AIContext.klass)}
  150. }),
  151. smalltalk.AIContext.klass);
  152. smalltalk.addClass('ASTInterpreter', smalltalk.NodeVisitor, ['currentNode', 'context', 'shouldReturn'], 'Compiler-Interpreter');
  153. smalltalk.addMethod(
  154. "_context",
  155. smalltalk.method({
  156. selector: "context",
  157. fn: function (){
  158. var self=this;
  159. return smalltalk.withContext(function($ctx1) { return self["@context"];
  160. }, self, "context", [], smalltalk.ASTInterpreter)}
  161. }),
  162. smalltalk.ASTInterpreter);
  163. smalltalk.addMethod(
  164. "_context_",
  165. smalltalk.method({
  166. selector: "context:",
  167. fn: function (anAIContext){
  168. var self=this;
  169. return smalltalk.withContext(function($ctx1) { self["@context"]=anAIContext;
  170. return self}, self, "context:", [anAIContext], smalltalk.ASTInterpreter)}
  171. }),
  172. smalltalk.ASTInterpreter);
  173. smalltalk.addMethod(
  174. "_initialize",
  175. smalltalk.method({
  176. selector: "initialize",
  177. fn: function (){
  178. var self=this;
  179. return smalltalk.withContext(function($ctx1) { smalltalk.NodeVisitor.fn.prototype._initialize.apply(_st(self), []);
  180. self["@shouldReturn"]=false;
  181. return self}, self, "initialize", [], smalltalk.ASTInterpreter)}
  182. }),
  183. smalltalk.ASTInterpreter);
  184. smalltalk.addMethod(
  185. "_interpret_",
  186. smalltalk.method({
  187. selector: "interpret:",
  188. fn: function (aNode){
  189. var self=this;
  190. return smalltalk.withContext(function($ctx1) { var $1;
  191. self["@shouldReturn"]=false;
  192. $1=_st(self)._interpretNode_(aNode);
  193. return $1;
  194. }, self, "interpret:", [aNode], smalltalk.ASTInterpreter)}
  195. }),
  196. smalltalk.ASTInterpreter);
  197. smalltalk.addMethod(
  198. "_interpretNode_",
  199. smalltalk.method({
  200. selector: "interpretNode:",
  201. fn: function (aNode){
  202. var self=this;
  203. return smalltalk.withContext(function($ctx1) { var $1;
  204. self["@currentNode"]=aNode;
  205. $1=_st(self)._visit_(aNode);
  206. return $1;
  207. }, self, "interpretNode:", [aNode], smalltalk.ASTInterpreter)}
  208. }),
  209. smalltalk.ASTInterpreter);
  210. smalltalk.addMethod(
  211. "_messageFromSendNode_",
  212. smalltalk.method({
  213. selector: "messageFromSendNode:",
  214. fn: function (aSendNode){
  215. var self=this;
  216. return smalltalk.withContext(function($ctx1) { var $2,$3,$1;
  217. $2=_st((smalltalk.Message || Message))._new();
  218. _st($2)._selector_(_st(aSendNode)._selector());
  219. _st($2)._arguments_(_st(_st(aSendNode)._arguments())._collect_((function(each){
  220. return smalltalk.withContext(function($ctx2) { return _st(self)._interpretNode_(each);
  221. })})));
  222. $3=_st($2)._yourself();
  223. $1=$3;
  224. return $1;
  225. }, self, "messageFromSendNode:", [aSendNode], smalltalk.ASTInterpreter)}
  226. }),
  227. smalltalk.ASTInterpreter);
  228. smalltalk.addMethod(
  229. "_visitBlockNode_",
  230. smalltalk.method({
  231. selector: "visitBlockNode:",
  232. fn: function (aNode){
  233. var self=this;
  234. return smalltalk.withContext(function($ctx1) { var $1;
  235. $1=(function(){
  236. return smalltalk.withContext(function($ctx2) { return _st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  237. })});
  238. return $1;
  239. }, self, "visitBlockNode:", [aNode], smalltalk.ASTInterpreter)}
  240. }),
  241. smalltalk.ASTInterpreter);
  242. smalltalk.addMethod(
  243. "_visitCascadeNode_",
  244. smalltalk.method({
  245. selector: "visitCascadeNode:",
  246. fn: function (aNode){
  247. var self=this;
  248. return smalltalk.withContext(function($ctx1) { var $1;
  249. $ctx1.locals.receiver=nil;
  250. $ctx1.locals.receiver=_st(self)._interpretNode_(_st(aNode)._receiver());
  251. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  252. return smalltalk.withContext(function($ctx2) { return _st(_st(self)._messageFromSendNode_(each))._sendTo_($ctx1.locals.receiver);
  253. })}));
  254. $1=_st(_st(self)._messageFromSendNode_(_st(_st(aNode)._nodes())._last()))._sendTo_($ctx1.locals.receiver);
  255. return $1;
  256. }, self, "visitCascadeNode:", [aNode], smalltalk.ASTInterpreter)}
  257. }),
  258. smalltalk.ASTInterpreter);
  259. smalltalk.addMethod(
  260. "_visitClassReferenceNode_",
  261. smalltalk.method({
  262. selector: "visitClassReferenceNode:",
  263. fn: function (aNode){
  264. var self=this;
  265. return smalltalk.withContext(function($ctx1) { var $1;
  266. $1=_st(_st((smalltalk.Smalltalk || Smalltalk))._current())._at_(_st(aNode)._value());
  267. return $1;
  268. }, self, "visitClassReferenceNode:", [aNode], smalltalk.ASTInterpreter)}
  269. }),
  270. smalltalk.ASTInterpreter);
  271. smalltalk.addMethod(
  272. "_visitJSStatementNode_",
  273. smalltalk.method({
  274. selector: "visitJSStatementNode:",
  275. fn: function (aNode){
  276. var self=this;
  277. return smalltalk.withContext(function($ctx1) { _st(self)._halt();
  278. return self}, self, "visitJSStatementNode:", [aNode], smalltalk.ASTInterpreter)}
  279. }),
  280. smalltalk.ASTInterpreter);
  281. smalltalk.addMethod(
  282. "_visitReturnNode_",
  283. smalltalk.method({
  284. selector: "visitReturnNode:",
  285. fn: function (aNode){
  286. var self=this;
  287. return smalltalk.withContext(function($ctx1) { var $1;
  288. self["@shouldReturn"]=true;
  289. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  290. return $1;
  291. }, self, "visitReturnNode:", [aNode], smalltalk.ASTInterpreter)}
  292. }),
  293. smalltalk.ASTInterpreter);
  294. smalltalk.addMethod(
  295. "_visitSendNode_",
  296. smalltalk.method({
  297. selector: "visitSendNode:",
  298. fn: function (aNode){
  299. var self=this;
  300. return smalltalk.withContext(function($ctx1) { var $1;
  301. $1=_st(_st(self)._messageFromSendNode_(aNode))._sendTo_(_st(self)._interpretNode_(_st(aNode)._receiver()));
  302. return $1;
  303. }, self, "visitSendNode:", [aNode], smalltalk.ASTInterpreter)}
  304. }),
  305. smalltalk.ASTInterpreter);
  306. smalltalk.addMethod(
  307. "_visitSequenceNode_",
  308. smalltalk.method({
  309. selector: "visitSequenceNode:",
  310. fn: function (aNode){
  311. var self=this;
  312. return smalltalk.withContext(function($ctx1) { var $1;
  313. var $early={};
  314. try {
  315. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  316. return smalltalk.withContext(function($ctx2) { $ctx2.value=nil;
  317. $ctx2.locals.value=_st(self)._interpretNode_(each);
  318. $ctx2.locals.value;
  319. if(smalltalk.assert(self["@shouldReturn"])){
  320. throw $early=[$ctx2.locals.value];
  321. };
  322. })}));
  323. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._last());
  324. return $1;
  325. }
  326. catch(e) {if(e===$early)return e[0]; throw e}
  327. }, self, "visitSequenceNode:", [aNode], smalltalk.ASTInterpreter)}
  328. }),
  329. smalltalk.ASTInterpreter);
  330. smalltalk.addMethod(
  331. "_visitValueNode_",
  332. smalltalk.method({
  333. selector: "visitValueNode:",
  334. fn: function (aNode){
  335. var self=this;
  336. return smalltalk.withContext(function($ctx1) { var $1;
  337. $1=_st(aNode)._value();
  338. return $1;
  339. }, self, "visitValueNode:", [aNode], smalltalk.ASTInterpreter)}
  340. }),
  341. smalltalk.ASTInterpreter);
  342. smalltalk.addClass('ASTInterpreterTest', smalltalk.TestCase, [], 'Compiler-Interpreter');
  343. smalltalk.addMethod(
  344. "_analyze_forClass_",
  345. smalltalk.method({
  346. selector: "analyze:forClass:",
  347. fn: function (aNode,aClass){
  348. var self=this;
  349. return smalltalk.withContext(function($ctx1) { _st(_st((smalltalk.SemanticAnalyzer || SemanticAnalyzer))._on_(aClass))._visit_(aNode);
  350. return aNode;
  351. }, self, "analyze:forClass:", [aNode,aClass], smalltalk.ASTInterpreterTest)}
  352. }),
  353. smalltalk.ASTInterpreterTest);
  354. smalltalk.addMethod(
  355. "_interpret_",
  356. smalltalk.method({
  357. selector: "interpret:",
  358. fn: function (aString){
  359. var self=this;
  360. return smalltalk.withContext(function($ctx1) { var $1;
  361. $1=_st(_st((smalltalk.ASTInterpreter || ASTInterpreter))._new())._interpret_(_st(_st(_st(self)._parse_forClass_(aString,(smalltalk.Object || Object)))._nodes())._first());
  362. return $1;
  363. }, self, "interpret:", [aString], smalltalk.ASTInterpreterTest)}
  364. }),
  365. smalltalk.ASTInterpreterTest);
  366. smalltalk.addMethod(
  367. "_parse_",
  368. smalltalk.method({
  369. selector: "parse:",
  370. fn: function (aString){
  371. var self=this;
  372. return smalltalk.withContext(function($ctx1) { var $1;
  373. $1=_st(_st((smalltalk.Smalltalk || Smalltalk))._current())._parse_(aString);
  374. return $1;
  375. }, self, "parse:", [aString], smalltalk.ASTInterpreterTest)}
  376. }),
  377. smalltalk.ASTInterpreterTest);
  378. smalltalk.addMethod(
  379. "_parse_forClass_",
  380. smalltalk.method({
  381. selector: "parse:forClass:",
  382. fn: function (aString,aClass){
  383. var self=this;
  384. return smalltalk.withContext(function($ctx1) { var $1;
  385. $1=_st(self)._analyze_forClass_(_st(self)._parse_(aString),aClass);
  386. return $1;
  387. }, self, "parse:forClass:", [aString,aClass], smalltalk.ASTInterpreterTest)}
  388. }),
  389. smalltalk.ASTInterpreterTest);
  390. smalltalk.addMethod(
  391. "_testBinarySend",
  392. smalltalk.method({
  393. selector: "testBinarySend",
  394. fn: function (){
  395. var self=this;
  396. return smalltalk.withContext(function($ctx1) { _st(self)._assert_equals_(_st(self)._interpret_("foo 2+3+4"),(9));
  397. return self}, self, "testBinarySend", [], smalltalk.ASTInterpreterTest)}
  398. }),
  399. smalltalk.ASTInterpreterTest);
  400. smalltalk.addMethod(
  401. "_testBlockLiteral",
  402. smalltalk.method({
  403. selector: "testBlockLiteral",
  404. fn: function (){
  405. var self=this;
  406. return smalltalk.withContext(function($ctx1) { _st(self)._assert_equals_(_st(self)._interpret_("foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]"),(1));
  407. _st(self)._assert_equals_(_st(self)._interpret_("foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]"),(1));
  408. _st(self)._assert_equals_(_st(self)._interpret_("foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]"),(2));
  409. return self}, self, "testBlockLiteral", [], smalltalk.ASTInterpreterTest)}
  410. }),
  411. smalltalk.ASTInterpreterTest);
  412. smalltalk.addMethod(
  413. "_testCascade",
  414. smalltalk.method({
  415. selector: "testCascade",
  416. fn: function (){
  417. var self=this;
  418. 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)));
  419. return self}, self, "testCascade", [], smalltalk.ASTInterpreterTest)}
  420. }),
  421. smalltalk.ASTInterpreterTest);