compiler/TParserTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 25 Sep 2015 03:51:15 +0100
changeset 16 17a2d1d9f205
parent 14 fa42d3f1a578
permissions -rw-r--r--
Added standalone Tea compiler - teak It allows for compilation of .tea files from the command line.

"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
"{ Package: 'jv:tea/compiler' }"

"{ NameSpace: Smalltalk }"

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

!TParserTests class methodsFor:'documentation'!

copyright
"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
! !

!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>"
! !