changeset 75:86aec2160221 new_gen

Parsing "int* x"
author Anders Johnsen <skabet@gmail.com>
date Fri, 02 May 2008 15:05:02 +0200
parents 192da4976daa
children 9171f04dd9ee
files parser/Action.d parser/Parser.d
diffstat 2 files changed, 43 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/parser/Action.d	Fri May 02 13:19:23 2008 +0200
+++ b/parser/Action.d	Fri May 02 15:05:02 2008 +0200
@@ -22,8 +22,14 @@
 }
 
 
-struct Id
+class Id
 {
+    public static Id opCall(Token tok)
+    {
+        auto id = new Id();
+        id.tok = tok;
+        return id;
+    }
     Token tok;
 
     char[] toString()
@@ -32,6 +38,18 @@
     }
 }
 
+class PointerId : Id
+{
+    public static PointerId opCall(Id id)
+    {
+        auto p = new PointerId();
+        p.id = id;
+        return p;
+    }
+
+    Id id;
+}
+
 /**
   All methods are optional.
 
--- a/parser/Parser.d	Fri May 02 13:19:23 2008 +0200
+++ b/parser/Parser.d	Fri May 02 15:05:02 2008 +0200
@@ -39,7 +39,7 @@
 
         if (t.isBasicType || t.isIdentifier)
         {
-            Id type = Id(lexer.next);
+            Id type = parseType;//Id(lexer.next);
             Id iden = Id(require(Tok.Identifier));
             Token next = lexer.peek();
             if (next.type == Tok.Seperator)
@@ -180,11 +180,12 @@
                 Token n = lexer.peek(1);
                 // Must be an decl, if we start with a basic type, or two
                 // identifiers in a row
-                if (iden.isBasicType() && iden.isIdentifier() || n.isIdentifier())
+                if (iden.isBasicType() || iden.isIdentifier())
                 {
                     // manually hardcoded to only support "type id [= exp];"
                     // as that is the only thing the codegen understands
-                    Id type = Id(lexer.next);
+                    Id type = parseType;
+                    n = lexer.peek();
                     Id id = Id(lexer.next);
                     Exp init;
                     if (skip(Tok.Assign))
@@ -288,15 +289,31 @@
             .tok(tok);
     }
 
+    /**
+      Parse a type - this includes pointer and array(at some point) types.
+     */
     Id parseType()
     {
         Token type = lexer.next;
 
-        if (type.isBasicType || type.type == Tok.Identifier)
-            return Id(type);
+        Id currentType;
+
+        if ( !(type.isBasicType || type.type == Tok.Identifier) )
+            throw error(__LINE__, "Unexpected token in Type parsing. Got %0")
+                .arg(type.getType)
+                .tok(type);
 
-        char[] c = type.getType;
-        error(__LINE__, "Unexpected token in Type parsing. Got %0").arg(c);
+        currentType = Id(type);
+        type = lexer.peek;
+
+        while(type.type == Tok.Star)
+        {
+            currentType = PointerId(currentType);
+            lexer.next;
+            type = lexer.peek;
+        }
+
+        return currentType;
     }
 
 private: