compiler/TParserTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 22 Sep 2015 17:43:38 +0100
changeset 14 fa42d3f1a578
parent 9 569bf5707c7e
child 16 17a2d1d9f205
permissions -rw-r--r--
Removed syntax for inline assembly, use <primitive: [:asm | ... ]> syntax. This one is easier to implement and less introusive, syntax-wise. And follows Smalltalk tradiiton.

"{ Package: 'jv:tea/compiler' }"

"{ NameSpace: Smalltalk }"

TestCase subclass:#TParserTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Tea-Compiler-AST-Tests'
!

!TParserTests methodsFor:'tests'!

test_blockargs
    | method |    

    method := TParser parseMethod: 'foo <^ Float> [ :e <Float> | e = NaN ]'.

    self assert: method body statements first arguments size == 1.
    self assert: method body statements first arguments first typeSpec type name = 'Float'.
    self assert: method body statements first returnTypeSpec isNil.

    method := TParser parseMethod: 'foo <^ Float> [ :e <Float> <^ Boolean> | e = NaN ]'.

    self assert: method body statements first arguments size == 1.
    self assert: method body statements first arguments first typeSpec type name = 'Float'.
    self assert: method body statements first returnTypeSpec notNil.
    self assert: method body statements first returnTypeSpec type name = 'Boolean'.


    method := TParser parseMethod: 'foo <^ Float> [ <^ Float> | e = NaN ]'.
    self assert: method body statements first returnTypeSpec notNil.
    self assert: method body statements first returnTypeSpec type name = 'Float'.

    "Created: / 21-08-2015 / 07:10:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-09-2015 / 14:54:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_locals
    | method |    

    method := TParser parseMethod: 'foo <^ Float> | local1 <Integer> local2 <Float> | ^ local1'.

    method := TParser parseMethod: 'foo <^ Float> self something ifTrue:[ | local1 <Float> | local + instvar ]'.

    "Created: / 21-08-2015 / 07:06:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-09-2015 / 12:15:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_method_pattern_01
    | method |    

    method := TParser parseMethod: 'foo < ^ Integer >'.
    self assert: method returnTypeSpec notNil.
    self assert: method returnTypeSpec type name = 'Integer'.

    method := TParser parseMethod: 'foo: foo <Integer> < ^ Integer >'.

    self assert: method returnTypeSpec notNil.
    self assert: method returnTypeSpec type name = 'Integer'.
    self assert: method arguments first typeSpec notNil.
    self assert: method arguments first typeSpec type name = 'Integer'.

    method := TParser parseMethod: '+ anObject <Integer> < ^ Integer >'.
    self assert: method returnTypeSpec notNil.
    self assert: method returnTypeSpec type name = 'Integer'.
    self assert: method arguments first typeSpec notNil.
    self assert: method arguments first typeSpec type name = 'Integer'.

    self should: [ TParser parseMethod: '+ anObject ' ] raise: Error.
    self should: [ TParser parseMethod: '+ anObject < >' ] raise: Error.
    self should: [ TParser parseMethod: '+ anObject < i32 >' ] raise: Error.
    self should: [ TParser parseMethod: '+ anObject < i32 > < i32 >' ] raise: Error.
    self should: [ TParser parseMethod: '+ anObject < i32 > < ^  >' ] raise: Error.
    self should: [ TParser parseMethod: '+ anObject < i32 > < ^ i32 ' ] raise: Error.

    "Created: / 20-08-2015 / 17:01:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-08-2015 / 23:00:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_primitive_inline
    | method |    

    method := TParser parseMethod: 'foo < ^ Integer > <primitive: [:asm | asm ret: self]>'.
    self assert: method body statements isEmpty.
    self assert: method pragmas size == 1.
    self assert: method pragmas anElement arguments first isBlock.

    "Created: / 22-09-2015 / 16:51:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_special_form
    | method |    

    method := TParser parseMethod: 'foo < ^ Integer > true ifTrue:[]'.
    self assert: method body statements size = 1.
    self assert: method body statements first isSpecialFormNode.

    method := TParser parseMethod: 'foo < ^ Integer > true ifTrue:[] ifFalse:[]'.
    self assert: method body statements size = 1.
    self assert: method body statements first isSpecialFormNode.
    
    method := TParser parseMethod: 'foo < ^ Integer > [] whileTrue:[]'.
    self assert: method body statements size = 1.
    self assert: method body statements first isSpecialFormNode.

    "Created: / 14-09-2015 / 12:18:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !