Package litil

Source Code of litil.TypeScope

package litil;

import litil.ast.Type;

import java.util.HashMap;
import java.util.Map;

public class TypeScope {

    private final Map<String, Type> scope;
    private final TypeScope parent;

    private TypeScope(TypeScope parent, Map<String, Type> scope) {
        this.parent = parent;
        this.scope = scope;
    }

    public TypeScope(TypeScope parent) {
        this(parent, new HashMap<String, Type>());
    }

    public TypeScope() {
        this(null, new HashMap<String, Type>());
    }

    public void define(String name, Type type) {
        scope.put(name, type);
    }

    public Type get(String name) {
        Type res = scope.get(name);
        if (res == null && parent != null) {
            res = parent.get(name);
        }
        return res;
    }

    public TypeScope child() {
        return new TypeScope(this);
    }

    @Override
    public String toString() {
        return scope + (parent == null ? "-|" : "->" + parent);
    }
}
TOP

Related Classes of litil.TypeScope

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.