[Mathlib-commitlog] mathlib/Source/MathLib/Functions UserFunction.java, 1.35, 1.36
Status: Beta
Brought to you by:
st_mueller
|
From: Stefan M. <st_...@us...> - 2007-01-05 14:29:25
|
Update of /cvsroot/mathlib/mathlib/Source/MathLib/Functions In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24875/Source/MathLib/Functions Modified Files: UserFunction.java Log Message: added support for "varargin" Index: UserFunction.java =================================================================== RCS file: /cvsroot/mathlib/mathlib/Source/MathLib/Functions/UserFunction.java,v retrieving revision 1.35 retrieving revision 1.36 diff -C2 -d -r1.35 -r1.36 *** UserFunction.java 31 Aug 2006 08:41:16 -0000 1.35 --- UserFunction.java 5 Jan 2007 14:29:21 -0000 1.36 *************** *** 38,42 **** { Context functionContext; // The context of the function ! OperandToken result = null; if (mScriptB) --- 38,44 ---- { Context functionContext; // The context of the function ! OperandToken result = null; ! boolean vararginB = false; ! boolean varargoutB = false; if (mScriptB) *************** *** 50,53 **** --- 52,56 ---- // evaluate m-function + // m-functions have a local context (local variables) VariableList localVariables = new VariableList(); functionContext = getContextList().createContext(localVariables); *************** *** 59,71 **** //getContextList().pushContext(activeContext); - //create a local variable to store the no of arguments - //getVariables().createVariable("NARGIN"); - - //create a local variable to store the no of return values expected - //getVariables().createVariable("NARGOUT"); - - //set the functions parameters - //setFunctionParameters(operands); - //set the variable NARGIN to the number of arguments int opLength = 0; --- 62,65 ---- *************** *** 73,86 **** opLength = operands.length; // check if number of parameters inside the function is equal to the number // of calling expression // e.g. plot(x,y,z) // function [...]=plot(x,y,z) ! if (parameterVariables.size() < opLength) Errors.throwMathLibException("UserFunction: "+name+" number of parameters to large"); - // set the variable NARGIN to the number of parameter values - //getVariables().createVariable("NARGIN").assign(new NumberToken(parameterVariables.size())); - // set the variable NARGIN to the number of parameters of the calling function getVariables().createVariable("nargin").assign(new NumberToken(opLength)); --- 67,99 ---- opLength = operands.length; + // check for "varargin" as last input parameter + // e.g. function x=barfoo(a,b,c,varargin) + if (parameterVariables.size()>0) + { + if ( ((String)parameterVariables.get(parameterVariables.size()-1)).equals("varargin") ) + { + vararginB = true; + ErrorLogger.debugLine("UserF: varargin found"); + } + } + + // check for "varargout" as last input parameter + // e.g. function [x,y,varargout]=barfoo(a,b,c) + if (returnVariables.size()>0) + { + if ( ((String)returnVariables.get(returnVariables.size()-1)).equals("varargout") ) + { + varargoutB = true; + ErrorLogger.debugLine("UserF: varargout found"); + } + } + // check if number of parameters inside the function is equal to the number // of calling expression // e.g. plot(x,y,z) // function [...]=plot(x,y,z) ! if ( (parameterVariables.size() < opLength) && !vararginB ) Errors.throwMathLibException("UserFunction: "+name+" number of parameters to large"); // set the variable NARGIN to the number of parameters of the calling function getVariables().createVariable("nargin").assign(new NumberToken(opLength)); *************** *** 89,110 **** getVariables().createVariable("nargout").assign(new NumberToken(returnVariables.size())); - //getContextList().setContextDetails(name); ! // e.g. createVariable("x").assign(operands[1]) ! // e.g. createVariable("x").assign(operands[1]) ! //set the parameters for the function ! for(int paramNo = 0; paramNo < opLength; paramNo++) ! { ! String parameterName = (String)parameterVariables.get(paramNo); ! //System.out.println("UserFunction: "+parameterName); ! getVariables().createVariable(parameterName).assign((OperandToken)operands[paramNo]); ! } ! try { - //executeCurrentContext(); - //result = getFunctionResult(); - // must clone function code, so that the original code remains untouched OperandToken codeLocal = (OperandToken)code.clone(); --- 102,155 ---- getVariables().createVariable("nargout").assign(new NumberToken(returnVariables.size())); ! //set the input parameters for the function ! // e.g. function x=barfoo(x,y,z) ! // e.g. function x=barfoo(x,y,z,varargin) ! if (!vararginB) ! { ! // e.g. function =bar(a,b,c,d,e) ! for(int paramNo = 0; paramNo < opLength; paramNo++) ! { ! String parameterName = (String)parameterVariables.get(paramNo); ! //System.out.println("UserFunction: "+parameterName); ! getVariables().createVariable(parameterName).assign((OperandToken)operands[paramNo]); ! } ! } ! else ! { ! //e.g. function =bar(a,b,c,varargin) ! int parN = parameterVariables.size(); ! int remainingOps = opLength - (parN - 1); ! ! // copy parameters, but not "varargin" (copy parameters 0 ... n-1) ! for(int paramNo = 0; paramNo < Math.min((parN - 1), opLength); paramNo++) ! { ! String parameterName = (String)parameterVariables.get(paramNo); ! ErrorLogger.debugLine("UserF: params: "+parameterName); ! getVariables().createVariable(parameterName).assign((OperandToken)operands[paramNo]); ! } ! ! ErrorLogger.debugLine("UserF: remainingOps: "+ remainingOps); ! ! ! // copy remaining operands into cell array, but only if operands are left ! // e.g. function x=barfoo(a,b,c,d,varargin) ! // with barfoo(1,2,3) will have varargin==null; ! if (remainingOps >0) ! { ! OperandToken[][] values = new OperandToken[remainingOps][1]; ! for (int i=0; i<remainingOps; i++) ! { ! values[i][0] = (OperandToken)operands[parameterVariables.size()-1+i]; ! } ! CellArrayToken cell = new CellArrayToken(values); ! getVariables().createVariable("varargin").assign(cell); ! } ! } ! + // execute m-function try { // must clone function code, so that the original code remains untouched OperandToken codeLocal = (OperandToken)code.clone(); *************** *** 114,118 **** // result should be NumberToken e.g. 1+2 -> >3< // or a MatrixToken e.g. [x,y]=foo(2,4) -> [x,y] - if(returnVariables.size() == 1) { --- 159,162 ---- *************** *** 146,166 **** //reset to the previous variable frame getContextList().popContext(); - //activeContext = null; } catch(ControlException e) { - result = e.getResults(); - if(e.getType() == ControlException.Yield) - { - // assign return values - //activeContext = getContextList().popContext(); - } - else - { // assign return values getContextList().popContext(); //activeContext = null; - } } } --- 190,205 ---- //reset to the previous variable frame getContextList().popContext(); } catch(ControlException e) { // assign return values getContextList().popContext(); //activeContext = null; } + catch (Exception e) + { + getContextList().popContext(); + throwMathLibException(e.getMessage()); + } } |