Search in sources :

Example 6 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class ImportFrom method importStar.

public void importStar(@NotNull State s, @Nullable Type mt) {
    if (mt == null || mt.file == null) {
        return;
    }
    Node node = Analyzer.self.getAstForFile(mt.file);
    if (node == null) {
        return;
    }
    List<String> names = new ArrayList<>();
    Type allType = mt.table.lookupType("__all__");
    if (allType != null && allType instanceof ListType) {
        ListType lt = (ListType) allType;
        for (Object o : lt.values) {
            if (o instanceof String) {
                names.add((String) o);
            }
        }
    }
    if (!names.isEmpty()) {
        int start = this.start;
        int col = this.col;
        for (String name : names) {
            Set<Binding> b = mt.table.lookupLocal(name);
            if (b != null) {
                s.update(name, b);
            } else {
                List<Name> m2 = new ArrayList<>(module);
                Name fakeName = new Name(name, this.file, start, start + name.length(), this.line, col);
                m2.add(fakeName);
                Type type = Analyzer.self.loadModule(m2, s);
                if (type != null) {
                    start += name.length();
                    col += name.length();
                    s.insert(name, fakeName, type, Binding.Kind.VARIABLE);
                }
            }
        }
    } else {
        // Fall back to importing all names not starting with "_".
        for (Entry<String, Set<Binding>> e : mt.table.entrySet()) {
            if (!e.getKey().startsWith("_")) {
                s.update(e.getKey(), e.getValue());
            }
        }
    }
}
Also used : Binding(org.yinwang.pysonar.Binding) Set(java.util.Set) ArrayList(java.util.ArrayList) Type(org.yinwang.pysonar.types.Type) ListType(org.yinwang.pysonar.types.ListType) ListType(org.yinwang.pysonar.types.ListType)

Example 7 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method setAttrType.

private void setAttrType(Attribute node, @NotNull Type targetType, @NotNull Type v) {
    if (targetType.isUnknownType()) {
        addWarningToNode(node, "Can't set attribute for UnknownType");
        return;
    }
    Set<Binding> bs = targetType.table.lookupAttr(node.attr.id);
    if (bs != null) {
        for (Binding b : bs) {
            b.addType(v);
            Analyzer.self.putRef(node.attr, b);
        }
    } else {
        targetType.table.insert(node.attr.id, node.attr, v, ATTRIBUTE);
    }
}
Also used : Binding(org.yinwang.pysonar.Binding)

Example 8 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method bind.

public static void bind(@NotNull State s, @NotNull Name name, @NotNull Type rvalue, Binding.Kind kind) {
    if (s.isGlobalName(name.id)) {
        Set<Binding> bs = s.lookup(name.id);
        if (bs != null) {
            for (Binding b : bs) {
                b.addType(rvalue);
                Analyzer.self.putRef(name, b);
            }
        }
    } else {
        s.insert(name.id, name, rvalue, kind);
    }
}
Also used : Binding(org.yinwang.pysonar.Binding)

Example 9 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Name node, State s) {
    Set<Binding> b = s.lookup(node.id);
    if (b != null) {
        Analyzer.self.putRef(node, b);
        Analyzer.self.resolved.add(node);
        Analyzer.self.unresolved.remove(node);
        return State.makeUnion(b);
    } else {
        addWarningToNode(node, "unbound variable " + node.id);
        Analyzer.self.unresolved.add(node);
        Type t = Types.UNKNOWN;
        t.table.setPath(s.extendPath(node.id));
        return t;
    }
}
Also used : Binding(org.yinwang.pysonar.Binding) ClassType(org.yinwang.pysonar.types.ClassType) ListType(org.yinwang.pysonar.types.ListType) DictType(org.yinwang.pysonar.types.DictType) InstanceType(org.yinwang.pysonar.types.InstanceType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TupleType(org.yinwang.pysonar.types.TupleType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Binding (org.yinwang.pysonar.Binding)9 ListType (org.yinwang.pysonar.types.ListType)5 Type (org.yinwang.pysonar.types.Type)5 ClassType (org.yinwang.pysonar.types.ClassType)4 DictType (org.yinwang.pysonar.types.DictType)4 FunType (org.yinwang.pysonar.types.FunType)4 InstanceType (org.yinwang.pysonar.types.InstanceType)4 ModuleType (org.yinwang.pysonar.types.ModuleType)4 TupleType (org.yinwang.pysonar.types.TupleType)4 UnionType (org.yinwang.pysonar.types.UnionType)4 NotNull (org.jetbrains.annotations.NotNull)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1