2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 final class Variable implements CharSequence {
21 private static final String STACK_VAR_PREFIX = "st";
22 private static final String LOCAL_VAR_PREFIX = "lc";
24 private final String name;
25 private final int type;
26 private final int index;
28 private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
30 private Variable(final String prefix, final int type, final int index) {
31 this.name = prefix + TYPE_IDS[type] + index;
36 public static Variable getStackVariable(
37 final int type, final int index) {
38 // TODO: precreate frequently used variables
39 return new Variable(STACK_VAR_PREFIX, type, index);
42 public static Variable getLocalVariable(
43 final int type, final int index) {
44 // TODO: precreate frequently used variables
45 return new Variable(LOCAL_VAR_PREFIX, type, index);
48 public String getName() {
52 public int getType() {
56 public int getIndex() {
60 public boolean isCategory2() {
61 return VarType.isCategory2(type);
70 public char charAt(final int index) {
71 return name.charAt(index);
75 public CharSequence subSequence(final int start, final int end) {
76 return name.subSequence(start, end);
80 public int hashCode() {
81 return name.hashCode();
85 public boolean equals(final Object other) {
89 if (!(other instanceof Variable)) {
93 return name.equals(((Variable) other).name);
97 public String toString() {