Charj : added basic support for automatic puping of pointer types
[charm.git] / src / langs / charj / src / charj / translator / PupRoutineCreator.java
index 95c778e6b1315acc6b9832fe59e4c6145dbc8c5c..fac6454a39fa9aecf76aa154f7200b72e9be1b78 100644 (file)
@@ -55,30 +55,66 @@ class PupRoutineCreator
 
     protected void varPup(CharjAST idNode)
     {
-        boolean primitive = false;
+        System.out.println("in var pup: " + idNode.getText());
+        int type = -1;
 
         for(CharjAST p = idNode.getParent(); p != null; p = p.getParent())
-            if(p.getType() == CharjParser.PRIMITIVE_VAR_DECLARATION)
-                primitive = true;
-            else if(p.getType() == CharjParser.FUNCTION_METHOD_DECL)
-                break;
-            else if(p.getType() == CharjParser.TYPE)
+        {
+            switch(p.getType())
             {
-                if(primitive)
-                    primitiveVarPup(idNode);
-                else
-                    objectVarPup(idNode);
-                break;
+                case CharjParser.PRIMITIVE_VAR_DECLARATION:
+                    System.out.println("got the type, it's primitive");
+                    type = p.getType();
+                    break;
+                case CharjParser.OBJECT_VAR_DECLARATION:
+                    System.out.println("got the type, it's an object!");
+                    type = p.getChild(0).getType();
+                    break;
+                case CharjParser.FUNCTION_METHOD_DECL:
+                    System.out.println("local var, not puping...");
+                    return;
+                case CharjParser.TYPE:
+                    System.out.println("class member var, puping... type " + type);
+                    switch(type)
+                    {
+                        case CharjParser.REFERENCE_TYPE: System.out.println("found ref");
+                        case CharjParser.PRIMITIVE_VAR_DECLARATION: System.out.println("found simple or ref");
+                            primitiveVarPup(idNode);
+                            break;
+                        case CharjParser.POINTER_TYPE: System.out.println("found pointer");
+                            pointerVarPup(idNode);
+                            break;
+                    }
+                    return;
             }
+        }
+        System.out.println("after for");
     }
 
     protected void primitiveVarPup(CharjAST idNode)
     {
         pupNode.getChild(4).addChild(createNode(CharjParser.EXPR, "EXPR"));
-        pupNode.getChild(4).getChild(0).addChild(createNode(CharjParser.BITWISE_OR, "|"));
-        pupNode.getChild(4).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
-        pupNode.getChild(4).getChild(0).getChild(0).addChild(idNode.dupNode());
+        
+        int index = pupNode.getChild(4).getChildren().size() - 1;
+
+        pupNode.getChild(4).getChild(index).addChild(createNode(CharjParser.BITWISE_OR, "|"));
+        pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
+        pupNode.getChild(4).getChild(index).getChild(0).addChild(idNode.dupNode());
     }
     
-    protected void objectVarPup(CharjAST varDeclNode){}
+    protected void pointerVarPup(CharjAST idNode)
+    {
+        pupNode.getChild(4).addChild(createNode(CharjParser.EXPR, "EXPR"));
+        
+        int index = pupNode.getChild(4).getChildren().size() - 1;
+
+        pupNode.getChild(4).getChild(index).addChild(createNode(CharjParser.METHOD_CALL, "METHOD_CALL"));
+        pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.DOT, "."));
+        pupNode.getChild(4).getChild(index).getChild(0).getChild(0).addChild(idNode.dupNode());
+        pupNode.getChild(4).getChild(index).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "pup"));
+        pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.ARGUMENT_LIST, "ARGUMENT_LIST"));
+        pupNode.getChild(4).getChild(index).getChild(0).getChild(1).addChild(createNode(CharjParser.EXPR, "EXPR"));
+        pupNode.getChild(4).getChild(index).getChild(0).getChild(1).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
+    }
+
 }