compiler/libec2: Some classes and methods parsing
authorJerome St-Louis <jerome@ecere.com>
Mon, 8 Jul 2013 00:58:12 +0000 (20:58 -0400)
committerJerome St-Louis <jerome@ecere.com>
Mon, 21 Nov 2016 14:19:01 +0000 (09:19 -0500)
compiler/libec2/src/astNode.ec
compiler/libec2/src/classes.ec
compiler/libec2/src/declarators.ec
compiler/libec2/src/externals.ec
compiler/libec2/src/lexing.ec
compiler/libec2/src/specifiers.ec
compiler/libec2/src/statements.ec
compiler/libec2/test.ec

index efaf934..53747fb 100644 (file)
@@ -59,9 +59,9 @@ public:
             if(!list) list = eInstance_New(c);
             list.Add(e);
          }
-         if(peekToken().type == sep)
+         if(sep && peekToken().type == sep)
             readToken();
-         else
+         else if(sep || peekToken().type == '}')
             break;
       }
       return list;
index 19ccc89..3e8f263 100644 (file)
@@ -1,27 +1,6 @@
-/*
-public class ClassFunction : struct
-{
-public:
-   ClassFunction prev, next;
-   Location loc;
-   OldList * specifiers;
-   ASTDeclarator declarator;
-   OldList * declarations;
-   Statement body;
-   Class _class;
-   OldList attached;    // For IDE
-   AccessMode declMode;
-   
-   // COMPILING DATA
-   Type type;
-   Symbol propSet;
-
-   bool isVirtual;
-   bool isConstructor, isDestructor;
-   bool dontMangle;
-   int id, idCode;
-};
+import "externals"
 
+/*
 public class MembersInit : struct
 {
 public:
@@ -51,16 +30,15 @@ public:
    bool variable;
    bool takeOutExp;
 };
+*/
 
-public class ClassDefinition : struct
+/*
+public class ASTClassDefinition : struct
 {
 public:
-   ClassDefinition prev, next;
-   Location loc;
-   Specifier _class;
-   // Specifier base;
-   OldList * baseSpecs;
-   OldList * definitions;
+   ASTSpecifier _class;
+   SpecsList baseSpecs;
+   SpecsList definitions;
    Symbol symbol;
    Location blockStart;
    Location nameLoc;
@@ -87,31 +65,119 @@ public:
    bool isWatchable;
    Expression category;
 };
+*/
 
-public class ClassDef : struct
+public class ClassDefList : ASTList<ASTClassDef>
 {
-public:
-   ClassDef prev, next;
-   Location loc;
-   ClassDefType type;
-   union
+   ClassDefList ::parse()
    {
-      Declaration decl;
-      ClassFunction function;
-      OldList * defProperties;
-      PropertyDef propertyDef;
-      ASTPropertyWatch propertyWatch;
-      char * designer;
-      Identifier defaultProperty;
-      struct
-      {
-         Identifier id;
-         Initializer initializer;
-      };
-   };
+      return (ClassDefList)ASTList::parse(class(ClassDefList), ASTClassDef::parse, 0);
+   }
+
+   void printSep()
+   {
+      PrintLn("");
+   }
+}
+
+public class ASTClassDef : ASTNode
+{
+public:
    AccessMode memberAccess;
 
    // IDE
    void * object;
+
+   ASTClassDef ::parse()
+   {
+      SpecsList specs = SpecsList::parse();
+      InitDeclList decls = InitDeclList::parse();
+
+      if(peekToken().type == '{')
+         return ClassDefFunction::parse(specs, decls);
+      else if(specs || decls)
+         return ClassDefDeclaration::parse(specs, decls);
+      else
+      {
+         readToken(); // Error
+         return null;
+      }
+   }
 };
+
+public class ClassDefClassPropertyValue : ASTClassDef
+{
+   ASTIdentifier id;
+   ASTInitializer initializer;
+}
+
+public class ClassDefDeclaration : ASTClassDef
+{
+   ASTDeclaration decl;
+
+   ClassDefDeclaration ::parse(SpecsList specs, InitDeclList decls)
+   {
+      ASTDeclaration decl = ASTDeclaration::parse(specs, decls);
+      if(decl)
+      {
+         return { decl = decl };
+      }
+      return null;
+   }
+
+   void print()
+   {
+      printIndent();
+      if(decl) decl.print();
+   }
+}
+
+public class ASTClassFunction : ASTFunctionDefinition
+{
+public:
+   Class _class;
+   //List attached;    // For IDE
+   AccessMode declMode;
+   
+   // COMPILING DATA
+   Type type;
+   Symbol propSet;
+
+   bool isVirtual;
+   bool isConstructor, isDestructor;
+   bool dontMangle;
+   // int id, idCode;
+
+   ASTClassFunction ::parse(SpecsList specs, InitDeclList decls)
+   {
+      return (ASTClassFunction)ASTFunctionDefinition::parse(specs, decls);
+   }
+};
+
+public class ClassDefFunction : ASTClassDef
+{
+   ASTClassFunction function;
+
+   ClassDefFunction ::parse(SpecsList specs, InitDeclList decls)
+   {
+      ASTClassFunction function = ASTClassFunction::parse(specs, decls);
+      if(function)
+      {
+         return { function = function };
+      }
+      return null;
+   }
+
+   void print()
+   {
+      if(function) function.print();
+   }
+}
+
+/*
+   List<ASTMemberInit> defProperties;
+   ASTPropertyDef propertyDef;
+   ASTPropertyWatch propertyWatch;
+   String designer;
+   ASTIdentifier defaultProperty;
 */
