📄 functionmodule.java
字号:
/*
* MC2 -- j2me spreadsheet
*
* Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.wapindustrial.calc;
import java.io.UnsupportedEncodingException;
public abstract class FunctionModule extends FunctionObject {
public abstract void initializeNames();
//#ifdef JAVA_COMPILER
/** to separate run-time variables at compilation stage - name indexes */
protected int startNamesCount;
/** to separate run-time variables at compilation stage - name bytes indexes */
protected int startNamesArrayCount;
protected abstract String getModuleClassName();
//#endif
public ModuleName table[];
protected byte namesArray[];
protected int namesArrayCount;
protected int namesCount;
/* (non-Javadoc)
* @see com.wapindustrial.calc.FunctionObject#evaluate(com.wapindustrial.calc.FunctorList)
*/
public LispObject evaluate(FunctorList sexp) throws EvaluateException {
ModuleName nm;
try {
nm = (ModuleName) sexp.functor;
}
catch(ClassCastException e) {
throw new EvaluateException("cannot evaluate - not a functor",sexp);
}
try {
return evaluate(nm, sexp);
}
catch(ClassCastException e) {
throw new EvaluateException("wrong argument type",sexp);
}
}
/**
* @param nm
* @param sexp
* @return
*/
public LispObject evaluate(ModuleName modulename, FunctorList sexp) throws EvaluateException {
return NIL;
}
/**
* Adds a name to namesArray[] as bytes in UTF-8.
* Soesn't checks if the name already is in the table
* @param name
* name to add
* @return
* index for using in NameObjectBase constructor
* (current position in namesArray - namesArrayCount)
*/
protected int addNameToArray( String name ) {
int index = namesArrayCount;
byte value[] = getBytesUTF8(name);
namesArray[namesArrayCount++] = (byte)value.length;
for(int i=0; i<value.length; i++)
namesArray[namesArrayCount++] = value[i];
return index;
}
// finds if there's such Name in the table
// null if not found
// Hashtable could be used to speed up the search
final ModuleName findName( String name ) {
int len = name.length();
byte value[] = getBytesUTF8(name);
for(int i=0; i<namesCount; i++) {
ModuleName nm = table[i];
if( nm == null )
continue;
if(namesArray[nm.offset] == len) {
for(int j=0;j<len;j++) {
if(namesArray[nm.offset+j+1]==value[j]) {
if(j==len-1) {
return nm;
}
// continue
}
else {
break;
}
}
}
}
return null;
}
/**
* Finds name in namesArray
* @param name
* @return
* offset of name in namesArray, -1 if not found
*/
public int findNameInArray( String name ) {
byte value[] = {};
value = getBytesUTF8(name);
int len = value.length;
int nn = 0;
// FIXME finding only names but constants (now constants are considered as names)
while(nn<namesArrayCount) {
int ll = namesArray[nn];
if(len==ll) {
// empty string duplicate
if(ll==0)
return nn;
for(int i=0;i<len;i++) {
if(namesArray[nn+i+1]==value[i]) {
if(i==len-1) {
return nn;
}
// continue
}
else {
break;
}
}
}
nn += ll+1;
}
return -1;
}
protected ModuleName addDataName( String name ) {
ModuleName nm = new ModuleNameData(addNameToArray(name),NIL);
table[namesCount++] = nm;
return nm;
}
protected ModuleName addFunctionName( String name ) {
ModuleName nm = new ModuleName(addNameToArray(name));
table[namesCount++] = nm;
return nm;
}
protected ModuleStringConstant addStringConstant( String value ) {
int offset = findNameInArray(value);
if(offset<0)
offset = addNameToArray(value);
return new ModuleStringConstant(offset);
}
protected class ModuleName extends NameObjectBase {
public final short offset; // offset in byte array of names (for system object) or index in table for user names
public ModuleName( int offset ) {
this.offset = (short)offset;
}
// to use NameObject as a key in HashTable
private ModuleName( String value ) {
offset = (short)namesArrayCount;
byte valueBytes[] = getBytesUTF8(value);
namesArray[namesArrayCount++] = (byte)(valueBytes.length+1);
for(int i=0; i<valueBytes.length; i++)
namesArray[namesArrayCount++] = valueBytes[i];
}
public String getName() {
return getStringUTF8(namesArray,offset+1,namesArray[offset]);
}
/**
* Returns the object this name maps to
*
* @return The object this name maps to. NIL if the name is unmapped
*/
public LispObject getMappedObject() {
return FunctionModule.this;
}
/**
* Maps the name to an object
* @param mapTo object to map to
* @throws EvaluateException if the name is immutable, and cannot be changed (for a system name)
*/
public LispObject setMappedObject(LispObject mapTo) throws EvaluateException {
throw new EvaluateException("name of this type cannot be set");
}
//#ifdef JAVA_COMPILER
public void toJavaBuffer( StringBuffer sb, int ident, FunctorList parent ) {
addIdent( sb, ident );
String instanceName = getModuleClassName();
if(instanceName!=null) {
if(instanceName.compareTo("Bfunc")==0 || instanceName.compareTo("ScreenLite")==0 || instanceName.compareTo("MathModule")==0)
sb.append( instanceName.toUpperCase()+"_table[" + instanceName+".INDEX_"+getNameLabel() +"]" );
else
sb.append( instanceName + '.' +instanceName.toUpperCase()+".table[" + instanceName+".INDEX_"+getNameLabel() +"]" );
}
else
sb.append( "table[INDEX_"+getNameLabel() +"]" );
}
//#endif
}
protected class ModuleNameData extends ModuleName {
private LispObject mapTo = NIL;
public ModuleNameData(int offset, LispObject mapTo) {
super(offset);
this.mapTo = mapTo;
}
public ModuleNameData(int offset) {
super(offset);
}
public LispObject getMappedObject() {
return mapTo;
}
/* (non-Javadoc)
* @see com.wapindustrial.calc.NameObjectBase#setMappedObject(com.wapindustrial.calc.LispObject)
*/
public LispObject setMappedObject(LispObject mapTo) throws EvaluateException {
LispObject oldmapping = mapTo;
this.mapTo = mapTo;
return oldmapping;
}
}
protected class ModuleStringConstant extends StringAtomBase {
private final short offset;
public ModuleStringConstant(int offset) {
this.offset = (short)offset;
}
/* (non-Javadoc)
* @see com.wapindustrial.calc.StringAtomBase#getValue()
*/
public String getValue() {
return getStringUTF8(namesArray,offset+1,namesArray[offset]);
}
//#ifdef JAVA_COMPILER
public void toJavaBuffer( StringBuffer sb, int ident, FunctorList parent ) {
addIdent( sb, ident );
sb.append( "new ModuleStringConstant(" + (offset-startNamesArrayCount) +")" );
}
//#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -