Beispiel #1
0
def resolveAssign(parentScope: Scope, target: Node, type_: Type, addVar=True):
    target.parentScope = parentScope
    target.resolvedType = type_

    if target.nodeType == NodeType.NAME:
        name = target.getText()
        symbol = parentScope.findNested(name)

        if not symbol:
            symbol = Symbol(name, type_)
            parentScope.define(symbol)

        target.symbol = symbol

    elif target.nodeType == NodeType.TUPLE:
        elts = target.getChild('elts')
        for i, elt in enumerate(elts):
            resolveAssign(parentScope, elt, type_.elementTypes[i], addVar)

    elif target.nodeType == NodeType.ATTR:
        var_name = target.getChild('value').getText()
        var_attr = target.getChild('attr')
        tmp = target

        while tmp != None and tmp.nodeType != NodeType.FUNCTION:
            tmp = tmp.parent

        if tmp and tmp.getChild('name') == '__init__':
            args = tmp.getChild('args').getChild('args')
            if args and args[0].getChild('arg') == var_name:
                classType = tmp.parentScope.classType
                classType.addField(var_attr, type_)

    elif target.nodeType == NodeType.SUBSCRIPT:
        pass
    else:
        raise Exception('Unknown node type:' + target.nodeType)
Beispiel #2
0
def getType(scope: Scope, typeName: str) -> Type:
    symbol = scope.findNested(typeName)
    if symbol:
        return symbol.resolvedType