changeset 192:5c898f80b436

- Started implementation of parseDeclaration(). - Added classes FunctionDeclaration and FunctionBody. - Implemented parseFunctionBody(). - Simplified parseDeclaratorSuffix().
author aziz
date Sun, 15 Jul 2007 18:04:03 +0000
parents 3ce110cefbc5
children 2a2975b6d539
files trunk/src/Declarations.d trunk/src/Parser.d trunk/src/Statements.d
diffstat 3 files changed, 115 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Declarations.d	Sat Jul 14 20:56:05 2007 +0000
+++ b/trunk/src/Declarations.d	Sun Jul 15 18:04:03 2007 +0000
@@ -183,6 +183,21 @@
   }
 }
 
+class FunctionDeclaration : Declaration
+{
+  string funcName;
+  Type funcType;
+  TemplateParameter[] tparams;
+  FunctionBody funcBody;
+  this(string funcName, Type funcType, TemplateParameter[] tparams, FunctionBody funcBody)
+  {
+    super(hasBody);
+    this.funcName = funcName;
+    this.funcType = funcType;
+    this.funcBody = funcBody;
+  }
+}
+
 class InvariantDeclaration : Declaration
 {
   Statements statements;
--- a/trunk/src/Parser.d	Sat Jul 14 20:56:05 2007 +0000
+++ b/trunk/src/Parser.d	Sun Jul 15 18:04:03 2007 +0000
@@ -109,6 +109,7 @@
          T.Const,
          T.Auto,
          T.Scope:
+         // TODO: T.Synchronized
     case_AttributeSpecifier:
       decl = parseAttributeSpecifier();
       break;
@@ -193,6 +194,17 @@
       nT();
       decl = new EmptyDeclaration();
       break;
+    // Declaration
+    case T.Identifier, T.Dot, T.Typeof:
+    // BasicType
+    case T.Char,   T.Wchar,   T.Dchar,  T.Bool,
+         T.Byte,   T.Ubyte,   T.Short,  T.Ushort,
+         T.Int,    T.Uint,    T.Long,   T.Ulong,
+         T.Float,  T.Double,  T.Real,
+         T.Ifloat, T.Idouble, T.Ireal,
+         T.Cfloat, T.Cdouble, T.Creal, T.Void:
+      decl = parseDeclaration();
+      break;
     case T.Module:
       // TODO: Error: module is optional and can appear only once at the top of the source file.
       break;
@@ -235,6 +247,70 @@
     return decls;
   }
 
+  Declaration parseDeclaration()
+  {
+    auto type = parseType();
+    string ident = requireIdentifier();
+
+    // Type FunctionName ( Parameters )
+    if (token.type == T.LParen)
+    {
+      // It's a function declaration
+      type = parseDeclaratorSuffix(type);
+      auto funcBody = new FunctionBody;
+      parseFunctionBody(funcBody);
+      return new FunctionDeclaration(ident, type, null, funcBody);
+    }
+
+    // It's a variable declaration.
+
+    return null;
+  }
+
+  void parseFunctionBody(FunctionBody func)
+  {
+    while (1)
+    {
+      switch (token.type)
+      {
+      case T.LBrace:
+        require(T.LBrace);
+        func.outBody = parseStatements();
+        require(T.RBrace);
+        break;
+      case T.Semicolon:
+        nT();
+        break;
+      case T.In:
+        //if (func.inBody)
+          // TODO: issue error msg.
+        require(T.LBrace);
+        func.inBody = parseStatements();
+        require(T.RBrace);
+        continue;
+      case T.Out:
+        //if (func.outBody)
+          // TODO: issue error msg.
+        nT();
+        if (token.type == T.LParen)
+        {
+          func.outIdent = requireIdentifier();
+          require(T.RParen);
+        }
+        require(T.LBrace);
+        func.outBody = parseStatements();
+        require(T.RBrace);
+        continue;
+      case T.Body:
+        nT();
+        goto case T.LBrace;
+      default:
+        // TODO: issue error msg.
+      }
+      break; // exit while loop
+    }
+  }
+
   Declaration parseAttributeSpecifier()
   {
     Declaration decl;
@@ -2485,12 +2561,12 @@
       }
       break;
     // BasicType . Identifier
-    case T.Void,   T.Char,    T.Wchar,  T.Dchar, T.Bool,
+    case T.Char,   T.Wchar,   T.Dchar,  T.Bool,
          T.Byte,   T.Ubyte,   T.Short,  T.Ushort,
          T.Int,    T.Uint,    T.Long,   T.Ulong,
          T.Float,  T.Double,  T.Real,
          T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal:
+         T.Cfloat, T.Cdouble, T.Creal, T.Void:
       auto type = new Type(token.type);
       requireNext(T.Dot);
       auto ident = requireIdentifier();
@@ -2555,12 +2631,12 @@
 
     switch (token.type)
     {
-    case T.Void,   T.Char,    T.Wchar,  T.Dchar, T.Bool,
+    case T.Char,   T.Wchar,   T.Dchar,  T.Bool,
          T.Byte,   T.Ubyte,   T.Short,  T.Ushort,
          T.Int,    T.Uint,    T.Long,   T.Ulong,
          T.Float,  T.Double,  T.Real,
          T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal:
+         T.Cfloat, T.Cdouble, T.Creal, T.Void:
       t = new Type(token.type);
       nT();
       break;
@@ -2630,24 +2706,22 @@
 
   Type parseDeclaratorSuffix(Type t)
   {
-    while (1)
+    switch (token.type)
     {
-      switch (token.type)
-      {
-      case T.LBracket:
-        // Type Identifier ArrayType
-        // ArrayType := [] or [Type] or [Expression..Expression]
+    case T.LBracket:
+      // Type Identifier ArrayType
+      // ArrayType := [] or [Type] or [Expression..Expression]
+      do
         t = parseArrayType(t);
-        continue;
-      case T.LParen:
-        auto params = parseParameterList();
-        // TODO: handle ( TemplateParameterList ) ( ParameterList )
-        // ReturnType FunctionName ( ParameterList )
-        t = new FunctionType(t, params);
-        break;
-      default:
-        break;
-      }
+      while (token.type == T.LBracket)
+      break;
+    case T.LParen:
+      auto params = parseParameterList();
+      // TODO: handle ( TemplateParameterList ) ( ParameterList )
+      // ReturnType FunctionName ( ParameterList )
+      t = new FunctionType(t, params);
+      break;
+    default:
       break;
     }
     return t;
--- a/trunk/src/Statements.d	Sat Jul 14 20:56:05 2007 +0000
+++ b/trunk/src/Statements.d	Sun Jul 15 18:04:03 2007 +0000
@@ -22,6 +22,12 @@
   }
 }
 
+class FunctionBody
+{
+  Statement funcBody, inBody, outBody;
+  string outIdent;
+}
+
 class ScopeStatement : Statement
 {
   Statement s;