Compiler-Interpreter.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. smalltalk.addPackage('Compiler-Interpreter', {});
  2. smalltalk.addClass('ASTBlockClosure', smalltalk.BlockClosure, ['astNode'], 'Compiler-Interpreter');
  3. smalltalk.ASTBlockClosure.comment="ASTBlockClosure is polymorph with BlockClosure. \x0aAn ASTBlockClosure is used to interpret a BlockNode, and override all \x22primitive\x22 methods (#value and co)."
  4. smalltalk.addMethod(
  5. "_astNode",
  6. smalltalk.method({
  7. selector: "astNode",
  8. category: 'accessing',
  9. fn: function (){
  10. var self=this;
  11. return self["@astNode"];
  12. },
  13. args: [],
  14. source: "astNode\x0a\x09^ astNode",
  15. messageSends: [],
  16. referencedClasses: []
  17. }),
  18. smalltalk.ASTBlockClosure);
  19. smalltalk.addMethod(
  20. "_astNode_",
  21. smalltalk.method({
  22. selector: "astNode:",
  23. category: 'accessing',
  24. fn: function (aNode){
  25. var self=this;
  26. self["@astNode"]=aNode;
  27. return self},
  28. args: ["aNode"],
  29. source: "astNode: aNode\x0a\x09astNode := aNode",
  30. messageSends: [],
  31. referencedClasses: []
  32. }),
  33. smalltalk.ASTBlockClosure);
  34. smalltalk.addMethod(
  35. "_value",
  36. smalltalk.method({
  37. selector: "value",
  38. category: 'evaluating',
  39. fn: function (){
  40. var self=this;
  41. var $1;
  42. $1=smalltalk.send(smalltalk.send((smalltalk.ASTInterpreter || ASTInterpreter),"_current",[]),"_blockValue_",[self]);
  43. return $1;
  44. },
  45. args: [],
  46. source: "value\x0a\x09^ ASTInterpreter current blockValue: self",
  47. messageSends: ["blockValue:", "current"],
  48. referencedClasses: ["ASTInterpreter"]
  49. }),
  50. smalltalk.ASTBlockClosure);
  51. smalltalk.addClass('ASTInterpreter', smalltalk.NodeVisitor, ['currentNode', 'context', 'shouldReturn'], 'Compiler-Interpreter');
  52. smalltalk.addMethod(
  53. "_blockValue_",
  54. smalltalk.method({
  55. selector: "blockValue:",
  56. category: 'interpreting',
  57. fn: function (anASTBlockClosure){
  58. var self=this;
  59. var $1;
  60. $1=smalltalk.send(self,"_interpret_",[smalltalk.send(smalltalk.send(smalltalk.send(anASTBlockClosure,"_astNode",[]),"_nodes",[]),"_first",[])]);
  61. return $1;
  62. },
  63. args: ["anASTBlockClosure"],
  64. source: "blockValue: anASTBlockClosure\x0a\x09^ self interpret: anASTBlockClosure astNode nodes first",
  65. messageSends: ["interpret:", "first", "nodes", "astNode"],
  66. referencedClasses: []
  67. }),
  68. smalltalk.ASTInterpreter);
  69. smalltalk.addMethod(
  70. "_context",
  71. smalltalk.method({
  72. selector: "context",
  73. category: 'accessing',
  74. fn: function (){
  75. var self=this;
  76. return self["@context"];
  77. },
  78. args: [],
  79. source: "context\x0a\x09^ context",
  80. messageSends: [],
  81. referencedClasses: []
  82. }),
  83. smalltalk.ASTInterpreter);
  84. smalltalk.addMethod(
  85. "_context_",
  86. smalltalk.method({
  87. selector: "context:",
  88. category: 'accessing',
  89. fn: function (aMethodContext){
  90. var self=this;
  91. self["@context"]=aMethodContext;
  92. return self},
  93. args: ["aMethodContext"],
  94. source: "context: aMethodContext\x0a\x09context := aMethodContext",
  95. messageSends: [],
  96. referencedClasses: []
  97. }),
  98. smalltalk.ASTInterpreter);
  99. smalltalk.addMethod(
  100. "_initialize",
  101. smalltalk.method({
  102. selector: "initialize",
  103. category: 'initialization',
  104. fn: function (){
  105. var self=this;
  106. smalltalk.send(self,"_initialize",[],smalltalk.NodeVisitor);
  107. self["@shouldReturn"]=false;
  108. return self},
  109. args: [],
  110. source: "initialize\x0a\x09super initialize.\x0a shouldReturn := false",
  111. messageSends: ["initialize"],
  112. referencedClasses: []
  113. }),
  114. smalltalk.ASTInterpreter);
  115. smalltalk.addMethod(
  116. "_interpret_",
  117. smalltalk.method({
  118. selector: "interpret:",
  119. category: 'interpreting',
  120. fn: function (aNode){
  121. var self=this;
  122. var $1;
  123. self["@shouldReturn"]=false;
  124. $1=smalltalk.send(self,"_interpretNode_",[aNode]);
  125. return $1;
  126. },
  127. args: ["aNode"],
  128. source: "interpret: aNode\x0a\x09shouldReturn := false.\x0a ^ self interpretNode: aNode",
  129. messageSends: ["interpretNode:"],
  130. referencedClasses: []
  131. }),
  132. smalltalk.ASTInterpreter);
  133. smalltalk.addMethod(
  134. "_interpretNode_",
  135. smalltalk.method({
  136. selector: "interpretNode:",
  137. category: 'interpreting',
  138. fn: function (aNode){
  139. var self=this;
  140. var $1;
  141. self["@currentNode"]=aNode;
  142. $1=smalltalk.send(self,"_visit_",[aNode]);
  143. return $1;
  144. },
  145. args: ["aNode"],
  146. source: "interpretNode: aNode\x0a\x09currentNode := aNode.\x0a ^ self visit: aNode",
  147. messageSends: ["visit:"],
  148. referencedClasses: []
  149. }),
  150. smalltalk.ASTInterpreter);
  151. smalltalk.addMethod(
  152. "_send_to_arguments_",
  153. smalltalk.method({
  154. selector: "send:to:arguments:",
  155. category: 'interpreting',
  156. fn: function (aSelector,anObject,aCollection){
  157. var self=this;
  158. var $1;
  159. $1=smalltalk.send(anObject,"_perform_withArguments_",[aSelector,aCollection]);
  160. return $1;
  161. },
  162. args: ["aSelector", "anObject", "aCollection"],
  163. source: "send: aSelector to: anObject arguments: aCollection\x0a\x09^ anObject perform: aSelector withArguments: aCollection",
  164. messageSends: ["perform:withArguments:"],
  165. referencedClasses: []
  166. }),
  167. smalltalk.ASTInterpreter);
  168. smalltalk.addMethod(
  169. "_visitBlockNode_",
  170. smalltalk.method({
  171. selector: "visitBlockNode:",
  172. category: 'visiting',
  173. fn: function (aNode){
  174. var self=this;
  175. var $2,$3,$1;
  176. $2=smalltalk.send((smalltalk.ASTBlockClosure || ASTBlockClosure),"_new",[]);
  177. smalltalk.send($2,"_astNode_",[aNode]);
  178. $3=smalltalk.send($2,"_yourself",[]);
  179. $1=$3;
  180. return $1;
  181. },
  182. args: ["aNode"],
  183. source: "visitBlockNode: aNode\x0a\x09^ ASTBlockClosure new\x0a \x09astNode: aNode;\x0a yourself",
  184. messageSends: ["astNode:", "new", "yourself"],
  185. referencedClasses: ["ASTBlockClosure"]
  186. }),
  187. smalltalk.ASTInterpreter);
  188. smalltalk.addMethod(
  189. "_visitReturnNode_",
  190. smalltalk.method({
  191. selector: "visitReturnNode:",
  192. category: 'visiting',
  193. fn: function (aNode){
  194. var self=this;
  195. var $1;
  196. self["@shouldReturn"]=true;
  197. $1=smalltalk.send(self,"_interpret_",[smalltalk.send(smalltalk.send(aNode,"_nodes",[]),"_first",[])]);
  198. return $1;
  199. },
  200. args: ["aNode"],
  201. source: "visitReturnNode: aNode\x0a\x09shouldReturn := true.\x0a ^ self interpret: aNode nodes first",
  202. messageSends: ["interpret:", "first", "nodes"],
  203. referencedClasses: []
  204. }),
  205. smalltalk.ASTInterpreter);
  206. smalltalk.addMethod(
  207. "_visitSendNode_",
  208. smalltalk.method({
  209. selector: "visitSendNode:",
  210. category: 'visiting',
  211. fn: function (aNode){
  212. var self=this;
  213. var $1;
  214. var receiver;
  215. var arguments;
  216. receiver=smalltalk.send(self,"_interpret_",[smalltalk.send(aNode,"_receiver",[])]);
  217. arguments=smalltalk.send(smalltalk.send(aNode,"_arguments",[]),"_collect_",[(function(each){
  218. return smalltalk.send(self,"_interpret_",[each]);
  219. })]);
  220. $1=smalltalk.send(self,"_send_to_arguments_",[smalltalk.send(aNode,"_selector",[]),receiver,arguments]);
  221. return $1;
  222. },
  223. args: ["aNode"],
  224. source: "visitSendNode: aNode\x0a\x09\x22TODO: Handle super sends\x22\x0a\x09| receiver arguments |\x0a \x0a receiver := self interpret: aNode receiver.\x0a arguments := aNode arguments collect: [ :each |\x0a\x09\x09self interpret: each ].\x0a \x0a ^ self send: aNode selector to: receiver arguments: arguments",
  225. messageSends: ["interpret:", "receiver", "collect:", "arguments", "send:to:arguments:", "selector"],
  226. referencedClasses: []
  227. }),
  228. smalltalk.ASTInterpreter);
  229. smalltalk.addMethod(
  230. "_visitSequenceNode_",
  231. smalltalk.method({
  232. selector: "visitSequenceNode:",
  233. category: 'visiting',
  234. fn: function (aNode){
  235. var self=this;
  236. var $1;
  237. var $early={};
  238. try {
  239. smalltalk.send(smalltalk.send(smalltalk.send(aNode,"_nodes",[]),"_allButLast",[]),"_do_",[(function(each){
  240. var value;
  241. value=smalltalk.send(self,"_interpret_",[each]);
  242. value;
  243. if(smalltalk.assert(self["@shouldReturn"])){
  244. throw $early=[value];
  245. };
  246. })]);
  247. $1=smalltalk.send(self,"_interpret_",[smalltalk.send(smalltalk.send(aNode,"_nodes",[]),"_last",[])]);
  248. return $1;
  249. }
  250. catch(e) {if(e===$early)return e[0]; throw e}
  251. },
  252. args: ["aNode"],
  253. source: "visitSequenceNode: aNode\x0a\x09aNode nodes allButLast do: [ :each | | value |\x0a value := self interpret: each.\x0a\x09\x09shouldReturn ifTrue: [ ^ value ] ].\x0a ^ self interpret: aNode nodes last",
  254. messageSends: ["do:", "interpret:", "ifTrue:", "allButLast", "nodes", "last"],
  255. referencedClasses: []
  256. }),
  257. smalltalk.ASTInterpreter);
  258. smalltalk.addMethod(
  259. "_visitValueNode_",
  260. smalltalk.method({
  261. selector: "visitValueNode:",
  262. category: 'visiting',
  263. fn: function (aNode){
  264. var self=this;
  265. var $1;
  266. $1=smalltalk.send(aNode,"_value",[]);
  267. return $1;
  268. },
  269. args: ["aNode"],
  270. source: "visitValueNode: aNode\x0a\x09^ aNode value",
  271. messageSends: ["value"],
  272. referencedClasses: []
  273. }),
  274. smalltalk.ASTInterpreter);
  275. smalltalk.ASTInterpreter.klass.iVarNames = ['current'];
  276. smalltalk.addMethod(
  277. "_current",
  278. smalltalk.method({
  279. selector: "current",
  280. category: 'instance creation',
  281. fn: function (){
  282. var self=this;
  283. var $1;
  284. if(($receiver = self["@current"]) == nil || $receiver == undefined){
  285. self["@current"]=smalltalk.send(self,"_new",[],smalltalk.NodeVisitor.klass);
  286. $1=self["@current"];
  287. } else {
  288. $1=self["@current"];
  289. };
  290. return $1;
  291. },
  292. args: [],
  293. source: "current\x0a\x09^ current ifNil: [ current := super new ]",
  294. messageSends: ["ifNil:", "new"],
  295. referencedClasses: []
  296. }),
  297. smalltalk.ASTInterpreter.klass);
  298. smalltalk.addMethod(
  299. "_new",
  300. smalltalk.method({
  301. selector: "new",
  302. category: 'instance creation',
  303. fn: function (){
  304. var self=this;
  305. smalltalk.send(self,"_shouldNotImplement",[]);
  306. return self},
  307. args: [],
  308. source: "new\x0a\x09self shouldNotImplement",
  309. messageSends: ["shouldNotImplement"],
  310. referencedClasses: []
  311. }),
  312. smalltalk.ASTInterpreter.klass);