view sema/Declarations.d @ 13:e5caf9971207

Checking for types and identifiers. TODO: Make each varDecl create a new scope
author johnsen@johnsen-desktop
date Fri, 18 Apr 2008 14:47:23 +0200
parents
children 59bfbaf8847f
line wrap: on
line source

module sema.Declarations;

import sema.Visitor;

import tango.io.Stdout;

import misc.Error;

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

    this()
    {
        types = [
            "byte"[]:0,
            "ubyte":1,
            "short":2,
            "ushort":3,
            "int":4,
            "uint":5,
            "long":6,
            "ulong":7
                ];
    }

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

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

    }

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

        visitExp(d.type);
        visitExp(d.identifier);
        if (d.init)
            visitExp(d.init);
    }


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

}