index a2ffad4..867c8ca 100644 (file)
@@ -165,14 +165,24 @@ public class DeclPointer : ASTDeclarator
    }
 }
 
+public class DeclStruct : ASTDeclarator
+{
+   ASTDeclarator declarator;
+   Expression exp;
+   Expression posExp;
+   Attrib attrib;
+
+   DeclStruct ::parse()
+   {
+      return { declarator = ASTDeclarator::parse(); };
+   }
+}
+
 /*
    union
    {
       struct
       {
-         Expression exp;
-         Expression posExp;
-         Attrib attrib;
       } structDecl;
       struct
       {
index e7c595f..f53f3f1 100644 (file)
@@ -42,6 +42,7 @@ public:
    void print()
    {
       PrintLn("");
+      printIndent();
       if(specifiers) 
       {
          for(s : specifiers)
@@ -61,7 +62,7 @@ public:
       ASTDeclarator decl = (decls && decls[0]) ? decls[0].declarator : null;
       if(decl && decls[0]) decls[0].declarator = null;
       delete decls;
-      function.specifiers = (void *)specs;         // TOFIX: #768
+      function.specifiers = specs;
       function.declarator = decl;
       function.body = StmtCompound::parse();
       return function;
index 2bed41c..cdaee79 100644 (file)
@@ -22,7 +22,7 @@ public enum TokenType
          return isQualifier || this == CHAR || this == SHORT || this == INT || this == UINT || this == INT64 || this == LONG || this == SIGNED ||
                 this == UNSIGNED || this == FLOAT || this == DOUBLE || this == VOID ||
                 this == VALIST || this == THISCLASS || this == TYPED_OBJECT || this == ANY_OBJECT ||
-                this == TYPEDEF || this == STRUCT || this == UNION || this == ENUM ||
+                this == TYPEDEF || this == STRUCT || this == CLASS || this == UNION || this == ENUM ||
                 this == TYPEOF || this == SUBCLASS;
       }
    }
@@ -86,6 +86,7 @@ public enum TokenType
             case STRUCT: Print("struct"); break;
             case UNION: Print("union"); break;
             case ENUM: Print("enum"); break;
+            case CLASS: Print("class"); break;
 
             case TYPEOF: Print("typeof"); break;
             case SUBCLASS: Print("subclass"); break;
index edb91c4..36a4b37 100644 (file)
@@ -1,26 +1,16 @@
 import "astNode"
+import "classes"
 
 // Specifiers
 public class ASTSpecifier : ASTNode
 {
 public:
-   SpecifierType type;
   /*    struct
       {
          //ExtDecl extDecl;
          //Symbol symbol;
          //OldList * templateArgs;
       };
-      struct
-      {
-         Identifier id;
-         OldList * list;
-         OldList * baseSpecs;
-         OldList * definitions;
-         bool addNameSpace;
-         Context ctx;
-         ExtDecl extDeclStruct;
-      };
       Expression expression;
       NewSpecifier _class;
       TemplateParameter templateParameter;
@@ -42,7 +32,17 @@ public class SpecsList : ASTList<ASTSpecifier>
       while(true)
       {
          peekToken();
-         if(nextToken.type.isSpecifier)
+         if(nextToken.type == STRUCT || nextToken.type == UNION)
+         {
+            ASTSpecifier s = SpecClass::parse();
+            if(s)
+            {
+               if(!specs) specs = { };
+               specs.Add(s);
+            }
+            break;
+         }
+         else if(nextToken.type.isSpecifier)
          {
             readToken();
             if(!specs) specs = { };
@@ -90,6 +90,56 @@ public class SpecName : ASTSpecifier
    }
 }
 
+public class ASTEnumerator : struct
+{
+public:
+   ASTIdentifier id;
+   ASTExpression exp;
+};
+
+public class SpecClass : ASTSpecifier
+{
+   TokenType type;
+   ASTIdentifier id;
+   List<ASTEnumerator> enumerators;
+   SpecsList baseSpecs;
+   ClassDefList definitions;
+   bool addNameSpace;
+   Context ctx;
+   // ExtDecl extDeclStruct;
+
+   SpecClass ::parse()
+   {
+      SpecClass spec { };
+      spec.type = readToken().type;
+      if(peekToken().type == IDENTIFIER)
+         spec.id = ASTIdentifier::parse();
+      if(peekToken().type == '{')
+      {
+         readToken();
+         spec.definitions = ClassDefList::parse();
+         if(peekToken().type == '}')
+            readToken();
+      }
+      return spec;
+   }
+
+   void print()
+   {
+      type.print();
+      Print(" ");
+      if(id) id.print();
+      if(definitions)
+      {
+         PrintLn("\n{");
+         indent++;
+         definitions.print();
+         indent--;
+         Print("\n}");
+      }
+   }
+}
+
 /*
 public class Attribute : struct
 {
@@ -120,14 +170,3 @@ public:
    };
 }
 */
