changeset 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 6282db07115f
children a51bdf15a33d
files dang/compiler.d gen/LLVMGen.d sema/Declarations.d test.td
diffstat 4 files changed, 67 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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);
     }
 
--- 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;
 
--- /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);
+    }
+
+}
--- 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;