package nl.bluering; import java.util.*; /** * This library does all basic mathematic funtions */ public class MathLib extends Interpreter { public final static int ADD=1,SUB=2,MUL=3,MOD=4,DIV=5,NEG=6,GT=7,LT=8,GTE=9; public final static int LSHIFT=10,RSHIFT=11,ROUND=12,FLOOR=13,CEIL=14; public final static int SIN=15,COS=16,TAN=17,SQRT=18,EXP=19,LOG=20; public final static int LTE=21,NEQ=22,EQ=23,PI=24,E=25,AND=26,OR=27,NOT=28; public final static int ORB=29,ANDB=30,XOR=31; MathLib() { } public void mountall() { mnormal("add",100,ADD,"add two integers",numberclass); mnormal("sub",2,SUB,"subtract two integers",numberclass); mnormal("mul",100,MUL,"multiply two integers",numberclass); mnormal("mod",2,MOD,"calculate the remainder",numberclass); mnormal("div",2,DIV,"divide one integer by another",numberclass); mnormal("neg",1,NEG,"return -arg",numberclass); mnormal("gt",2,GT,"returns true if the first number is greater than the second",numberclass); mnormal("lt",2,LT,"returns true if the first number is less than the second",numberclass); mnormal("gte",2,GTE,"returns true if the first number is not less",numberclass); mnormal("lshift",2,LSHIFT,"shift a number to the left",numberclass); mnormal("rshift",2,RSHIFT,"shift a number to the right",numberclass); mnormal("round",1,ROUND,"round towards the nearest integer",numberclass); mnormal("floor",1,FLOOR,"round to a lower integer",numberclass); mnormal("ceil",1,CEIL,"round to a higher integer",numberclass); mnormal("sin",1,SIN,"calculate the sinus function",numberclass); mnormal("cos",1,COS,"cosinus function",numberclass); mnormal("tan",1,TAN,"tangens function",numberclass); mnormal("sqrt",1,SQRT,"square root",numberclass); mnormal("exp",2,EXP,"raise a1 to the power a2",numberclass); mnormal("log",1,LOG,"calculate the natural logarithm",numberclass); mnormal("lte",2,LTE,"returns true if the first number is not greater",numberclass); mnormal("neq",2,NEQ,"returns true if the arguments are not equal",numberclass); mnormal("eq",2,EQ,"returns true if the arguments are equal",numberclass); mspecial("pi",0,PI,"returns the number pi=3.1415926..."); mspecial("euler",0,E,"returns the number e=2.71828..."); mnormal("and",100,AND,"returns true if both arguments are true",numberclass); mnormal("or",100,OR,"returns true if one argument is true",numberclass); mnormal("not",1,NOT,"returns true if the argument is false",numberclass); mnormal("orb",2,ORB,"returns bitwise or",numberclass); mnormal("andb",2,ANDB,"bitwise and",numberclass); mnormal("xor",2,XOR,"returns bitwise xor",numberclass); } public Variable run(Variable result,Expression e,int index) throws Exception { double d1,d2; int i,i2; boolean b1; switch(index) {case ADD: d1=result.getdouble(); for(i=1;i root.run(result,e.get(1)).getdouble()); break; case LT: result.set(result.getdouble()< root.run(result,e.get(1)).getdouble()); break; case GTE: result.set(result.getdouble()>= root.run(result,e.get(1)).getdouble()); break; case LTE: result.set(result.getdouble()<= root.run(result,e.get(1)).getdouble()); break; case LSHIFT: result.set(result.getint()<< root.run(result,e.get(1)).getint()); break; case RSHIFT: result.set(result.getint()>> root.run(result,e.get(1)).getint()); break; case ROUND: result.set(result.getint()); break; case FLOOR: result.set((int)Math.floor(result.getdouble())); break; case CEIL: result.set((int)Math.ceil(result.getdouble())); break; case SIN: result.set(Math.sin(result.getdouble())); break; case COS: result.set(Math.cos(result.getdouble())); break; case TAN: result.set(Math.tan(result.getdouble())); break; case SQRT: result.set(Math.sqrt(result.getdouble())); break; case EXP: result.set(Math.pow(result.getdouble(), root.run(result,e.get(1)).getdouble())); break; case LOG: result.set(Math.log(result.getdouble())); break; case NEQ: result.set(result.getdouble()!= root.run(result,e.get(1)).getdouble()); break; case EQ: result.set(result.getdouble()== root.run(result,e.get(1)).getdouble()); break; case PI: result.set(Math.PI); break; case E: result.set(Math.E); break; case AND: b1=result.getboolean(); for(i=1;i