compiler/TTypechecker.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 20 Sep 2015 12:01:42 +0100
changeset 13 97090c2baa33
parent 11 6d39860d0fdb
child 14 fa42d3f1a578
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 }"

TCompilerPass subclass:#TTypechecker
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Tea-Compiler-Internals'
!

!TTypechecker methodsFor:'visiting'!

visitArgument: anRBVariableNode
    | binding |


    super visitArgument: anRBVariableNode.
    binding := anRBVariableNode binding.
    binding type: (anRBVariableNode typeSpec asType)

    "Created: / 20-09-2015 / 07:19:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TTypechecker methodsFor:'visitor-double dispatching'!

acceptIfTrueIfFalseNode: node
    | receiverType booleanType |

    receiverType := node receiver binding type.
    booleanType := context environment binding lookupClassBoolean type.

    receiverType = booleanType ifFalse:[ 
        context reportTypeError: 'receiver of ifTrue:ifFalse: special form must be of type tBoolean (is ' , receiverType printString.
    ].

    "Created: / 14-09-2015 / 14:24:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-09-2015 / 08:29:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

acceptIfTrueNode: node
    | receiverType booleanType |

    receiverType := node binding type.
    booleanType := context environment binding lookupClassBoolean.

    receiverType = booleanType ifFalse:[ 
        context reportTypeError: 'receiver of ifTrue: special form must be of type tBoolean (is ' , receiverType printString.
    ].

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

acceptLiteralNode: aLiteralNode
    | binding value |

    binding := aLiteralNode binding.
    value := binding value.
    value isInteger ifTrue:[ 
        binding type: (context environment binding lookupClassSIntegerW) type.
        ^ self
    ].
    value isBoolean ifTrue:[ 
        binding type: (context environment binding lookupClassBoolean) type.
        ^ self.
    ].
    context reportTypeError: 'Unsupported constant type'.

    "Created: / 20-09-2015 / 07:13:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

acceptMessageNode: aMessageNode 
    | receiverType receiverBinding methodBinding |

    super acceptMessageNode: aMessageNode.
    receiverType := aMessageNode receiver binding type.
    receiverType isSimpleType ifTrue:[ 
        receiverBinding := context environment binding lookupClassNamed: receiverType name.
        methodBinding := receiverBinding lookupMethodNamed: aMessageNode selector.
        1 to: aMessageNode arguments size do:[:paramIdx |  
            | actualParamType formalParamType |    
            actualParamType := (aMessageNode arguments at: paramIdx) binding type.
            formalParamType := methodBinding parameterTypes at: paramIdx.
            (actualParamType isSubtypeOf: formalParamType) ifFalse:[ 
                context reportTypeError: ('Type mismatch for parameter %1 (expected %2, got %3)' bindWith: paramIdx with: formalParamType with: actualParamType).
                ^ self.
            ].
        ].
        aMessageNode binding: methodBinding.
        ^ self.
    ].
    self notYetImplemented

    "Created: / 02-09-2015 / 10:34:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 14-09-2015 / 14:22:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !