Adding feature for enum and struct generations
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 3dbc21f297ecad90da6a589d3945aadcab9d5c2b..f3a1ce13398aea7bc5e886ae2d3ae9eadbd36eeb 100644 (file)
@@ -24,6 +24,8 @@ import iotpolicy.tree.DeclarationHandler;
 import iotpolicy.tree.CapabilityDecl;
 import iotpolicy.tree.InterfaceDecl;
 import iotpolicy.tree.RequiresDecl;
+import iotpolicy.tree.EnumDecl;
+import iotpolicy.tree.StructDecl;
 
 import iotrmi.Java.IoTRMITypes;
 
@@ -107,15 +109,26 @@ public class IoTCompiler {
                ParseTreeHandler ptHandler = new ParseTreeHandler(origInt, pnPol, pnReq);
                DeclarationHandler decHandler = new DeclarationHandler();
                // Process ParseNode and generate Declaration objects
+               // Interface
                ptHandler.processInterfaceDecl();
                InterfaceDecl intDecl = ptHandler.getInterfaceDecl();
                decHandler.addInterfaceDecl(origInt, intDecl);
+               // Capabilities
                ptHandler.processCapabilityDecl();
                CapabilityDecl capDecl = ptHandler.getCapabilityDecl();
                decHandler.addCapabilityDecl(origInt, capDecl);
+               // Requires
                ptHandler.processRequiresDecl();
                RequiresDecl reqDecl = ptHandler.getRequiresDecl();
                decHandler.addRequiresDecl(origInt, reqDecl);
+               // Enumeration
+               ptHandler.processEnumDecl();
+               EnumDecl enumDecl = ptHandler.getEnumDecl();
+               decHandler.addEnumDecl(origInt, enumDecl);
+               // Struct
+               ptHandler.processStructDecl();
+               StructDecl structDecl = ptHandler.getStructDecl();
+               decHandler.addStructDecl(origInt, structDecl);
 
                mapIntfacePTH.put(origInt, ptHandler);
                mapIntDeclHand.put(origInt, decHandler);
@@ -193,6 +206,55 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeEnumJava() writes the enumeration declaration
+        */
+       private void writeEnumJava(EnumDecl enumDecl) {
+
+               Set<String> enumTypes = enumDecl.getEnumDeclarations();
+               // Iterate over enum declarations
+               for (String enType : enumTypes) {
+
+                       println("public enum " + enType + " {");
+                       List<String> enumMembers = enumDecl.getMembers(enType);
+                       for (int i = 0; i < enumMembers.size(); i++) {
+
+                               String member = enumMembers.get(i);
+                               print(member);
+                               // Check if this is the last element (don't print a comma)
+                               if (i != enumMembers.size() - 1)
+                                       println(",");
+                               else
+                                       println("");
+                       }
+                       println("}\n");
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructJava() writes the struct declaration
+        */
+       private void writeStructJava(StructDecl structDecl) {
+
+               List<String> structTypes = structDecl.getStructTypes();
+               // Iterate over enum declarations
+               for (String stType : structTypes) {
+
+                       println("public class " + stType + " {");
+                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                       List<String> structMembers = structDecl.getMembers(stType);
+                       for (int i = 0; i < structMembers.size(); i++) {
+
+                               String memberType = structMemberTypes.get(i);
+                               String member = structMembers.get(i);
+                               println("public static " + memberType + " " + member + ";");
+                       }
+                       println("}\n");
+               }
+       }
+
+
        /**
         * generateJavaLocalInterface() writes the local interface and provides type-checking.
         * <p>
@@ -218,6 +280,12 @@ public class IoTCompiler {
                        // Write interface header
                        println("");
                        println("public interface " + intface + " {");
+                       // Write enum if any...
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       writeEnumJava(enumDecl);
+                       // Write struct if any...
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       writeStructJava(structDecl);
                        // Write methods
                        writeMethodJavaInterface(methods, intDecl);
                        println("}");
@@ -250,7 +318,7 @@ public class IoTCompiler {
                                printImportStatements(importClasses);
                                // Write interface header
                                println("");
-                               println("public interface " + newIntface + " {");
+                               println("public interface " + newIntface + " {\n");
                                // Write methods
                                writeMethodJavaInterface(intMeth.getValue(), intDecl);
                                println("}");
@@ -671,6 +739,57 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeEnumCplus() writes the enumeration declaration
+        */
+       private void writeEnumCplus(EnumDecl enumDecl) {
+
+               Set<String> enumTypes = enumDecl.getEnumDeclarations();
+               // Iterate over enum declarations
+               for (String enType : enumTypes) {
+
+                       println("enum " + enType + " {");
+                       List<String> enumMembers = enumDecl.getMembers(enType);
+                       for (int i = 0; i < enumMembers.size(); i++) {
+
+                               String member = enumMembers.get(i);
+                               print(member);
+                               // Check if this is the last element (don't print a comma)
+                               if (i != enumMembers.size() - 1)
+                                       println(",");
+                               else
+                                       println("");
+                       }
+                       println("};\n");
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructCplus() writes the struct declaration
+        */
+       private void writeStructCplus(StructDecl structDecl) {
+
+               List<String> structTypes = structDecl.getStructTypes();
+               // Iterate over enum declarations
+               for (String stType : structTypes) {
+
+                       println("struct " + stType + " {");
+                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                       List<String> structMembers = structDecl.getMembers(stType);
+                       for (int i = 0; i < structMembers.size(); i++) {
+
+                               String memberType = structMemberTypes.get(i);
+                               String member = structMembers.get(i);
+                               String structTypeC = checkAndGetCplusType(memberType);
+                               String structComplete = checkAndGetCplusArray(structTypeC, member);
+                               println(structComplete + ";");
+                       }
+                       println("};\n");
+               }
+       }
+
+
        /**
         * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking.
         * <p>
@@ -698,6 +817,12 @@ public class IoTCompiler {
                        Set<String> includeClasses = getIncludeClasses(methods, intDecl);
                        printIncludeStatements(includeClasses); println("");
                        println("using namespace std;\n");
+                       // Write enum if any...
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       writeEnumCplus(enumDecl);
+                       // Write struct if any...
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       writeStructCplus(structDecl);
                        println("class " + intface); println("{");
                        println("public:");
                        // Write methods