4f273fde4467a6f4252beee4f2e592ed53979e95
[IRC.git] / Robust / src / IR / Tree / LiteralNode.java
1 package IR.Tree;
2
3 public class LiteralNode extends ExpressionNode {
4     public final static int INTEGER=1;
5     public final static int FLOAT=2;
6     public final static int BOOLEAN=3;
7     public final static int CHAR=4;
8     public final static int STRING=5;
9     public final static int NULL=6;
10
11
12     Object value;
13     int type;
14     
15     public LiteralNode(String type, Object o) {
16         this.type=parseType(type);
17         value=o;
18     }
19
20     private static int parseType(String type) {
21         if (type.equals("integer"))
22             return INTEGER;
23         else if (type.equals("float"))
24             return FLOAT;
25         else if (type.equals("boolean"))
26             return BOOLEAN;
27         else if (type.equals("char"))
28             return CHAR;
29         else if (type.equals("string"))
30             return STRING;
31         else if (type.equals("null"))
32             return NULL;
33         else throw new Error();
34     }
35
36     private String getType() {
37         if (type==INTEGER)
38             return "integer";
39         else if (type==FLOAT)
40             return "float";     
41         else if (type==BOOLEAN)
42             return "boolean";
43         else if (type==CHAR)
44             return "char";
45         else if (type==STRING)
46             return "string";
47         else if (type==NULL)
48             return "null";
49         else throw new Error();
50
51     }
52
53     public String printNode(int indent) {
54         if (type==NULL)
55             return "null";
56         if (type==STRING) {
57             return '"'+escapeString(value.toString())+'"';
58         }
59         return "/*"+getType()+ "*/"+value.toString();
60     }
61     private static String escapeString(String st) {
62         String new_st="";
63         for(int i=0;i<st.length();i++) {
64             char x=st.charAt(i);
65             if (x=='\n')
66                 new_st+="\\n";
67             else if (x=='"')
68                 new_st+="'"+'"'+"'";
69             else new_st+=x;
70         }
71         return new_st;
72     }
73     public int kind() {
74         return Kind.LiteralNode;
75     }
76 }