1 6 7 11 12 package org.openlaszlo.sc; 13 import java.io.*; 14 15 public class Values { 16 20 public static class PushTypes { 21 public static final byte String = 0; 22 public static final byte Float = 1; 23 public static final byte Null = 2; 24 public static final byte Undefined = 3; 25 public static final byte Register = 4; 26 public static final byte Boolean = 5; 27 public static final byte Integer = 7; 28 public static final byte Double = 6; 29 public static final byte CONSTANT_INDEX8 = 8; 30 public static final byte CONSTANT_INDEX16 = 9; 31 } 32 33 public static Value r0 = Value.make("r:0", PushTypes.Register, (byte)0); 35 public static Value r1 = Value.make("r:1", PushTypes.Register, (byte)1); 36 public static Value r2 = Value.make("r:2", PushTypes.Register, (byte)2); 37 public static Value r3 = Value.make("r:3", PushTypes.Register, (byte)3); 38 39 public static Value Register(int n) { 40 switch (n) { 41 case 0: 42 return r0; 43 case 1: 44 return r1; 45 case 2: 46 return r2; 47 case 3: 48 return r3; 49 default: 50 return Value.make("r:" + n, PushTypes.Register, (byte)n); 51 } 52 } 53 54 public static boolean isRegister(Object v) { 55 return v instanceof Value && ((Value)v).type == PushTypes.Register; 56 } 57 58 public static boolean isInteger(Object v) { 59 return v instanceof Number && ((Number )v).intValue() == ((Number )v).doubleValue(); 60 } 61 62 65 66 public static class Value implements Serializable { 67 public String name; 68 public byte type; 69 70 public static Value make(String name, byte type) { 71 return new Value(name, type); 72 } 73 74 public static Value make(String name, byte type, byte value) { 75 return new ParameterizedValue(name, type, value); 76 } 77 78 private Value(String name, byte type) { 79 this.name = name.intern(); 80 this.type = type; 81 } 82 83 public String toString() { 84 return this.name; 85 } 86 87 public Object readResolve() throws ObjectStreamException { 88 switch (this.type) { 89 case PushTypes.Undefined: 90 return Values.Undefined; 91 case PushTypes.Null: 92 return Values.Null; 93 default: 94 return this; 95 } 96 } 97 } 98 99 public static class ParameterizedValue extends Value { 100 public byte value; 101 102 private ParameterizedValue(String name, byte type, byte value) { 103 super(name, type); 104 this.value = value; 105 } 106 107 public Object readResolve() throws ObjectStreamException { 108 switch (this.type) { 109 case PushTypes.Boolean: 110 return this.value == 0 ? Values.False : Values.True; 111 case PushTypes.Register: 112 return Values.Register(this.value); 113 default: 114 return super.readResolve(); 115 } 116 } 117 } 118 119 public static Value True = Value.make("TRUE", PushTypes.Boolean, (byte)1); 120 public static Value False = Value.make("FALSE", PushTypes.Boolean, (byte)0); 121 public static Value Undefined = Value.make("UNDEF", PushTypes.Undefined); 122 public static Value Null = Value.make("NULL", PushTypes.Null); 123 124 126 } 128 129 | Popular Tags |