view sema/SymbolTable.d @ 53:da551f90e03f new_gen

Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
author Anders Johnsen <skabet@gmail.com>
date Sat, 26 Apr 2008 18:52:27 +0200
parents 69464d465284
children 4ae365eff712
line wrap: on
line source

module sema.SymbolTable;

import tango.io.Stdout;

import lexer.Token,
       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 (enclosing)
            res = enclosing.names;
        foreach (id, sym; symbols)
            res ~= sym.id.name ~ " : " ~ sym.type.name;
        return res;
    }

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

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

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

    Symbol parentFunction(Symbol f)
    {
        func = f;
        return f;
    }
    DType[char[]] types;
private:
    Symbol[Identifier] symbols;
    Symbol func;
}