123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- Smalltalk createPackage: 'Compiler-Tests'!
- TestCase subclass: #ASTParsingTest
- instanceVariableNames: ''
- package: 'Compiler-Tests'!
- !ASTParsingTest methodsFor: 'convenience'!
- analyze: aNode forClass: aClass
- (SemanticAnalyzer on: aClass) visit: aNode.
- ^ aNode
- ! !
- !ASTParsingTest methodsFor: 'parsing'!
- parse: aString
- ^ Smalltalk parse: aString
- !
- parse: aString forClass: aClass
- ^ self analyze: (self parse: aString) forClass: aClass
- ! !
- ASTParsingTest subclass: #ASTPCNodeVisitorTest
- instanceVariableNames: ''
- package: 'Compiler-Tests'!
- !ASTPCNodeVisitorTest methodsFor: 'factory'!
- astPCNodeVisitor
- ^ ASTPCNodeVisitor new
- context: (AIContext new
- yourself);
- yourself
- !
- astPCNodeVisitorForSelector: aString
- ^ ASTPCNodeVisitor new
- selector: aString;
- context: (AIContext new
- yourself);
- yourself
- ! !
- !ASTPCNodeVisitorTest methodsFor: 'tests'!
- testJSStatementNode
- | ast visitor |
-
- ast := self parse: 'foo <consolee.log(1)>' forClass: Object.
- self assert: (self astPCNodeVisitor
- visit: ast;
- currentNode) isJSStatementNode
- !
- testMessageSend
- | ast |
-
- ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
- self assert: ((self astPCNodeVisitorForSelector: 'yourself')
- visit: ast;
- currentNode) selector equals: 'yourself'
- !
- testMessageSendWithBlocks
- | ast |
-
- ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
- self assert: ((self astPCNodeVisitorForSelector: 'yourself')
- visit: ast;
- currentNode) selector equals: 'yourself'
- !
- testMessageSendWithInlining
- | ast |
-
- ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
- self assert: ((self astPCNodeVisitorForSelector: 'yourself')
- visit: ast;
- currentNode) selector equals: 'yourself'.
-
- ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
- self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
- visit: ast;
- currentNode) selector equals: 'asBoolean'
- !
- testNoMessageSend
- | ast |
-
- ast := self parse: 'foo ^ self' forClass: Object.
- self assert: (self astPCNodeVisitor
- visit: ast;
- currentNode) isNil
- !
- testPC
- | ast visitor |
-
- ast := self parse: 'foo <console.log(1)>' forClass: Object.
- self assert: (self astPCNodeVisitor
- visit: ast;
- currentNode) isJSStatementNode
- ! !
- ASTParsingTest subclass: #CodeGeneratorTest
- instanceVariableNames: 'receiver'
- package: 'Compiler-Tests'!
- !CodeGeneratorTest methodsFor: 'accessing'!
- codeGeneratorClass
- ^ CodeGenerator
- ! !
- !CodeGeneratorTest methodsFor: 'factory'!
- compiler
- ^ Compiler new
- codeGeneratorClass: self codeGeneratorClass;
- yourself
- ! !
- !CodeGeneratorTest methodsFor: 'initialization'!
- setUp
- receiver := DoIt new
- !
- tearDown
- "receiver := nil"
- ! !
- !CodeGeneratorTest methodsFor: 'testing'!
- should: aString receiver: anObject return: aResult
- | method result |
- receiver := anObject.
- method := self compiler install: aString forClass: anObject class protocol: 'tests'.
- result := receiver perform: method selector.
- anObject class removeCompiledMethod: method.
- self assert: aResult equals: result
- !
- should: aString return: anObject
- ^ self
- should: aString
- receiver: receiver
- return: anObject
- ! !
- !CodeGeneratorTest methodsFor: 'tests'!
- testAssignment
- self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
- self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
- self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
- !
- testBackslashSelectors
-
- self should: '\ arg ^ 4' return: 4.
- self should: '\\ arg ^ 42' return: 42
- !
- testBlockReturn
- self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
- self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
- self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
- !
- testCascades
-
- self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
- !
- testCascadesWithInlining
-
- self should: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 1.
- self should: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 2
- !
- testDynamicArrayElementsOrdered
- self should: 'foo
- | x |
- x := 1.
- ^ { x. x := 2 }
- ' return: #(1 2).
- self should: 'foo
- | x |
- x := 1.
- ^ { x. true ifTrue: [ x := 2 ] }
- ' return: #(1 2).
- !
- testDynamicDictionaryElementsOrdered
- self should: 'foo
- | x |
- x := ''foo''.
- ^ #{ x->1. ''bar''->(true ifTrue: [ 2 ]) }
- ' return: #{'foo'->1. 'bar'->2}.
- !
- testDynamicDictionaryWithMoreArrows
- self should: 'foo ^ #{1->2->3}' return: (HashedCollection with: 1->2->3)
- !
- testGlobalVar
- self should: 'foo ^ eval class' return: BlockClosure.
- self should: 'foo ^ Math cos: 0' return: 1.
- self should: 'foo ^ NonExistingVar' return: nil
- !
- testInnerTemporalDependentElementsOrdered
- self should: 'foo
- | x |
- x := Array.
- ^ x with: ''foo''->x with: ''bar''->(x := 2)
- ' return: {'foo'->Array. 'bar'->2}.
- self should: 'foo
- | x |
- x := Array.
- ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
- ' return: {'foo'->Array. 'bar'->2}.
- self should: 'foo
- | x |
- x := 1.
- ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
- ' return: {'foo'->1. 'bar'->2}.
- self should: 'foo
- | x |
- x := 1.
- ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
- ' return: {'foo'->1. 'bar'->2}.
- self should: 'foo
- | x |
- x := 1.
- ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
- ' return: #{'foo'->1. 'bar'->2}.
- !
- testJSStatement
- self should: 'foo <return 2+3>' return: 5
- !
- testLiterals
- self should: 'foo ^ 1' return: 1.
- self should: 'foo ^ ''hello''' return: 'hello'.
- self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
- self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
- self should: 'foo ^ true' return: true.
- self should: 'foo ^ false' return: false.
- self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
- self should: 'foo ^ #hello' return: #hello.
- self should: 'foo ^ -123.456' return: -123.456.
- self should: 'foo ^ -2.5e4' return: -25000.
- !
- testLocalReturn
- self should: 'foo ^ 1' return: 1.
- self should: 'foo ^ 1 + 1' return: 2.
- self should: 'foo ' return: receiver.
- self should: 'foo self asString' return: receiver.
- self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
- !
- testMessageSends
- self should: 'foo ^ 1 asString' return: '1'.
- self should: 'foo ^ 1 + 1' return: 2.
- self should: 'foo ^ 1 + 2 * 3' return: 9.
- self should: 'foo ^ 1 to: 3' return: #(1 2 3).
- self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
- !
- testMultipleSequences
- self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
- !
- testMutableLiterals
- "Mutable literals must be aliased in cascades.
- See https://github.com/amber-smalltalk/amber/issues/428"
-
- self
- should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
- return: #(3 2)
- !
- testNestedIfTrue
- self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
- self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
- self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
- self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
- !
- testNestedSends
- self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
- !
- testNonLocalReturn
- self should: 'foo [ ^ 1 ] value' return: 1.
- self should: 'foo [ ^ 1 + 1 ] value' return: 2.
- self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
- self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
- !
- testPascalCaseGlobal
- self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
- self should: 'foo ^NonExistent' return: nil
- !
- testSendReceiverAndArgumentsOrdered
- self should: 'foo
- | x |
- x := 1.
- ^ Array with: x with: (true ifTrue: [ x := 2 ])
- ' return: #(1 2).
- self should: 'foo
- | x |
- x := Array.
- ^ x with: x with: (true ifTrue: [ x := 2 ])
- ' return: {Array. 2}.
- !
- testSuperSend
- self
- should: 'foo ^ super isBoolean'
- receiver: true
- return: false
- !
- testTempVariables
- self should: 'foo | a | ^ a' return: nil.
- self should: 'foo | AVariable | ^ AVariable' return: nil.
- self should: 'foo | a b c | ^ c' return: nil.
- self should: 'foo | a | [ | d | ^ d ] value' return: nil.
-
- self should: 'foo | a | a:= 1. ^ a' return: 1.
- self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
- !
- testThisContext
- self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
- !
- testifFalse
- self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
- self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
-
- self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
- self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
- !
- testifFalseIfTrue
- self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
- self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
-
- self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
- self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
- !
- testifNil
- self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
- self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
- self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
- self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
- !
- testifNilIfNotNil
- self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
- self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
- self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
- self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
- !
- testifNotNil
- self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
- self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
- self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
- self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
- !
- testifNotNilWithArgument
- self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
- self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
-
- self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
- self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
-
- self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
- self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
- !
- testifTrue
- self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
- self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
-
- self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
- self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
- !
- testifTrueIfFalse
- self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
- self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
-
- self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
- self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
- ! !
- CodeGeneratorTest subclass: #InliningCodeGeneratorTest
- instanceVariableNames: ''
- package: 'Compiler-Tests'!
- !InliningCodeGeneratorTest methodsFor: 'accessing'!
- codeGeneratorClass
- ^ InliningCodeGenerator
- ! !
- CodeGeneratorTest subclass: #InterpreterTest
- instanceVariableNames: ''
- package: 'Compiler-Tests'!
- !InterpreterTest methodsFor: 'parsing'!
- analyze: aNode forClass: aClass
- (SemanticAnalyzer on: aClass) visit: aNode.
- ^ aNode
- !
- parse: aString
- ^ Smalltalk parse: aString
- !
- parse: aString forClass: aClass
- ^ self analyze: (self parse: aString) forClass: aClass
- ! !
- !InterpreterTest methodsFor: 'private'!
- interpret: aString receiver: anObject withArguments: aDictionary
- "The food is a methodNode. Interpret the sequenceNode only"
-
- | ctx interpreter |
-
- interpreter := ASTInterpreter new.
-
- ctx := AIContext new
- receiver: anObject;
- interpreter: interpreter;
- yourself.
- aDictionary keysAndValuesDo: [ :key :value |
- ctx localAt: key put: value ].
-
- ^ interpreter
- context: ctx;
- interpret: (self parse: aString forClass: anObject class) nextChild;
- proceed;
- result
- ! !
- !InterpreterTest methodsFor: 'testing'!
- should: aString receiver: anObject return: aResult
- receiver := anObject.
-
- ^ self
- assert: (self interpret: aString receiver: receiver withArguments: #{})
- equals: aResult
- !
- should: aString return: anObject
- ^ self
- should: aString
- receiver: receiver
- return: anObject
- ! !
- TestCase subclass: #ScopeVarTest
- instanceVariableNames: ''
- package: 'Compiler-Tests'!
- !ScopeVarTest methodsFor: 'tests'!
- testClassRefVar
- | node |
- node := VariableNode new
- value: 'Object';
- yourself.
- SemanticAnalyzer new
- pushScope: MethodLexicalScope new;
- visit: node.
- self assert: node binding isClassRefVar
- !
- testInstanceVar
- | node scope |
- node := VariableNode new
- value: 'bzzz';
- yourself.
- scope := MethodLexicalScope new.
- scope addIVar: 'bzzz'.
- self assert: (scope bindingFor: node) isInstanceVar
- !
- testPseudoVar
- | node pseudoVars |
- pseudoVars := #('self' 'super' 'true' 'false' 'nil').
- pseudoVars do: [:each |
- node := VariableNode new
- value: each;
- yourself.
- self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
- !
- testTempVar
- | node scope |
- node := VariableNode new
- value: 'bzzz';
- yourself.
- scope := MethodLexicalScope new.
- scope addTemp: 'bzzz'.
- self assert: (scope bindingFor: node) isTempVar
- !
- testUnknownVar
- | node |
- node := VariableNode new
- value: 'bzzz';
- yourself.
- self assert: (MethodLexicalScope new bindingFor: node) isNil
- ! !
- 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 scope hasNonLocalReturn
- !
- testNonLocalReturn2
- | src ast |
- src := 'foo | a | a + 1. [ [ ^ a] ]'.
- ast := Smalltalk parse: src.
- analyzer visit: ast.
- self assert: ast scope 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.
- !
- testScopeLevel
- | src ast |
- src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
- ast := Smalltalk parse: src.
- analyzer visit: ast.
- self assert: ast scope scopeLevel equals: 1.
- self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
- !
- testUnknownVariables
- | src ast |
- src := 'foo | a | b + a'.
- ast := Smalltalk parse: src.
- self should: [ analyzer visit: ast ] raise: UnknownVariableError
- !
- testUnknownVariablesWithScope
- | src ast |
- src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
- ast := Smalltalk parse: src.
-
- self should: [ analyzer visit: ast ] raise: UnknownVariableError
- !
- 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.
- ! !
|