view sema/Scope.d @ 129:ed815b31479b

Added a Symbol
author Anders Halager <halager@gmail.com>
date Sat, 21 Jun 2008 20:41:18 +0200
parents 189c049cbfcc
children a14ac9e5c858
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,
       sema.Symbol;

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

    Scope enclosing;
    ModuleHandler mHandle;
    Module inModule;
    Scope[] imported;

    ImportDecl[] imports;

    void put(Identifier id, Decl d)
    {
        symbols[id] = d;
    }

    Decl find(Identifier id)
    {
        if(id is null)
            return null;
        else if (auto sym = id in symbols)
            return *sym;
        else if (enclosing !is null)
        {
            auto res = enclosing.find(id);
            if (res is null)
                return mHandle.find(getImports, id);
            return res;
        }
        return null;
    }

    ImportDecl[] getImports()
    {
        if(enclosing)
            return enclosing.getImports ~ imports;
        return imports;
    }

    DType findType(Identifier id)
    {
        if (auto type = id.get in types)
            return *type;
        if (enclosing !is null)
        {
            auto type = enclosing.findType(id);
            if(type is null)
                return mHandle.findType(getImports, id);
            return type;
        }
        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:
    Decl[Identifier] symbols;
    FuncDecl func;
}

class ModuleHandler
{
    void add(Module m)
    {
        modules[m.moduleName] = m;
    }
    void add(Module m, char[] file)
    {
        fileToModule[file] = m.moduleName;
        add(m);
    }

    DType findType(ImportDecl[] imports, Identifier type)
    {
        foreach(i ; imports)
            if(i.get in modules)
            {
                auto t = modules[i.get].env.findType(type);
                if(t !is null)
                    return t;
            }
        return null;
    }

    Decl find(ImportDecl[] imports, Identifier id)
    {
        foreach(i ; imports)
            if(i.get in modules)
            {
                auto t = modules[i.get].env.find(id);
                if(t !is null)
                    return t;
            }
        return null;
    }

    char[][char[]] fileToModule;
    Module[char[]] modules;
}