view sema/SymbolTable.d @ 59:1d6f4ad38a91 new_gen

Make most of the tests pass again Still have two bugs, one is that type conversion has been diabled for return statements, the other is at line ~540 of CodeGen.d, were the following error is emitted: Can't find an implicit conversion between { i32 } and i32
author Anders Halager <halager@gmail.com>
date Tue, 29 Apr 2008 00:31:56 +0200
parents 4ae365eff712
children 06dda301ea61
line wrap: on
line source

module sema.SymbolTable;

import tango.io.Stdout;

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

import sema.DType;

public
import sema.Symbol;

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

    Scope enclosing;

    Symbol add(Identifier id)
    {
        auto s = new Symbol;
        s.id = id;
        symbols[id] = s;
        return s;
    }

    Symbol find(Identifier id)
    {
        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.id.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:
    Symbol[Identifier] symbols;
    FuncDecl func;
}