package nl.bluering; import java.util.*; class functionpointer { //the name of the function. String name; //the number of arguments of this function int arity; //the library that can do this function Interpreter domain; //the number that the library needs to know what it is doing int index; //the help message: one-line description of this function String help; //a term that defines this function, if this function is userdefined. Expression userdef; //the class of the objects it can handle as first argument.null if all. //integer if int, boolean, etc Class a1class; /** * mount a userdefined function. */ functionpointer(String name, int arity,Expression def) {this.name=name; this.arity=arity; this.userdef=def; } /** * mount a special function. A special function must have a globally * unique name and may not look at the value of result. */ functionpointer(String name,int arity,Interpreter domain, int index, String help) {this.name=name; this.arity=arity; this.domain=domain; this.index=index; this.help=help; } /** * mount a normal function. A normal function may share its name with * other normal functions defined on different first argument types * and gets the evaluation of its first argument in result. * @param c the class of the objects its first argument must be. use the * Integer Class object for int's, and the Double class for doubles. */ functionpointer(String name,int arity,Interpreter domain, int index, String help,Class c) {this.name=name; this.arity=arity; this.domain=domain; this.index=index; this.help=help; a1class=c; } public boolean userdefined() {return userdef!=null;} public boolean isnormal() {return a1class!=null;} /** * functions with a specific number of arguments can have up to * MANY_ARG arguments. Anything more and it is considered a function * with any number of arguments. */ public final static int MANY_ARG=6; /** * a prototype is a combination of a function name and the number of arguments. * prototypes are used as keys in hashtables. */ String getprototype() {return name+"_"+(arity>MANY_ARG?MANY_ARG:arity); } /** * an extended prototype is a combination of a function name,the number * of arguments and the className of the first argument. This allows * functions with the same normal prototype to be differentiated. * extended prototypes are used as keys in hashtables. */ String getextendedprototype() {if(!isnormal()) { return getprototype(); } return makeextprototype(name,arity,a1class); } static String makeprototype(String name,int arity) { return name+"_"+(arity>MANY_ARG?MANY_ARG:arity); } static String makeextprototype(String name,int arity,Class c) {return name+"_"+(arity>MANY_ARG?MANY_ARG:arity)+"@"+c.getName(); } static String extendprototype(String prototype,Class c) {return prototype+"@"+c.getName(); } /** * return whether this function matches the call * @param c the Class of the first argument. */ boolean workson(Class c) { //System.out.println(c.getName()+"=?="+a1class.getName()); while (c!=null) {if(c==a1class) return true; c=Variable.getsuperclass(c); } return false; } Expression getdef() {return userdef; } String manualline() { if(userdefined()) return "