Compiler-Interpreter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. category: 'interpreting',
  8. fn: function (anASTBlockClosure){
  9. var self=this;
  10. return smalltalk.withContext(function($ctx) { var $1;
  11. $1=_st(self)._interpret_(_st(_st(_st(anASTBlockClosure)._astNode())._nodes())._first());
  12. return $1;
  13. }, self, "blockValue:", [anASTBlockClosure], smalltalk.ASTInterpreter)},
  14. args: ["anASTBlockClosure"],
  15. source: "blockValue: anASTBlockClosure\x0a\x09^ self interpret: anASTBlockClosure astNode nodes first",
  16. messageSends: ["interpret:", "first", "nodes", "astNode"],
  17. referencedClasses: []
  18. }),
  19. smalltalk.ASTInterpreter);
  20. smalltalk.addMethod(
  21. "_context",
  22. smalltalk.method({
  23. selector: "context",
  24. category: 'accessing',
  25. fn: function (){
  26. var self=this;
  27. return smalltalk.withContext(function($ctx) { return self["@context"];
  28. }, self, "context", [], smalltalk.ASTInterpreter)},
  29. args: [],
  30. source: "context\x0a\x09^ context",
  31. messageSends: [],
  32. referencedClasses: []
  33. }),
  34. smalltalk.ASTInterpreter);
  35. smalltalk.addMethod(
  36. "_context_",
  37. smalltalk.method({
  38. selector: "context:",
  39. category: 'accessing',
  40. fn: function (aMethodContext){
  41. var self=this;
  42. return smalltalk.withContext(function($ctx) { self["@context"]=aMethodContext;
  43. return self}, self, "context:", [aMethodContext], smalltalk.ASTInterpreter)},
  44. args: ["aMethodContext"],
  45. source: "context: aMethodContext\x0a\x09context := aMethodContext",
  46. messageSends: [],
  47. referencedClasses: []
  48. }),
  49. smalltalk.ASTInterpreter);
  50. smalltalk.addMethod(
  51. "_initialize",
  52. smalltalk.method({
  53. selector: "initialize",
  54. category: 'initialization',
  55. fn: function (){
  56. var self=this;
  57. return smalltalk.withContext(function($ctx) { smalltalk.NodeVisitor.fn.prototype._initialize.apply(_st(self), []);
  58. self["@shouldReturn"]=false;
  59. return self}, self, "initialize", [], smalltalk.ASTInterpreter)},
  60. args: [],
  61. source: "initialize\x0a\x09super initialize.\x0a shouldReturn := false",
  62. messageSends: ["initialize"],
  63. referencedClasses: []
  64. }),
  65. smalltalk.ASTInterpreter);
  66. smalltalk.addMethod(
  67. "_interpret_",
  68. smalltalk.method({
  69. selector: "interpret:",
  70. category: 'interpreting',
  71. fn: function (aNode){
  72. var self=this;
  73. return smalltalk.withContext(function($ctx) { var $1;
  74. self["@shouldReturn"]=false;
  75. $1=_st(self)._interpretNode_(aNode);
  76. return $1;
  77. }, self, "interpret:", [aNode], smalltalk.ASTInterpreter)},
  78. args: ["aNode"],
  79. source: "interpret: aNode\x0a\x09shouldReturn := false.\x0a ^ self interpretNode: aNode",
  80. messageSends: ["interpretNode:"],
  81. referencedClasses: []
  82. }),
  83. smalltalk.ASTInterpreter);
  84. smalltalk.addMethod(
  85. "_interpretNode_",
  86. smalltalk.method({
  87. selector: "interpretNode:",
  88. category: 'interpreting',
  89. fn: function (aNode){
  90. var self=this;
  91. return smalltalk.withContext(function($ctx) { var $1;
  92. self["@currentNode"]=aNode;
  93. $1=_st(self)._visit_(aNode);
  94. return $1;
  95. }, self, "interpretNode:", [aNode], smalltalk.ASTInterpreter)},
  96. args: ["aNode"],
  97. source: "interpretNode: aNode\x0a\x09currentNode := aNode.\x0a ^ self visit: aNode",
  98. messageSends: ["visit:"],
  99. referencedClasses: []
  100. }),
  101. smalltalk.ASTInterpreter);
  102. smalltalk.addMethod(
  103. "_send_to_arguments_",
  104. smalltalk.method({
  105. selector: "send:to:arguments:",
  106. category: 'interpreting',
  107. fn: function (aSelector,anObject,aCollection){
  108. var self=this;
  109. return smalltalk.withContext(function($ctx) { var $1;
  110. $1=_st(anObject)._perform_withArguments_(aSelector,aCollection);
  111. return $1;
  112. }, self, "send:to:arguments:", [aSelector,anObject,aCollection], smalltalk.ASTInterpreter)},
  113. args: ["aSelector", "anObject", "aCollection"],
  114. source: "send: aSelector to: anObject arguments: aCollection\x0a\x09^ anObject perform: aSelector withArguments: aCollection",
  115. messageSends: ["perform:withArguments:"],
  116. referencedClasses: []
  117. }),
  118. smalltalk.ASTInterpreter);
  119. smalltalk.addMethod(
  120. "_visitBlockNode_",
  121. smalltalk.method({
  122. selector: "visitBlockNode:",
  123. category: 'visiting',
  124. fn: function (aNode){
  125. var self=this;
  126. return smalltalk.withContext(function($ctx) { var $1;
  127. $1=(function(){
  128. return _st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  129. });
  130. return $1;
  131. }, self, "visitBlockNode:", [aNode], smalltalk.ASTInterpreter)},
  132. args: ["aNode"],
  133. source: "visitBlockNode: aNode\x0a ^ [ self interpretNode: aNode nodes first ]",
  134. messageSends: ["interpretNode:", "first", "nodes"],
  135. referencedClasses: []
  136. }),
  137. smalltalk.ASTInterpreter);
  138. smalltalk.addMethod(
  139. "_visitCascadeNode_",
  140. smalltalk.method({
  141. selector: "visitCascadeNode:",
  142. category: 'visiting',
  143. fn: function (aNode){
  144. var self=this;
  145. return smalltalk.withContext(function($ctx) { var $1;
  146. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  147. _st(each)._receiver_(_st(aNode)._receiver());
  148. return _st(self)._interpretNode_(each);
  149. }));
  150. _st(_st(_st(aNode)._nodes())._last())._receiver_(_st(aNode)._receiver());
  151. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._last());
  152. return $1;
  153. }, self, "visitCascadeNode:", [aNode], smalltalk.ASTInterpreter)},
  154. args: ["aNode"],
  155. source: "visitCascadeNode: aNode\x0a\x0a aNode nodes allButLast\x0a \x09do: [ :each | \x0a \x09each receiver: aNode receiver.\x0a\x09\x09\x09self interpretNode: each ].\x0a \x0a\x09aNode nodes last receiver: aNode receiver.\x0a ^ self interpretNode: aNode nodes last",
  156. messageSends: ["do:", "receiver:", "receiver", "interpretNode:", "allButLast", "nodes", "last"],
  157. referencedClasses: []
  158. }),
  159. smalltalk.ASTInterpreter);
  160. smalltalk.addMethod(
  161. "_visitJSStatementNode_",
  162. smalltalk.method({
  163. selector: "visitJSStatementNode:",
  164. category: 'visiting',
  165. fn: function (aNode){
  166. var self=this;
  167. return smalltalk.withContext(function($ctx) { _st(self)._halt();
  168. return self}, self, "visitJSStatementNode:", [aNode], smalltalk.ASTInterpreter)},
  169. args: ["aNode"],
  170. source: "visitJSStatementNode: aNode\x0a\x09self halt",
  171. messageSends: ["halt"],
  172. referencedClasses: []
  173. }),
  174. smalltalk.ASTInterpreter);
  175. smalltalk.addMethod(
  176. "_visitReturnNode_",
  177. smalltalk.method({
  178. selector: "visitReturnNode:",
  179. category: 'visiting',
  180. fn: function (aNode){
  181. var self=this;
  182. return smalltalk.withContext(function($ctx) { var $1;
  183. self["@shouldReturn"]=true;
  184. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._first());
  185. return $1;
  186. }, self, "visitReturnNode:", [aNode], smalltalk.ASTInterpreter)},
  187. args: ["aNode"],
  188. source: "visitReturnNode: aNode\x0a\x09shouldReturn := true.\x0a ^ self interpretNode: aNode nodes first",
  189. messageSends: ["interpretNode:", "first", "nodes"],
  190. referencedClasses: []
  191. }),
  192. smalltalk.ASTInterpreter);
  193. smalltalk.addMethod(
  194. "_visitSendNode_",
  195. smalltalk.method({
  196. selector: "visitSendNode:",
  197. category: 'visiting',
  198. fn: function (aNode){
  199. var self=this;
  200. return smalltalk.withContext(function($ctx) { var $1;
  201. var receiver;
  202. var arguments;
  203. receiver=_st(self)._interpretNode_(_st(aNode)._receiver());
  204. arguments=_st(_st(aNode)._arguments())._collect_((function(each){
  205. return _st(self)._interpretNode_(each);
  206. }));
  207. $1=_st(self)._send_to_arguments_(_st(aNode)._selector(),receiver,arguments);
  208. return $1;
  209. }, self, "visitSendNode:", [aNode], smalltalk.ASTInterpreter)},
  210. args: ["aNode"],
  211. source: "visitSendNode: aNode\x0a\x09\x22TODO: Handle super sends\x22\x0a\x09| receiver arguments |\x0a \x0a receiver := self interpretNode: aNode receiver.\x0a arguments := aNode arguments collect: [ :each |\x0a\x09\x09self interpretNode: each ].\x0a \x0a ^ self send: aNode selector to: receiver arguments: arguments",
  212. messageSends: ["interpretNode:", "receiver", "collect:", "arguments", "send:to:arguments:", "selector"],
  213. referencedClasses: []
  214. }),
  215. smalltalk.ASTInterpreter);
  216. smalltalk.addMethod(
  217. "_visitSequenceNode_",
  218. smalltalk.method({
  219. selector: "visitSequenceNode:",
  220. category: 'visiting',
  221. fn: function (aNode){
  222. var self=this;
  223. return smalltalk.withContext(function($ctx) { var $1;
  224. var $early={};
  225. try {
  226. _st(_st(_st(aNode)._nodes())._allButLast())._do_((function(each){
  227. var value;
  228. value=_st(self)._interpretNode_(each);
  229. value;
  230. if(smalltalk.assert(self["@shouldReturn"])){
  231. throw $early=[value];
  232. };
  233. }));
  234. $1=_st(self)._interpretNode_(_st(_st(aNode)._nodes())._last());
  235. return $1;
  236. }
  237. catch(e) {if(e===$early)return e[0]; throw e}
  238. }, self, "visitSequenceNode:", [aNode], smalltalk.ASTInterpreter)},
  239. args: ["aNode"],
  240. source: "visitSequenceNode: aNode\x0a\x09aNode nodes allButLast do: [ :each | | value |\x0a value := self interpretNode: each.\x0a\x09\x09shouldReturn ifTrue: [ ^ value ] ].\x0a ^ self interpretNode: aNode nodes last",
  241. messageSends: ["do:", "interpretNode:", "ifTrue:", "allButLast", "nodes", "last"],
  242. referencedClasses: []
  243. }),
  244. smalltalk.ASTInterpreter);
  245. smalltalk.addMethod(
  246. "_visitValueNode_",
  247. smalltalk.method({
  248. selector: "visitValueNode:",
  249. category: 'visiting',
  250. fn: function (aNode){
  251. var self=this;
  252. return smalltalk.withContext(function($ctx) { var $1;
  253. $1=_st(aNode)._value();
  254. return $1;
  255. }, self, "visitValueNode:", [aNode], smalltalk.ASTInterpreter)},
  256. args: ["aNode"],
  257. source: "visitValueNode: aNode\x0a\x09^ aNode value",
  258. messageSends: ["value"],
  259. referencedClasses: []
  260. }),
  261. smalltalk.ASTInterpreter);
  262. smalltalk.addClass('ASTInterpreterTest', smalltalk.TestCase, [], 'Compiler-Interpreter');
  263. smalltalk.addMethod(
  264. "_analyze_forClass_",
  265. smalltalk.method({
  266. selector: "analyze:forClass:",
  267. category: 'accessing',
  268. fn: function (aNode,aClass){
  269. var self=this;
  270. return smalltalk.withContext(function($ctx) { _st(_st((smalltalk.SemanticAnalyzer || SemanticAnalyzer))._on_(aClass))._visit_(aNode);
  271. return aNode;
  272. }, self, "analyze:forClass:", [aNode,aClass], smalltalk.ASTInterpreterTest)},
  273. args: ["aNode", "aClass"],
  274. source: "analyze: aNode forClass: aClass\x0a\x09(SemanticAnalyzer on: aClass) visit: aNode.\x0a ^ aNode",
  275. messageSends: ["visit:", "on:"],
  276. referencedClasses: ["SemanticAnalyzer"]
  277. }),
  278. smalltalk.ASTInterpreterTest);
  279. smalltalk.addMethod(
  280. "_interpret_",
  281. smalltalk.method({
  282. selector: "interpret:",
  283. category: 'accessing',
  284. fn: function (aString){
  285. var self=this;
  286. return smalltalk.withContext(function($ctx) { var $1;
  287. $1=_st(_st((smalltalk.ASTInterpreter || ASTInterpreter))._new())._interpret_(_st(_st(_st(self)._parse_forClass_(aString,(smalltalk.Object || Object)))._nodes())._first());
  288. return $1;
  289. }, self, "interpret:", [aString], smalltalk.ASTInterpreterTest)},
  290. args: ["aString"],
  291. source: "interpret: aString\x0a\x09\x22the food is a methodNode. Interpret the sequenceNode only\x22\x0a ^ ASTInterpreter new\x0a \x09interpret: (self parse: aString forClass: Object) \x0a \x09nodes first",
  292. messageSends: ["interpret:", "first", "nodes", "parse:forClass:", "new"],
  293. referencedClasses: ["Object", "ASTInterpreter"]
  294. }),
  295. smalltalk.ASTInterpreterTest);
  296. smalltalk.addMethod(
  297. "_parse_",
  298. smalltalk.method({
  299. selector: "parse:",
  300. category: 'accessing',
  301. fn: function (aString){
  302. var self=this;
  303. return smalltalk.withContext(function($ctx) { var $1;
  304. $1=_st(_st((smalltalk.Smalltalk || Smalltalk))._current())._parse_(aString);
  305. return $1;
  306. }, self, "parse:", [aString], smalltalk.ASTInterpreterTest)},
  307. args: ["aString"],
  308. source: "parse: aString\x0a\x09^ Smalltalk current parse: aString",
  309. messageSends: ["parse:", "current"],
  310. referencedClasses: ["Smalltalk"]
  311. }),
  312. smalltalk.ASTInterpreterTest);
  313. smalltalk.addMethod(
  314. "_parse_forClass_",
  315. smalltalk.method({
  316. selector: "parse:forClass:",
  317. category: 'accessing',
  318. fn: function (aString,aClass){
  319. var self=this;
  320. return smalltalk.withContext(function($ctx) { var $1;
  321. $1=_st(self)._analyze_forClass_(_st(self)._parse_(aString),aClass);
  322. return $1;
  323. }, self, "parse:forClass:", [aString,aClass], smalltalk.ASTInterpreterTest)},
  324. args: ["aString", "aClass"],
  325. source: "parse: aString forClass: aClass\x0a\x09^ self analyze: (self parse: aString) forClass: aClass",
  326. messageSends: ["analyze:forClass:", "parse:"],
  327. referencedClasses: []
  328. }),
  329. smalltalk.ASTInterpreterTest);
  330. smalltalk.addMethod(
  331. "_testBinarySend",
  332. smalltalk.method({
  333. selector: "testBinarySend",
  334. category: 'tests',
  335. fn: function (){
  336. var self=this;
  337. return smalltalk.withContext(function($ctx) { _st(self)._assert_equals_(_st(self)._interpret_("foo 2+3+4"),(9));
  338. return self}, self, "testBinarySend", [], smalltalk.ASTInterpreterTest)},
  339. args: [],
  340. source: "testBinarySend\x0a\x09self assert: (self interpret: 'foo 2+3+4') equals: 9",
  341. messageSends: ["assert:equals:", "interpret:"],
  342. referencedClasses: []
  343. }),
  344. smalltalk.ASTInterpreterTest);
  345. smalltalk.addMethod(
  346. "_testBlockLiteral",
  347. smalltalk.method({
  348. selector: "testBlockLiteral",
  349. category: 'tests',
  350. fn: function (){
  351. var self=this;
  352. return smalltalk.withContext(function($ctx) { _st(self)._assert_equals_(_st(self)._interpret_("foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]"),(1));
  353. _st(self)._assert_equals_(_st(self)._interpret_("foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]"),(1));
  354. _st(self)._assert_equals_(_st(self)._interpret_("foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]"),(2));
  355. return self}, self, "testBlockLiteral", [], smalltalk.ASTInterpreterTest)},
  356. args: [],
  357. source: "testBlockLiteral\x0a\x09self assert: (self interpret: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 1.\x0a self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]') equals: 1.\x0a self assert: (self interpret: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 2",
  358. messageSends: ["assert:equals:", "interpret:"],
  359. referencedClasses: []
  360. }),
  361. smalltalk.ASTInterpreterTest);