Compiler-Tests.st 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Smalltalk current createPackage: 'Compiler-Tests' properties: #{}!
  2. TestCase subclass: #SemanticAnalyzerTest
  3. instanceVariableNames: 'analyzer'
  4. package: 'Compiler-Tests'!
  5. !SemanticAnalyzerTest methodsFor: 'running'!
  6. setUp
  7. analyzer := SemanticAnalyzer on: Object
  8. ! !
  9. !SemanticAnalyzerTest methodsFor: 'tests'!
  10. testAssignment
  11. | src ast |
  12. src := 'foo self := 1'.
  13. ast := smalltalk parse: src.
  14. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  15. !
  16. testNonLocalReturn
  17. | src ast |
  18. src := 'foo | a | a + 1. ^ a'.
  19. ast := smalltalk parse: src.
  20. analyzer visit: ast.
  21. self deny: ast hasNonLocalReturn
  22. !
  23. testNonLocalReturn2
  24. | src ast |
  25. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  26. ast := smalltalk parse: src.
  27. analyzer visit: ast.
  28. self assert: ast hasNonLocalReturn
  29. !
  30. testScope
  31. | src ast |
  32. src := 'foo | a | a + 1. [ | b | b := a ]'.
  33. ast := smalltalk parse: src.
  34. analyzer visit: ast.
  35. self deny: ast nodes first nodes last scope == ast scope.
  36. !
  37. testScope2
  38. | src ast |
  39. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  40. ast := smalltalk parse: src.
  41. analyzer visit: ast.
  42. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  43. !
  44. testScopeLevel
  45. | src ast |
  46. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  47. ast := smalltalk parse: src.
  48. analyzer visit: ast.
  49. self assert: ast scope scopeLevel = 1.
  50. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel = 3
  51. !
  52. testUnknownVariables
  53. | src ast |
  54. src := 'foo | a | b + a'.
  55. ast := smalltalk parse: src.
  56. analyzer visit: ast.
  57. self assert: ast scope unknownVariables = #('b')
  58. !
  59. testUnknownVariablesWithScope
  60. | src ast |
  61. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  62. ast := smalltalk parse: src.
  63. analyzer visit: ast.
  64. self assert: ast scope unknownVariables = #('c' 'd' )
  65. !
  66. testVariableShadowing
  67. | src ast |
  68. src := 'foo | a | a + 1'.
  69. ast := smalltalk parse: src.
  70. analyzer visit: ast
  71. !
  72. testVariableShadowing2
  73. | src ast |
  74. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  75. ast := smalltalk parse: src.
  76. self should: [analyzer visit: ast] raise: ShadowingVariableError
  77. !
  78. testVariableShadowing3
  79. | src ast |
  80. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  81. ast := smalltalk parse: src.
  82. analyzer visit: ast
  83. !
  84. testVariableShadowing4
  85. | src ast |
  86. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  87. ast := smalltalk parse: src.
  88. analyzer visit: ast
  89. !
  90. testVariableShadowing5
  91. | src ast |
  92. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  93. ast := smalltalk parse: src.
  94. self should: [analyzer visit: ast] raise: ShadowingVariableError
  95. !
  96. testVariablesLookup
  97. | src ast |
  98. src := 'foo | a | a + 1. [ | b | b := a ]'.
  99. ast := smalltalk parse: src.
  100. analyzer visit: ast.
  101. "Binding for `a` in the message send"
  102. self assert: ast nodes first nodes first receiver binding isTempVar.
  103. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  104. "Binding for `b`"
  105. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  106. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  107. ! !