123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- Smalltalk current createPackage: 'Compiler-Tests' properties: #{}!
- TestCase subclass: #SemanticAnalyzerTest
- instanceVariableNames: 'analyzer'
- package: 'Compiler-Tests'!
- !SemanticAnalyzerTest methodsFor: 'running'!
- setUp
- analyzer := SemanticAnalyzer on: Object
- ! !
- !SemanticAnalyzerTest methodsFor: 'tests'!
- testAssignment
- | src ast |
- src := 'foo self := 1'.
- ast := smalltalk parse: src.
- self should: [analyzer visit: ast] raise: InvalidAssignmentError
- !
- testNonLocalReturn
- | src ast |
- src := 'foo | a | a + 1. ^ a'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self deny: ast hasNonLocalReturn
- !
- testNonLocalReturn2
- | src ast |
- src := 'foo | a | a + 1. [ [ ^ a] ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self assert: ast hasNonLocalReturn
- !
- testScope
- | src ast |
- src := 'foo | a | a + 1. [ | b | b := a ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self deny: ast nodes first nodes last scope == ast scope.
- !
- testScope2
- | src ast |
- src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
- !
- testUnknownVariables
- | src ast |
- src := 'foo | a | b + a'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self assert: ast scope unknownVariables = #('b')
- !
- testUnknownVariablesWithScope
- | src ast |
- src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- self assert: ast scope unknownVariables = #('c' 'd' )
- !
- testVariableShadowing
- | src ast |
- src := 'foo | a | a + 1'.
- ast := smalltalk parse: src.
- analyzer visit: ast
- !
- testVariableShadowing2
- | src ast |
- src := 'foo | a | a + 1. [ | a | a := 2 ]'.
- ast := smalltalk parse: src.
- self should: [analyzer visit: ast] raise: ShadowingVariableError
- !
- testVariableShadowing3
- | src ast |
- src := 'foo | a | a + 1. [ | b | b := 2 ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast
- !
- testVariableShadowing4
- | src ast |
- src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast
- !
- testVariableShadowing5
- | src ast |
- src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
- ast := smalltalk parse: src.
- self should: [analyzer visit: ast] raise: ShadowingVariableError
- !
- testVariablesLookup
- | src ast |
- src := 'foo | a | a + 1. [ | b | b := a ]'.
- ast := smalltalk parse: src.
- analyzer visit: ast.
- "Binding for `a` in the message send"
- self assert: ast nodes first nodes first receiver binding isTempVar.
- self assert: ast nodes first nodes first receiver binding scope == ast scope.
- "Binding for `b`"
- self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
- self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
- ! !
|