-
-/*
-public class Enumerator : struct
-{
-public:
-   Enumerator prev, next;
-   Location loc;
-   Identifier id;
-   Expression exp;
-};
-*/
index a49911f..87c5a0b 100644 (file)
@@ -127,7 +127,7 @@ public class StmtExpression : ASTStatement
    }
 }
 int indent;
-void PrintIndent()
+void printIndent()
 {
    int i;
    for(i = 0; i < indent; i++)
@@ -145,13 +145,14 @@ public class StmtCompound : ASTStatement
 
    void print()
    {
+      printIndent();
       PrintLn("{");
       indent++;
       if(declarations)
       {
          for(d : declarations)
          {
-            PrintIndent();
+            printIndent();
             d.print();
             PrintLn("");
          }
@@ -165,7 +166,7 @@ public class StmtCompound : ASTStatement
             if(s._class == class(StmtCase))
                indent = caseIndent;
             if(s._class != class(StmtLabeled))
-               PrintIndent();
+               printIndent();
             s.print();
             if(s._class == class(StmtExpression)) 
                PrintLn("");
@@ -174,7 +175,7 @@ public class StmtCompound : ASTStatement
       indent--;
       if(indent == caseIndent)
          indent--;
-      PrintIndent();
+      printIndent();
       PrintLn("}");
    }
 
@@ -244,17 +245,17 @@ public class StmtIf : ASTStatement
       if(stmt)
       {
          if(stmt._class != class(StmtCompound)) indent++;
-         PrintIndent();
+         printIndent();
          stmt.print();
          if(stmt._class == class(StmtExpression)) PrintLn(""); 
          if(stmt._class != class(StmtCompound)) indent--;
       }
       if(elseStmt)
       {
-         PrintIndent();
+         printIndent();
          Print("else");
          if(elseStmt._class != class(StmtCompound)) { PrintLn(""); indent++; }
-         PrintIndent();
+         printIndent();
          if(elseStmt._class != class(StmtCompound)) elseStmt.print();
          if(elseStmt._class == class(StmtExpression)) PrintLn(""); 
          indent--;
@@ -297,7 +298,6 @@ public class StmtSwitch : ASTStatement
       if(stmt)
       {
          caseIndent = indent+1;
-         PrintIndent();
          stmt.print();
          indent = caseIndent-1;
       }
@@ -359,7 +359,7 @@ public class StmtCase : ASTStatement
       if(stmt)
       {
          if(stmt._class != class(StmtCompound)) indent++;
-         PrintIndent();
+         printIndent();
          stmt.print();
          if(stmt._class == class(StmtExpression)) PrintLn(""); 
       }
@@ -451,7 +451,7 @@ public class StmtFor : ASTStatement
       if(stmt)
       {
          if(stmt._class != class(StmtCompound)) indent++;
-         PrintIndent();
+         printIndent();
          stmt.print();
          if(stmt._class == class(StmtExpression)) PrintLn(""); 
          if(stmt._class != class(StmtCompound)) indent--;
@@ -594,6 +594,6 @@ public class StmtDecl : ASTStatement
 
    void print()
    {
-      if(decl) decl.print();
+      if(decl) { decl.print(); PrintLn(""); }
    }
 }
index ae90d9a..2eaa696 100644 (file)
@@ -1,5 +1,24 @@
+struct InventoryItem
+{
+   float price;
+   String name;
+
+   void Print()
+   {
+      int a;
+      PrintLn(a + 4);
+   }
+} item, * itemPtr;
+
+class SomeClass
+{
+
+}
+
 int a;
 
+typedef int bla;
+
 int SomeFunction(int * p)
 {
    int b[3][4] =
@@ -11,6 +30,7 @@ int SomeFunction(int * p)
    int c, d, e = 4;
    const String name = "Foo";
    b * a;
+   bla blo;
 
    b = ((a + 3) * a) - (3.1415 * 180 / a);
    a = b = 5*(2 + 10);