checking in changes
[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     Object value;
12     int type;
13     
14     public LiteralNode(String type, Object o) {
15         this.type=parseType(type);
16         value=o;
17     }
18
19     public Object getValue() {
20         return value;
21     }
22
23     private static int parseType(String type) {
24         if (type.equals("integer"))
25             return INTEGER;
26         else if (type.equals("float"))
27             return FLOAT;
28         else if (type.equals("boolean"))
29             return BOOLEAN;
30         else if (type.equals("char"))
31             return CHAR;
32         else if (type.equals("string"))
33             return STRING;
34         else if (type.equals("null"))
35             return NULL;
36         else throw new Error();
37     }
38
39     private String getStringType() {
40         if (type==INTEGER)
41             return "integer";
42         else if (type==FLOAT)
43             return "float";     
44         else if (type==BOOLEAN)
45             return "boolean";
46         else if (type==CHAR)
47             return "char";
48         else if (type==STRING)
49             return "string";
50         else if (type==NULL)
51             return "null";
52         else throw new Error();
53
54     }
55
56     public String printNode(int indent) {
57         if (type==NULL)
58             return "null";
59         if (type==STRING) {
60             return '"'+escapeString(value.toString())+'"';
61         }
62         return "/*"+getType()+ "*/"+value.toString();
63     }
64     private static String escapeString(String st) {
65         String new_st="";
66         for(int i=0;i<st.length();i++) {
67             char x=st.charAt(i);
68             if (x=='\n')
69                 new_st+="\\n";
70             else if (x=='"')
71                 new_st+="'"+'"'+"'";
72             else new_st+=x;
73         }
74         return new_st;
75     }
76     public int kind() {
77         return Kind.LiteralNode;
78     }
79 }