view sema/Scope.d @ 99:857f0d530789 new_gen

Imports and improved module statement Allow "module a.b.c" Supports most forms of D's import. import A, B; import A, B = C; import A, B : a = b, c;
author Anders Halager <halager@gmail.com>
date Tue, 06 May 2008 21:59:22 +0200
parents 621cedba53ea
children fea8d61a2451
line wrap: on
line source

module sema.Scope;

import tango.io.Stdout;

import lexer.Token,
       ast.Module,
       ast.Decl,
       ast.Exp;

public 
import sema.DType;

class Scope
{
    this() {}
    this(Scope enclosing)
    {
        this.enclosing = enclosing;
        this.func = enclosing.func;
        this.inModule = enclosing.inModule;
    }

    Scope enclosing;
    Module inModule;

    void add(Identifier id)
    {
        symbols[id] = id;
    }

    Identifier find(Identifier id)
    {
        if(!id)
            return null;
        if (auto sym = id in symbols)
            return *sym;
        if (enclosing !is null)
            return enclosing.find(id);
        return null;
    }

    DType findType(Identifier id)
    {
        if (auto type = id.get in types)
                return *type;
        if (enclosing !is null)
            return enclosing.findType(id);
        return null;
    }

    char[][] names()
    {
        char[][] res;
        if (parentFunction() !is null)
            res ~= "pf: " ~ parentFunction().identifier.get;
        if (enclosing)
            res = enclosing.names;
        foreach (id, sym; symbols)
            res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
        return res;
    }

    FuncDecl parentFunction()
    {
        if (func !is null)
            return func;
        else if (enclosing !is null)
            return enclosing.parentFunction();
        else
            return null;
    }

    int stmtIndex()
    {
        if (currentStmtIndex != -1)
            return currentStmtIndex;
        else if (enclosing !is null)
            return enclosing.stmtIndex();
        else
            return -1;
    }

    int opEquals(Object o)
    {
        return this is o;
    }

    char[] toString()
    {
        if (func)
            return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length);
        return Stdout.layout.convert("root: {}", symbols.length);
    }

    FuncDecl parentFunction(FuncDecl f)
    {
        func = f;
        return f;
    }
    DType[char[]] types;
    int currentStmtIndex = -1;
private:
    Identifier[Identifier] symbols;
    FuncDecl func;
}