📄 userfunction.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;
/**
* Description of the Class
*
*@author mixa
*@created May 21, 2003
*/
public class UserFunction extends FunctionObject {
private final NameObjectBase name;
private final QuotedList args;
private final LispObject body;
/**
* True for (defmacro) - arguments not evaluated on function call
*/
private final boolean isMacro;
/**
* Creates User Functor on (defun) or (defmacro)
* @param isMacro (defun) / (defmacro) selector
* @param name user functor
* @param args arguments list
* @param body function body
*/
public UserFunction(boolean isMacro, NameObjectBase name, QuotedList args, LispObject body) {
this.isMacro = isMacro;
this.name = name;
this.args = args;
this.body = body;
try {
name.setMappedObject(this);
}
catch (EvaluateException e) {
debug("cannot set name "+name);
}
}
public LispObject evaluate(FunctorList sexp) throws EvaluateException {
LispObject shadowed[] = new LispObject[ args.value.length ];
// evaluate args
for (int i = 0; i < args.value.length; i++) {
shadowed[i] = isMacro ? sexp.getArgumentN(i+1) : sexp.evaluateArgN(i+1);
}
// shadow this variables
for (int i = 0; i < args.value.length; i++) {
NameObjectBase nm = (NameObjectBase) args.value[i];
LispObject temp = nm.getMappedObject();
nm.setMappedObject(shadowed[i]);
shadowed[i] = temp;
}
// evaluate the sexps that constitute this function
LispObject result = body.interpret();
// copy the values of the shadowed variables back into the environment
for (int i = 0; i < args.value.length; i++) {
NameObjectBase nm = (NameObjectBase) args.value[i];
nm.setMappedObject(shadowed[i]);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -