package nl.bluering; import java.io.*; import java.util.*; import java.math.BigInteger; public abstract class Interpreter { RootLib root; static Class numberclass=new Integer(0).getClass(); static Class stringclass="".getClass(); Hashtable libdef=new Hashtable(); public void mount(functionpointer f) {functionpointer olddef=getfp(f.getprototype()); if (olddef!=null&&!olddef.isnormal()) {System.out.println("mount failed"); return; } if(f.isnormal()) olddef=getfp(f.getprototype(),f.a1class); if(olddef!=null) {System.out.println("mount of normal failed"); return; } mount2(f); } private void mount2(functionpointer f) { if (f.isnormal()) { // System.out.println("mount "+f.getextendedprototype()+" : "+f); libdef.put(f.getextendedprototype(),f); } //System.out.println("mount "+f.getprototype()+" : "+f); libdef.put(f.getprototype(),f); if(root!=null&&root!=this) root.mount(f); } abstract void mountall(); abstract Variable run(Variable result,Expression e,int index) throws Exception; Interpreter() {} void installin(RootLib r) {root=r; mountall(); } void mnormal(String name,int arity,int index,String help,Class c) {mount(new functionpointer(name,arity,this,index,help,c));} void mspecial(String name,int arity,int index,String help) {mount(new functionpointer(name,arity,this,index,help));} static File getfile(Variable result) { Object o=result.getobject(); if(o instanceof File) return (File)o; return null; } static ExpParser getexpparser(Variable result) { Object o=result.getobject(); if(o instanceof ExpParser) return (ExpParser)o; return null; } static ExpWriter getexpwriter(Variable result) { Object o=result.getobject(); if(o instanceof ExpWriter) return (ExpWriter)o; return null; } static RandomAccessFile getraf(Variable result) { Object o=result.getobject(); if(o instanceof RandomAccessFile) return (RandomAccessFile)o; return null; } static byte[] getba(Variable result) { Object o=result.getobject(); if(o instanceof byte[]) return (byte[])o; return null; } BigInteger getbi(Variable result,Expression e,int pos) throws Exception {Object o1=root.run(result,e.get(pos)).getobject(); if(o1 instanceof String) o1=new BigInteger((String)o1); if(o1==null || !(o1 instanceof BigInteger)) throw new Exception("BigInteger expected:"+o1.getClass().getName()); return (BigInteger)o1; } functionpointer getfp(String name,int arity) {functionpointer result=getfp(functionpointer.makeprototype(name,arity)); if(result==null&&arity>0) result=getfp(functionpointer.makeprototype(name,100)); return result; } /** * get the functionpointer * @param prototype a (not-extended) prototype * @return a functionpointer if there is any */ functionpointer getfp(String prototype) {return (functionpointer)libdef.get(prototype); } functionpointer getfp(String name,int arity,Class c) {functionpointer result=getfp(functionpointer.makeprototype(name,arity),c); if(result==null&&arity>0) result=getfp(functionpointer.makeprototype(name,100),c); return result; } /** * get the functionpointer. This only gets normal functions. You * should try the other getfp first to also find special functions. * @param prototype a (not-extended) prototype * @param c the class it must work on * @return a functionpointer if there is any for that specific class */ functionpointer getfp(String prototype,Class c1) { functionpointer result=null; Class c=c1; while (c!=null) { result=(functionpointer)libdef.get( functionpointer.extendprototype(prototype,c) ); if(result!=null) return result; c=Variable.getsuperclass(c); } if (c1==stringclass) result=(functionpointer) libdef.get(functionpointer.extendprototype(prototype,numberclass)); else if (c1==numberclass) result=(functionpointer) libdef.get(functionpointer.extendprototype(prototype,stringclass)); return result; } }