# HG changeset patch # User johnsen@johnsen-desktop # Date 1208522843 -7200 # Node ID e5caf99712075d137102fb5748add53bd93c4cbb # Parent 6282db07115f7238edf555fb659667548ecf26f5 Checking for types and identifiers. TODO: Make each varDecl create a new scope diff -r 6282db07115f -r e5caf9971207 dang/compiler.d --- a/dang/compiler.d Fri Apr 18 13:58:27 2008 +0200 +++ b/dang/compiler.d Fri Apr 18 14:47:23 2008 +0200 @@ -17,7 +17,9 @@ import gen.LuaGen, gen.LLVMGen; -import sema.Visitor; +import sema.Visitor, + sema.SymbolTableBuilder, + sema.Declarations; import dang.OptParse; @@ -145,6 +147,9 @@ auto parser = new Parser; auto decls = parser.parse(lexer); + (new SymbolTableBuilder).visit(decls); + (new Declarations).visit(decls); + postParse(decls, src); } diff -r 6282db07115f -r e5caf9971207 gen/LLVMGen.d --- a/gen/LLVMGen.d Fri Apr 18 13:58:27 2008 +0200 +++ b/gen/LLVMGen.d Fri Apr 18 14:47:23 2008 +0200 @@ -47,7 +47,6 @@ void gen(Decl[] decls) { // Fill in scopes - (new SymbolTableBuilder).visit(decls); table.enterScope; diff -r 6282db07115f -r e5caf9971207 sema/Declarations.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sema/Declarations.d Fri Apr 18 14:47:23 2008 +0200 @@ -0,0 +1,53 @@ +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); + } + +} diff -r 6282db07115f -r e5caf9971207 test.td --- a/test.td Fri Apr 18 13:58:27 2008 +0200 +++ b/test.td Fri Apr 18 14:47:23 2008 +0200 @@ -3,21 +3,21 @@ int main() { - long var1 = 1; - short var2 = 2; - return nice(var1, var2); + return fib(10); } -int fac(int n) +int fib(int n) { - if (n == 1) - return 1; - return n * fac(n - 1); + if(n < 2) + return n; + + return fib(n-1) + fib(n-2); } int nice(long s, short t) { - byte x = 5 + t; + byte x = 5 + t + y; + int y; if (x != 0) t = 5 + 1 * 5 * s + t; return 2 * (t + -1) - x;