view sema/Declarations.d @ 28:69464d465284 new_gen

Now supporting structs - both read and write. Still a few errors though, so watch out.
author Anders Johnsen <skabet@gmail.com>
date Sun, 20 Apr 2008 11:20:28 +0200
parents 2d28b21faad6
children 41d23f2762c3
line wrap: on
line source

module sema.Declarations;

import sema.Visitor,
       sema.DType;

import tango.io.Stdout;

import misc.Error;

class Declarations : Visitor!(void)
{
    int[char[]] types;

    override void visitIdentifier(Identifier i)
    {
        auto symbol = i.env.find(i);

        if(symbol is null)
            throw new Error("Undefined identifier: '"~i.get~"'",i.token.location);

    }

    override void visitVarDecl(VarDecl d)
    {
        if(!d.env.findType(d.type))
            throw new Error("Undefined type: '"~d.type.get~"'",d.type.token.location);

        visitExp(d.identifier);
        if (d.init)
            visitExp(d.init);
    }
    override void visitFuncDecl(FuncDecl f)
    {
        visitExp(f.identifier);

        foreach (stmt; f.statements)
            visitStmt(stmt);
    }

    override void visitMemberLookup(MemberLookup m)
    {
        switch(m.target.expType)
        {
            case ExpType.Identifier:
                auto target = cast(Identifier)m.target;
                auto child = m.child;
                auto st = cast(DStruct)(target.env.find(target).type);
                if((child.get in st.members) == null)
                    throw new Error("Undefined member in "~target.get~" with type "~st.name,st.getLoc);
        }
    }


    bool isType(char[] s)
    {
        return (s in types? true : false);
    }

}