1
0

Compiler-Tests.st 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Smalltalk current createPackage: 'Compiler-Tests' properties: #{}!
  2. TestCase subclass: #IRASTTranslatorTest
  3. instanceVariableNames: ''
  4. package: 'Compiler-Tests'!
  5. !IRASTTranslatorTest methodsFor: 'tests'!
  6. testIRMethod
  7. ! !
  8. TestCase subclass: #ScopeVarTest
  9. instanceVariableNames: ''
  10. package: 'Compiler-Tests'!
  11. !ScopeVarTest methodsFor: 'tests'!
  12. testClassRefVar
  13. | node |
  14. node := ClassReferenceNode new
  15. value: 'Object';
  16. yourself.
  17. SemanticAnalyzer new visit: node.
  18. self assert: node binding isClassRefVar
  19. !
  20. testInstanceVar
  21. | node scope |
  22. node := VariableNode new
  23. value: 'bzzz';
  24. yourself.
  25. scope := MethodLexicalScope new.
  26. scope addIVar: 'bzzz'.
  27. self assert: (scope bindingFor: node) isInstanceVar
  28. !
  29. testPseudoVar
  30. | node pseudoVars |
  31. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  32. pseudoVars do: [:each |
  33. node := VariableNode new
  34. value: each;
  35. yourself.
  36. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  37. !
  38. testTempVar
  39. | node scope |
  40. node := VariableNode new
  41. value: 'bzzz';
  42. yourself.
  43. scope := MethodLexicalScope new.
  44. scope addTemp: 'bzzz'.
  45. self assert: (scope bindingFor: node) isTempVar
  46. !
  47. testUnknownVar
  48. | node |
  49. node := VariableNode new
  50. value: 'bzzz';
  51. yourself.
  52. self assert: (MethodLexicalScope new bindingFor: node) isNil
  53. ! !
  54. TestCase subclass: #SemanticAnalyzerTest
  55. instanceVariableNames: 'analyzer'
  56. package: 'Compiler-Tests'!
  57. !SemanticAnalyzerTest methodsFor: 'running'!
  58. setUp
  59. analyzer := SemanticAnalyzer on: Object
  60. ! !
  61. !SemanticAnalyzerTest methodsFor: 'tests'!
  62. testAssignment
  63. | src ast |
  64. src := 'foo self := 1'.
  65. ast := smalltalk parse: src.
  66. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  67. !
  68. testNonLocalReturn
  69. | src ast |
  70. src := 'foo | a | a + 1. ^ a'.
  71. ast := smalltalk parse: src.
  72. analyzer visit: ast.
  73. self deny: ast hasNonLocalReturn
  74. !
  75. testNonLocalReturn2
  76. | src ast |
  77. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  78. ast := smalltalk parse: src.
  79. analyzer visit: ast.
  80. self assert: ast hasNonLocalReturn
  81. !
  82. testScope
  83. | src ast |
  84. src := 'foo | a | a + 1. [ | b | b := a ]'.
  85. ast := smalltalk parse: src.
  86. analyzer visit: ast.
  87. self deny: ast nodes first nodes last scope == ast scope.
  88. !
  89. testScope2
  90. | src ast |
  91. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  92. ast := smalltalk parse: src.
  93. analyzer visit: ast.
  94. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  95. !
  96. testScopeLevel
  97. | src ast |
  98. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  99. ast := smalltalk parse: src.
  100. analyzer visit: ast.
  101. self assert: ast scope scopeLevel = 1.
  102. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel = 3
  103. !
  104. testUnknownVariables
  105. | src ast |
  106. src := 'foo | a | b + a'.
  107. ast := smalltalk parse: src.
  108. analyzer visit: ast.
  109. self assert: ast scope unknownVariables = #('b')
  110. !
  111. testUnknownVariablesWithScope
  112. | src ast |
  113. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  114. ast := smalltalk parse: src.
  115. analyzer visit: ast.
  116. self assert: ast scope unknownVariables = #('c' 'd' )
  117. !
  118. testVariableShadowing
  119. | src ast |
  120. src := 'foo | a | a + 1'.
  121. ast := smalltalk parse: src.
  122. analyzer visit: ast
  123. !
  124. testVariableShadowing2
  125. | src ast |
  126. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  127. ast := smalltalk parse: src.
  128. self should: [analyzer visit: ast] raise: ShadowingVariableError
  129. !
  130. testVariableShadowing3
  131. | src ast |
  132. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  133. ast := smalltalk parse: src.
  134. analyzer visit: ast
  135. !
  136. testVariableShadowing4
  137. | src ast |
  138. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  139. ast := smalltalk parse: src.
  140. analyzer visit: ast
  141. !
  142. testVariableShadowing5
  143. | src ast |
  144. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  145. ast := smalltalk parse: src.
  146. self should: [analyzer visit: ast] raise: ShadowingVariableError
  147. !
  148. testVariablesLookup
  149. | src ast |
  150. src := 'foo | a | a + 1. [ | b | b := a ]'.
  151. ast := smalltalk parse: src.
  152. analyzer visit: ast.
  153. "Binding for `a` in the message send"
  154. self assert: ast nodes first nodes first receiver binding isTempVar.
  155. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  156. "Binding for `b`"
  157. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  158. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  159. ! !