95c778e6b1315acc6b9832fe59e4c6145dbc8c5c
[charm.git] / src / langs / charj / src / charj / translator / PupRoutineCreator.java
1 package charj.translator;
2
3 import org.antlr.runtime.tree.CommonTree;
4 import org.antlr.runtime.Token;
5 import org.antlr.runtime.CommonToken;
6
7 class PupRoutineCreator
8 {
9     private CharjAST pupNode;
10     private CharjAST block;
11
12     PupRoutineCreator()
13     {
14         createPupNode();
15         block = pupNode.getChild(4);
16     }
17
18     protected CharjAST getPupRoutineNode()
19     {
20         return pupNode;
21     }
22
23     private CharjAST createNode(int type, String text)
24     {
25         return new CharjAST(new CommonToken(type, text));
26     }    
27
28     private void createPupNode()
29     {
30         pupNode = createNode(CharjParser.FUNCTION_METHOD_DECL, "FUNCTION_METHOD_DECL");
31
32         pupNode.addChild(createNode(CharjParser.MODIFIER_LIST, "MODIFIER_LIST"));
33         pupNode.addChild(createNode(CharjParser.VOID, "void"));
34         pupNode.addChild(createNode(CharjParser.IDENT, "pup"));
35         pupNode.addChild(createNode(CharjParser.FORMAL_PARAM_LIST, "FORMAL_PARAM_LIST"));
36         pupNode.addChild(createNode(CharjParser.BLOCK, "BLOCK"));
37
38         pupNode.getChild(0).addChild(createNode(CharjParser.PUBLIC, "public"));
39
40         pupNode.getChild(3).addChild(createNode(CharjParser.FORMAL_PARAM_STD_DECL, "FORMAL_PARAM_STD_DECL"));
41         pupNode.getChild(3).getChild(0).addChild(createNode(CharjParser.REFERENCE_TYPE, "REFERENCE_TYPE"));
42         pupNode.getChild(3).getChild(0).getChild(0).addChild(createNode(CharjParser.QUALIFIED_TYPE_IDENT, "QUALIFIED_TYPE_IDENT"));
43         pupNode.getChild(3).getChild(0).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "PUP::er"));
44
45         pupNode.getChild(3).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
46     }
47
48     protected CharjAST getEnclosingType(CharjAST varDeclNode)
49     {
50         for(CharjAST p = varDeclNode.getParent(); p != null; p = p.getParent())
51             if(p.getType() == CharjParser.TYPE)
52                 return p;
53         return null;
54     }         
55
56     protected void varPup(CharjAST idNode)
57     {
58         boolean primitive = false;
59
60         for(CharjAST p = idNode.getParent(); p != null; p = p.getParent())
61             if(p.getType() == CharjParser.PRIMITIVE_VAR_DECLARATION)
62                 primitive = true;
63             else if(p.getType() == CharjParser.FUNCTION_METHOD_DECL)
64                 break;
65             else if(p.getType() == CharjParser.TYPE)
66             {
67                 if(primitive)
68                     primitiveVarPup(idNode);
69                 else
70                     objectVarPup(idNode);
71                 break;
72             }
73     }
74
75     protected void primitiveVarPup(CharjAST idNode)
76     {
77         pupNode.getChild(4).addChild(createNode(CharjParser.EXPR, "EXPR"));
78         pupNode.getChild(4).getChild(0).addChild(createNode(CharjParser.BITWISE_OR, "|"));
79         pupNode.getChild(4).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
80         pupNode.getChild(4).getChild(0).getChild(0).addChild(idNode.dupNode());
81     }
82     
83     protected void objectVarPup(CharjAST varDeclNode){}
84 }