1
0

Compiler-Tests.st 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. testUnknownVariables
  45. | src ast |
  46. src := 'foo | a | b + a'.
  47. ast := smalltalk parse: src.
  48. analyzer visit: ast.
  49. self assert: ast scope unknownVariables = #('b')
  50. !
  51. testUnknownVariablesWithScope
  52. | src ast |
  53. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  54. ast := smalltalk parse: src.
  55. analyzer visit: ast.
  56. self assert: ast scope unknownVariables = #('c' 'd' )
  57. !
  58. testVariableShadowing
  59. | src ast |
  60. src := 'foo | a | a + 1'.
  61. ast := smalltalk parse: src.
  62. analyzer visit: ast
  63. !
  64. testVariableShadowing2
  65. | src ast |
  66. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  67. ast := smalltalk parse: src.
  68. self should: [analyzer visit: ast] raise: ShadowingVariableError
  69. !
  70. testVariableShadowing3
  71. | src ast |
  72. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  73. ast := smalltalk parse: src.
  74. analyzer visit: ast
  75. !
  76. testVariableShadowing4
  77. | src ast |
  78. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  79. ast := smalltalk parse: src.
  80. analyzer visit: ast
  81. !
  82. testVariableShadowing5
  83. | src ast |
  84. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  85. ast := smalltalk parse: src.
  86. self should: [analyzer visit: ast] raise: ShadowingVariableError
  87. !
  88. testVariablesLookup
  89. | src ast |
  90. src := 'foo | a | a + 1. [ | b | b := a ]'.
  91. ast := smalltalk parse: src.
  92. analyzer visit: ast.
  93. "Binding for `a` in the message send"
  94. self assert: ast nodes first nodes first receiver binding isTempVar.
  95. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  96. "Binding for `b`"
  97. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  98. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  99. ! !