1
0

Compiler-Tests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. smalltalk.addPackage('Compiler-Tests', {});
  2. smalltalk.addClass('SemanticAnalyzerTest', smalltalk.TestCase, ['analyzer'], 'Compiler-Tests');
  3. smalltalk.addMethod(
  4. "_setUp",
  5. smalltalk.method({
  6. selector: "setUp",
  7. category: 'running',
  8. fn: function () {
  9. var self=this;
  10. (self['@analyzer']=smalltalk.send((smalltalk.SemanticAnalyzer || SemanticAnalyzer), "_on_", [(smalltalk.Object || Object)]));
  11. return self;},
  12. args: [],
  13. source: "setUp\x0a\x09analyzer := SemanticAnalyzer on: Object",
  14. messageSends: ["on:"],
  15. referencedClasses: ["SemanticAnalyzer", "Object"]
  16. }),
  17. smalltalk.SemanticAnalyzerTest);
  18. smalltalk.addMethod(
  19. "_testAssignment",
  20. smalltalk.method({
  21. selector: "testAssignment",
  22. category: 'tests',
  23. fn: function () {
  24. var self=this;
  25. var src=nil;
  26. var ast=nil;
  27. (src="foo self := 1");
  28. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  29. smalltalk.send(self, "_should_raise_", [(function(){return smalltalk.send(self['@analyzer'], "_visit_", [ast]);}), (smalltalk.InvalidAssignmentError || InvalidAssignmentError)]);
  30. return self;},
  31. args: [],
  32. source: "testAssignment\x0a\x09| src ast |\x0a\x0a\x09src := 'foo self := 1'.\x0a\x09ast := smalltalk parse: src.\x0a\x09self should: [analyzer visit: ast] raise: InvalidAssignmentError",
  33. messageSends: ["parse:", "should:raise:", "visit:"],
  34. referencedClasses: ["InvalidAssignmentError"]
  35. }),
  36. smalltalk.SemanticAnalyzerTest);
  37. smalltalk.addMethod(
  38. "_testNonLocalReturn",
  39. smalltalk.method({
  40. selector: "testNonLocalReturn",
  41. category: 'tests',
  42. fn: function () {
  43. var self=this;
  44. var src=nil;
  45. var ast=nil;
  46. (src="foo | a | a + 1. ^ a");
  47. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  48. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  49. smalltalk.send(self, "_deny_", [smalltalk.send(ast, "_hasNonLocalReturn", [])]);
  50. return self;},
  51. args: [],
  52. source: "testNonLocalReturn\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | a + 1. ^ a'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self deny: ast hasNonLocalReturn",
  53. messageSends: ["parse:", "visit:", "deny:", "hasNonLocalReturn"],
  54. referencedClasses: []
  55. }),
  56. smalltalk.SemanticAnalyzerTest);
  57. smalltalk.addMethod(
  58. "_testNonLocalReturn2",
  59. smalltalk.method({
  60. selector: "testNonLocalReturn2",
  61. category: 'tests',
  62. fn: function () {
  63. var self=this;
  64. var src=nil;
  65. var ast=nil;
  66. (src="foo | a | a + 1. [ [ ^ a] ]");
  67. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  68. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  69. smalltalk.send(self, "_assert_", [smalltalk.send(ast, "_hasNonLocalReturn", [])]);
  70. return self;},
  71. args: [],
  72. source: "testNonLocalReturn2\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | a + 1. [ [ ^ a] ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self assert: ast hasNonLocalReturn",
  73. messageSends: ["parse:", "visit:", "assert:", "hasNonLocalReturn"],
  74. referencedClasses: []
  75. }),
  76. smalltalk.SemanticAnalyzerTest);
  77. smalltalk.addMethod(
  78. "_testScope",
  79. smalltalk.method({
  80. selector: "testScope",
  81. category: 'tests',
  82. fn: function () {
  83. var self=this;
  84. var src=nil;
  85. var ast=nil;
  86. (src="foo | a | a + 1. [ | b | b := a ]");
  87. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  88. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  89. smalltalk.send(self, "_deny_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_last", []), "_scope", []), "__eq_eq", [smalltalk.send(ast, "_scope", [])])]);
  90. return self;},
  91. args: [],
  92. source: "testScope\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | a + 1. [ | b | b := a ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self deny: ast nodes first nodes last scope == ast scope.",
  93. messageSends: ["parse:", "visit:", "deny:", "==", "scope", "last", "nodes", "first"],
  94. referencedClasses: []
  95. }),
  96. smalltalk.SemanticAnalyzerTest);
  97. smalltalk.addMethod(
  98. "_testScope2",
  99. smalltalk.method({
  100. selector: "testScope2",
  101. category: 'tests',
  102. fn: function () {
  103. var self=this;
  104. var src=nil;
  105. var ast=nil;
  106. (src="foo | a | a + 1. [ [ | b | b := a ] ]");
  107. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  108. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  109. smalltalk.send(self, "_deny_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_last", []), "_nodes", []), "_first", []), "_nodes", []), "_first", []), "_scope", []), "__eq_eq", [smalltalk.send(ast, "_scope", [])])]);
  110. return self;},
  111. args: [],
  112. source: "testScope2\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.",
  113. messageSends: ["parse:", "visit:", "deny:", "==", "scope", "first", "nodes", "last"],
  114. referencedClasses: []
  115. }),
  116. smalltalk.SemanticAnalyzerTest);
  117. smalltalk.addMethod(
  118. "_testUnknownVariables",
  119. smalltalk.method({
  120. selector: "testUnknownVariables",
  121. category: 'tests',
  122. fn: function () {
  123. var self=this;
  124. var src=nil;
  125. var ast=nil;
  126. (src="foo | a | b + a");
  127. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  128. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  129. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(ast, "_scope", []), "_unknownVariables", []), "__eq", [["b"]])]);
  130. return self;},
  131. args: [],
  132. source: "testUnknownVariables\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | b + a'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self assert: ast scope unknownVariables = #('b')",
  133. messageSends: ["parse:", "visit:", "assert:", "=", "unknownVariables", "scope"],
  134. referencedClasses: []
  135. }),
  136. smalltalk.SemanticAnalyzerTest);
  137. smalltalk.addMethod(
  138. "_testUnknownVariablesWithScope",
  139. smalltalk.method({
  140. selector: "testUnknownVariablesWithScope",
  141. category: 'tests',
  142. fn: function () {
  143. var self=this;
  144. var src=nil;
  145. var ast=nil;
  146. (src="foo | a b | [ c + 1. [ a + 1. d + 1 ]]");
  147. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  148. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  149. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(ast, "_scope", []), "_unknownVariables", []), "__eq", [["c", "d"]])]);
  150. return self;},
  151. args: [],
  152. source: "testUnknownVariablesWithScope\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09self assert: ast scope unknownVariables = #('c' 'd' )",
  153. messageSends: ["parse:", "visit:", "assert:", "=", "unknownVariables", "scope"],
  154. referencedClasses: []
  155. }),
  156. smalltalk.SemanticAnalyzerTest);
  157. smalltalk.addMethod(
  158. "_testVariableShadowing",
  159. smalltalk.method({
  160. selector: "testVariableShadowing",
  161. category: 'tests',
  162. fn: function () {
  163. var self=this;
  164. var src=nil;
  165. var ast=nil;
  166. (src="foo | a | a + 1");
  167. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  168. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  169. return self;},
  170. args: [],
  171. source: "testVariableShadowing\x0a\x09| src ast |\x0a\x09src := 'foo | a | a + 1'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast",
  172. messageSends: ["parse:", "visit:"],
  173. referencedClasses: []
  174. }),
  175. smalltalk.SemanticAnalyzerTest);
  176. smalltalk.addMethod(
  177. "_testVariableShadowing2",
  178. smalltalk.method({
  179. selector: "testVariableShadowing2",
  180. category: 'tests',
  181. fn: function () {
  182. var self=this;
  183. var src=nil;
  184. var ast=nil;
  185. (src="foo | a | a + 1. [ | a | a := 2 ]");
  186. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  187. smalltalk.send(self, "_should_raise_", [(function(){return smalltalk.send(self['@analyzer'], "_visit_", [ast]);}), (smalltalk.ShadowingVariableError || ShadowingVariableError)]);
  188. return self;},
  189. args: [],
  190. source: "testVariableShadowing2\x0a\x09| src ast |\x0a\x09src := 'foo | a | a + 1. [ | a | a := 2 ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09self should: [analyzer visit: ast] raise: ShadowingVariableError",
  191. messageSends: ["parse:", "should:raise:", "visit:"],
  192. referencedClasses: ["ShadowingVariableError"]
  193. }),
  194. smalltalk.SemanticAnalyzerTest);
  195. smalltalk.addMethod(
  196. "_testVariableShadowing3",
  197. smalltalk.method({
  198. selector: "testVariableShadowing3",
  199. category: 'tests',
  200. fn: function () {
  201. var self=this;
  202. var src=nil;
  203. var ast=nil;
  204. (src="foo | a | a + 1. [ | b | b := 2 ]");
  205. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  206. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  207. return self;},
  208. args: [],
  209. source: "testVariableShadowing3\x0a\x09| src ast |\x0a\x09src := 'foo | a | a + 1. [ | b | b := 2 ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast",
  210. messageSends: ["parse:", "visit:"],
  211. referencedClasses: []
  212. }),
  213. smalltalk.SemanticAnalyzerTest);
  214. smalltalk.addMethod(
  215. "_testVariableShadowing4",
  216. smalltalk.method({
  217. selector: "testVariableShadowing4",
  218. category: 'tests',
  219. fn: function () {
  220. var self=this;
  221. var src=nil;
  222. var ast=nil;
  223. (src="foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]");
  224. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  225. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  226. return self;},
  227. args: [],
  228. source: "testVariableShadowing4\x0a\x09| src ast |\x0a\x09src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast",
  229. messageSends: ["parse:", "visit:"],
  230. referencedClasses: []
  231. }),
  232. smalltalk.SemanticAnalyzerTest);
  233. smalltalk.addMethod(
  234. "_testVariableShadowing5",
  235. smalltalk.method({
  236. selector: "testVariableShadowing5",
  237. category: 'tests',
  238. fn: function () {
  239. var self=this;
  240. var src=nil;
  241. var ast=nil;
  242. (src="foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]");
  243. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  244. smalltalk.send(self, "_should_raise_", [(function(){return smalltalk.send(self['@analyzer'], "_visit_", [ast]);}), (smalltalk.ShadowingVariableError || ShadowingVariableError)]);
  245. return self;},
  246. args: [],
  247. source: "testVariableShadowing5\x0a\x09| src ast |\x0a\x09src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09self should: [analyzer visit: ast] raise: ShadowingVariableError",
  248. messageSends: ["parse:", "should:raise:", "visit:"],
  249. referencedClasses: ["ShadowingVariableError"]
  250. }),
  251. smalltalk.SemanticAnalyzerTest);
  252. smalltalk.addMethod(
  253. "_testVariablesLookup",
  254. smalltalk.method({
  255. selector: "testVariablesLookup",
  256. category: 'tests',
  257. fn: function () {
  258. var self=this;
  259. var src=nil;
  260. var ast=nil;
  261. (src="foo | a | a + 1. [ | b | b := a ]");
  262. (ast=smalltalk.send((typeof smalltalk == 'undefined' ? nil : smalltalk), "_parse_", [src]));
  263. smalltalk.send(self['@analyzer'], "_visit_", [ast]);
  264. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_first", []), "_receiver", []), "_binding", []), "_isTempVar", [])]);
  265. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_first", []), "_receiver", []), "_binding", []), "_scope", []), "__eq_eq", [smalltalk.send(ast, "_scope", [])])]);
  266. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_last", []), "_nodes", []), "_first", []), "_nodes", []), "_first", []), "_left", []), "_binding", []), "_isTempVar", [])]);
  267. smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_last", []), "_nodes", []), "_first", []), "_nodes", []), "_first", []), "_left", []), "_binding", []), "_scope", []), "__eq_eq", [smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(ast, "_nodes", []), "_first", []), "_nodes", []), "_last", []), "_scope", [])])]);
  268. return self;},
  269. args: [],
  270. source: "testVariablesLookup\x0a\x09| src ast |\x0a\x0a\x09src := 'foo | a | a + 1. [ | b | b := a ]'.\x0a\x09ast := smalltalk parse: src.\x0a\x09analyzer visit: ast.\x0a\x0a\x09\x22Binding for `a` in the message send\x22\x0a\x09self assert: ast nodes first nodes first receiver binding isTempVar.\x0a\x09self assert: ast nodes first nodes first receiver binding scope == ast scope.\x0a\x0a\x09\x22Binding for `b`\x22\x0a\x09self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.\x0a\x09self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.",
  271. messageSends: ["parse:", "visit:", "assert:", "isTempVar", "binding", "receiver", "first", "nodes", "==", "scope", "left", "last"],
  272. referencedClasses: []
  273. }),
  274. smalltalk.SemanticAnalyzerTest);