001 package AST; 002 003 import java.util.HashSet; 004 import java.io.File; 005 import java.util.*; 006 import beaver.*; 007 import java.util.ArrayList; 008 import java.util.zip.*; 009 import java.io.*; 010 import java.io.FileNotFoundException; 011 import java.util.Collection; 012 /** 013 * @ast class 014 * 015 */ 016 public class Constant extends java.lang.Object { 017 018 static class ConstantInt extends Constant { 019 private int value; 020 public ConstantInt(int i) { this.value = i; } 021 int intValue() { return value; } 022 long longValue() { return value; } 023 float floatValue() { return value; } 024 double doubleValue() { return value; } 025 String stringValue() { return new Integer(value).toString(); } 026 } 027 028 029 static class ConstantLong extends Constant { 030 private long value; 031 public ConstantLong(long l) { this.value = l; } 032 int intValue() { return (int)value; } 033 long longValue() { return value; } 034 float floatValue() { return value; } 035 double doubleValue() { return value; } 036 String stringValue() { return new Long(value).toString(); } 037 } 038 039 040 static class ConstantFloat extends Constant { 041 private float value; 042 public ConstantFloat(float f) { this.value = f; } 043 int intValue() { return (int)value; } 044 long longValue() { return (long)value; } 045 float floatValue() { return value; } 046 double doubleValue() { return value; } 047 String stringValue() { return new Float(value).toString(); } 048 } 049 050 051 static class ConstantDouble extends Constant { 052 private double value; 053 public ConstantDouble(double d) { this.value = d; } 054 int intValue() { return (int)value; } 055 long longValue() { return (long)value; } 056 float floatValue() { return (float)value; } 057 double doubleValue() { return value; } 058 String stringValue() { return new Double(value).toString(); } 059 } 060 061 062 static class ConstantChar extends Constant { 063 private char value; 064 public ConstantChar(char c) { this.value = c; } 065 int intValue() { return value; } 066 long longValue() { return value; } 067 float floatValue() { return value; } 068 double doubleValue() { return value; } 069 String stringValue() { return new Character(value).toString(); } 070 } 071 072 073 static class ConstantBoolean extends Constant { 074 private boolean value; 075 public ConstantBoolean(boolean b) { this.value = b; } 076 boolean booleanValue() { return value; } 077 String stringValue() { return new Boolean(value).toString(); } 078 } 079 080 081 static class ConstantString extends Constant { 082 private String value; 083 public ConstantString(String s) { this.value = s; } 084 String stringValue() { return value; } 085 } 086 087 088 089 int intValue() { throw new UnsupportedOperationException(); } 090 091 092 long longValue() { throw new UnsupportedOperationException(); } 093 094 095 float floatValue() { throw new UnsupportedOperationException(); } 096 097 098 double doubleValue() { throw new UnsupportedOperationException(); } 099 100 101 boolean booleanValue() { throw new UnsupportedOperationException(getClass().getName()); } 102 103 104 String stringValue() { throw new UnsupportedOperationException(); } 105 106 107 108 protected Constant() { 109 } 110 111 112 113 public boolean error = false; 114 115 116 117 static Constant create(int i) { return new ConstantInt(i); } 118 119 120 static Constant create(long l) { return new ConstantLong(l); } 121 122 123 static Constant create(float f) { return new ConstantFloat(f); } 124 125 126 static Constant create(double d) { return new ConstantDouble(d); } 127 128 129 static Constant create(boolean b) { return new ConstantBoolean(b); } 130 131 132 static Constant create(char c) { return new ConstantChar(c); } 133 134 135 static Constant create(String s) { return new ConstantString(s); } 136 137 public void createBCode(CodeGeneration gen) { 138 if(this instanceof ConstantInt) 139 IntegerLiteral.push(gen, intValue()); 140 else if(this instanceof ConstantLong) 141 LongLiteral.push(gen, longValue()); 142 else if(this instanceof ConstantFloat) 143 FloatingPointLiteral.push(gen, floatValue()); 144 else if(this instanceof ConstantDouble) 145 DoubleLiteral.push(gen, doubleValue()); 146 else if(this instanceof ConstantChar) 147 IntegerLiteral.push(gen, intValue()); 148 else if(this instanceof ConstantBoolean) 149 BooleanLiteral.push(gen, booleanValue()); 150 else if(this instanceof ConstantString) 151 StringLiteral.push(gen, stringValue()); 152 } 153 154 155 }