lubomir@319: /** lubomir@319: * Back 2 Browser Bytecode Translator lubomir@319: * Copyright (C) 2012 Jaroslav Tulach lubomir@319: * lubomir@319: * This program is free software: you can redistribute it and/or modify lubomir@319: * it under the terms of the GNU General Public License as published by lubomir@319: * the Free Software Foundation, version 2 of the License. lubomir@319: * lubomir@319: * This program is distributed in the hope that it will be useful, lubomir@319: * but WITHOUT ANY WARRANTY; without even the implied warranty of lubomir@319: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the lubomir@319: * GNU General Public License for more details. lubomir@319: * lubomir@319: * You should have received a copy of the GNU General Public License lubomir@319: * along with this program. Look for COPYING file in the top folder. lubomir@319: * If not, see http://opensource.org/licenses/GPL-2.0. lubomir@319: */ lubomir@319: package org.apidesign.vm4brwsr; lubomir@319: lubomir@319: final class Variable implements CharSequence { lubomir@319: private static final String STACK_VAR_PREFIX = "st"; lubomir@319: private static final String LOCAL_VAR_PREFIX = "lc"; lubomir@319: lubomir@319: private final String name; lubomir@319: private final int type; lubomir@319: private final int index; lubomir@319: lubomir@319: private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' }; lubomir@319: lubomir@319: private Variable(final String prefix, final int type, final int index) { lubomir@319: this.name = prefix + TYPE_IDS[type] + index; lubomir@319: this.type = type; lubomir@319: this.index = index; lubomir@319: } lubomir@319: lubomir@319: public static Variable getStackVariable( lubomir@319: final int type, final int index) { lubomir@319: // TODO: precreate frequently used variables lubomir@319: return new Variable(STACK_VAR_PREFIX, type, index); lubomir@319: } lubomir@319: lubomir@319: public static Variable getLocalVariable( lubomir@319: final int type, final int index) { lubomir@319: // TODO: precreate frequently used variables lubomir@319: return new Variable(LOCAL_VAR_PREFIX, type, index); lubomir@319: } lubomir@319: lubomir@319: public String getName() { lubomir@319: return name; lubomir@319: } lubomir@319: lubomir@319: public int getType() { lubomir@319: return type; lubomir@319: } lubomir@319: lubomir@319: public int getIndex() { lubomir@319: return index; lubomir@319: } lubomir@319: lubomir@319: public boolean isCategory2() { lubomir@319: return VarType.isCategory2(type); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public int length() { lubomir@319: return name.length(); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public char charAt(final int index) { lubomir@319: return name.charAt(index); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public CharSequence subSequence(final int start, final int end) { lubomir@319: return name.subSequence(start, end); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public int hashCode() { lubomir@319: return name.hashCode(); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public boolean equals(final Object other) { lubomir@319: if (this == other) { lubomir@319: return true; lubomir@319: } lubomir@319: if (!(other instanceof Variable)) { lubomir@319: return false; lubomir@319: } lubomir@319: lubomir@319: return name.equals(((Variable) other).name); lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public String toString() { lubomir@319: return name; lubomir@319: } lubomir@319: }