compiler/TSemanticAnalyser.st
changeset 13 97090c2baa33
parent 11 6d39860d0fdb
child 15 10a95d798b36
equal deleted inserted replaced
12:d716a8181fc1 13:97090c2baa33
    12 !TSemanticAnalyser class methodsFor:'documentation'!
    12 !TSemanticAnalyser class methodsFor:'documentation'!
    13 
    13 
    14 documentation
    14 documentation
    15 "
    15 "
    16     This is the very first pass on the code. Its responsibility is:
    16     This is the very first pass on the code. Its responsibility is:
    17     * initialize bindings including types (except for message sends as those
    17     * initialize bindings 
    18       depends on type analysis)
       
    19     * initialize scopes (i.e, assign scopes and populate them
    18     * initialize scopes (i.e, assign scopes and populate them
    20       with variables)
    19       with variable bindings)
    21 
    20 
    22     [author:]
    21     [author:]
    23         Jan Vrany <jan.vrany@fit.cvut.cz>
    22         Jan Vrany <jan.vrany@fit.cvut.cz>
    24 
    23 
    25     [instance variables:]
    24     [instance variables:]
    39     anRBVariableNode parent isSequence ifTrue:[ 
    38     anRBVariableNode parent isSequence ifTrue:[ 
    40         binding := TLocalBinding name:anRBVariableNode name.
    39         binding := TLocalBinding name:anRBVariableNode name.
    41     ] ifFalse:[ 
    40     ] ifFalse:[ 
    42         binding := TArgumentBinding name:anRBVariableNode name.
    41         binding := TArgumentBinding name:anRBVariableNode name.
    43         binding index: (anRBVariableNode parent arguments indexOf: anRBVariableNode)                                
    42         binding index: (anRBVariableNode parent arguments indexOf: anRBVariableNode)                                
    44                        + (currentScope isMethodScope ifTrue:[1] ifFalse:[0])     
    43                        + (anRBVariableNode parent scope isMethodScope ifTrue:[1] ifFalse:[0])     
    45     ].
    44     ].
    46     anRBVariableNode parent scope addVariable: binding.
    45     anRBVariableNode parent scope addVariable: binding.
    47     super visitArgument: anRBVariableNode.
    46     super visitArgument: anRBVariableNode.
    48 
    47 
    49     "Created: / 25-08-2015 / 22:51:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    48     "Created: / 25-08-2015 / 22:51:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    50     "Modified: / 02-09-2015 / 08:58:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    49     "Modified: / 19-09-2015 / 06:17:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    51 ! !
    50 ! !
    52 
    51 
    53 !TSemanticAnalyser methodsFor:'visitor-double dispatching'!
    52 !TSemanticAnalyser methodsFor:'visitor-double dispatching'!
    54 
    53 
    55 acceptBlockNode: aBlockNode
    54 acceptBlockNode: aBlockNode
    56     | scope |
    55     | scope |
    57     aBlockNode parent isSpecialFormNode ifTrue:[ 
    56     aBlockNode parent isSpecialFormNode ifTrue:[ 
    58         scope := currentScope subScope: aBlockNode.
    57         scope := TScope node: aBlockNode parent: aBlockNode parent scope
    59     ] ifFalse:[ 
    58     ] ifFalse:[ 
    60         scope := TScope new.
    59         scope := TScope node: aBlockNode
    61     ].
    60     ].
    62     aBlockNode scope: scope.
    61     aBlockNode scope: scope.
    63     super acceptBlockNode: aBlockNode
    62     super acceptBlockNode: aBlockNode
    64 
    63 
    65     "Created: / 25-08-2015 / 22:30:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    64     "Created: / 25-08-2015 / 22:30:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    66     "Modified: / 14-09-2015 / 14:04:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    65     "Modified: / 19-09-2015 / 06:16:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    67 !
    66 !
    68 
    67 
    69 acceptLiteralNode: aRBLiteralNode
    68 acceptLiteralNode: aRBLiteralNode
    70     | value |
    69     | value |
    71 
    70 
    72     super acceptLiteralNode: aRBLiteralNode.
    71     super acceptLiteralNode: aRBLiteralNode.
    73     value := aRBLiteralNode value.
    72     value := aRBLiteralNode value.
    74     value isInteger ifTrue:[ 
    73     value isInteger ifTrue:[ 
    75         aRBLiteralNode binding: (TConstantBinding value: value type: (context environment binding lookupClassSIntegerW) type).
    74         aRBLiteralNode binding: (TConstantBinding value: value).
    76         ^ self.
    75         ^ self.
    77     ].
    76     ].
    78     value isBoolean ifTrue:[ 
    77     value isBoolean ifTrue:[ 
    79         aRBLiteralNode binding: (TConstantBinding value: (value ifTrue:[1] ifFalse:[0]) type: (context environment binding lookupClassBoolean) type).
    78         aRBLiteralNode binding: (TConstantBinding value: (value ifTrue:[1] ifFalse:[0]) type: (context environment binding lookupClassBoolean) type).
    80         ^ self.
    79         ^ self.
    81     ].
    80     ].
    82     self erorr: 'Unsupported constant'.
    81     self erorr: 'Unsupported constant'.
    83 
    82 
    84     "Created: / 25-08-2015 / 23:17:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    83     "Created: / 25-08-2015 / 23:17:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    85     "Modified: / 15-09-2015 / 08:27:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    84     "Modified: / 20-09-2015 / 07:12:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    86 !
    85 !
    87 
    86 
    88 acceptMethodNode: aMethodNode
    87 acceptMethodNode: aMethodNode
    89     | scope bindingForSelf |
    88     | scope bindingForSelf |
    90 
    89 
   100     "Created: / 25-08-2015 / 22:29:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    99     "Created: / 25-08-2015 / 22:29:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   101     "Modified: / 13-09-2015 / 09:30:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   100     "Modified: / 13-09-2015 / 09:30:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   102 !
   101 !
   103 
   102 
   104 acceptVariableNode: aVariableNode
   103 acceptVariableNode: aVariableNode
   105     aVariableNode binding: (aVariableNode scope lookupVariable: aVariableNode name).
   104     | binding |
       
   105 
       
   106     binding := aVariableNode scope lookupVariable: aVariableNode name.      
       
   107     binding isNil ifTrue:[ 
       
   108         context reportSemanticError: ('Undeclared variable %1' bindWith: aVariableNode name).
       
   109         ^ self.
       
   110     ].
       
   111     aVariableNode binding: binding.
   106     super acceptVariableNode: aVariableNode
   112     super acceptVariableNode: aVariableNode
   107 
   113 
   108     "Created: / 25-08-2015 / 23:00:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   114     "Created: / 25-08-2015 / 23:00:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   115     "Modified: / 20-09-2015 / 06:14:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   109 ! !
   116 ! !
   110 
   117 
   111 !TSemanticAnalyser class methodsFor:'documentation'!
   118 !TSemanticAnalyser class methodsFor:'documentation'!
   112 
   119 
   113 version
   120 version