compiler/TCompilerExamples.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 20 Sep 2015 12:01:42 +0100
changeset 13 97090c2baa33
parent 11 6d39860d0fdb
child 16 17a2d1d9f205
permissions -rw-r--r--
Fixes/refactoring of scopes and bindings. Fixed initialization of scopes and bindings. Make typechecker to seed types.

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

"{ NameSpace: Smalltalk }"

TestCase subclass:#TCompilerExamples
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Tea-Compiler-Examples'
!

!TCompilerExamples class methodsFor:'asserting'!

isTestSelector:aSelector
    ^ (super isTestSelector:aSelector) or:[ aSelector startsWith: 'example' ]

    "Created: / 14-09-2015 / 11:56:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TCompilerExamples methodsFor:'tests'!

example_factorialI
    | environment unit compiler|

    environment := TEnvironment new.
    unit := TSourceReader read:'
nil subclass: #FactorialI
    category: ''t-Examples''
!!
!!FactorialI class methodsFor:''examples''!!
factorialI:v <tSIntegerW> <^ tSIntegerW>
    | result <tSIntegerW> 
      i <tSIntegerW> |

    result := 0.
    i := v.

    [ i > 1 ] whileTrue:[ 
        result := result * i.
        i := i - 1
    ].
    ^ result
!! !!
    '.

    compiler := TCompiler new.
    compiler compile: unit in: environment.
    self halt.
    "
    compiler context module
    "

    "Created: / 19-09-2015 / 18:29:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example_if
    | environment unit compiler|

    environment := TEnvironment new.
    unit := TSourceReader read:'
nil subclass: #If
    category: ''t-Examples''
!!
!!If class methodsFor:''examples''!!
if <^ tSIntegerW> 
        true ifTrue:[ ^ 1 ] ifFalse:[ ^ 0 ]
    '.

    compiler := TCompiler new.
    compiler compile: unit in: environment.
    self halt.
    "
    compiler context module
    "

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

example_three_plus_four
    | environment unit compiler|

    environment := TEnvironment new.
    unit := TSourceReader read:'
nil subclass: #ThreePlusFour
    category: ''t-Examples''
!!
!!ThreePlusFour class methodsFor:''examples''!!
threePlusFour <^ tSIntegerW> 
        ^ 3 + 4

!! !!
    '.

    compiler := TCompiler new.
    compiler compile: unit in: environment.
    self halt.
    "
    compiler context module
    "

    "Created: / 14-09-2015 / 11:56:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-09-2015 / 15:15:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !