📄 lispstandard.java
字号:
package net.sf.yacas;class LispStandard{ static boolean IsNumber(String ptr, boolean aAllowFloat) { int pos = 0; if (ptr.charAt(pos) == '-' || ptr.charAt(pos) == '+') pos++; int nrDigits=0; int index=0; if (pos+index == ptr.length()) return false; while(ptr.charAt(pos+index) >= '0' && ptr.charAt(pos+index) <= '9') { nrDigits++; index++; if (pos+index == ptr.length()) return true; } if (ptr.charAt(pos+index) == '.') { if (!aAllowFloat) return false; index++; if (pos+index == ptr.length()) return true; while(ptr.charAt(pos+index) >= '0' && ptr.charAt(pos+index) <= '9') { nrDigits++; index++; if (pos+index == ptr.length()) return true; } } if (nrDigits == 0) return false; if (ptr.charAt(pos+index) == 'e' || ptr.charAt(pos+index) == 'E') { if (!aAllowFloat) return false; if (!BigNumber.NumericSupportForMantissa()) return false; index++; if (pos+index == ptr.length()) return true; if (ptr.charAt(pos+index) == '-' || ptr.charAt(pos+index) == '+') index++; while(ptr.charAt(pos+index) >= '0' && ptr.charAt(pos+index) <= '9') { index++; if (pos+index == ptr.length()) return true; } } if (ptr.length() != (pos+index)) return false; return true; } static int InternalListLength(LispPtr aOriginal) throws Exception { LispIterator iter = new LispIterator(aOriginal); int length = 0; while (iter.GetObject() != null) { iter.GoNext(); length++; } return length; } static void InternalReverseList(LispPtr aResult, LispPtr aOriginal) { LispPtr iter = new LispPtr(aOriginal); LispPtr previous = new LispPtr(); LispPtr tail = new LispPtr(); tail.Set(aOriginal.Get()); while (iter.Get() != null) { tail.Set(iter.Get().Next().Get()); iter.Get().Next().Set(previous.Get()); previous.Set(iter.Get()); iter.Set(tail.Get()); } aResult.Set(previous.Get()); } public static void ReturnUnEvaluated(LispPtr aResult,LispPtr aArguments, LispEnvironment aEnvironment) throws Exception { LispPtr full = new LispPtr(); full.Set(aArguments.Get().Copy(false)); aResult.Set(LispSubList.New(full.Get())); LispIterator iter = new LispIterator(aArguments); iter.GoNext(); while (iter.GetObject() != null) { LispPtr next = new LispPtr(); aEnvironment.iEvaluator.Eval(aEnvironment, next, iter.Ptr()); full.Get().Next().Set(next.Get()); full.Set(next.Get()); iter.GoNext(); } full.Get().Next().Set(null); } public static void InternalApplyString(LispEnvironment aEnvironment, LispPtr aResult, String aOperator,LispPtr aArgs) throws Exception { LispError.Check(InternalIsString(aOperator),LispError.KLispErrNotString); LispObject head = LispAtom.New(aEnvironment,SymbolName(aEnvironment, aOperator)); head.Next().Set(aArgs.Get()); LispPtr body = new LispPtr(); body.Set(LispSubList.New(head)); aEnvironment.iEvaluator.Eval(aEnvironment, aResult, body); } public static void InternalApplyPure(LispPtr oper,LispPtr args2,LispPtr aResult, LispEnvironment aEnvironment) throws Exception { LispError.Check(oper.Get().SubList() != null,LispError.KLispErrInvalidArg); LispError.Check(oper.Get().SubList().Get() != null,LispError.KLispErrInvalidArg); LispPtr oper2 = new LispPtr(); oper2.Set(oper.Get().SubList().Get().Next().Get()); LispError.Check(oper2.Get() != null,LispError.KLispErrInvalidArg); LispPtr body = new LispPtr(); body.Set(oper2.Get().Next().Get()); LispError.Check(body.Get() != null,LispError.KLispErrInvalidArg); LispError.Check(oper2.Get().SubList() != null,LispError.KLispErrInvalidArg); LispError.Check(oper2.Get().SubList().Get() != null,LispError.KLispErrInvalidArg); oper2.Set(oper2.Get().SubList().Get().Next().Get()); aEnvironment.PushLocalFrame(false); try { while (oper2.Get() != null) { LispError.Check(args2.Get() != null,LispError.KLispErrInvalidArg); String var = oper2.Get().String(); LispError.Check(var != null,LispError.KLispErrInvalidArg); LispPtr newly = new LispPtr(); newly.Set(args2.Get().Copy(false)); aEnvironment.NewLocal(var,newly.Get()); oper2.Set(oper2.Get().Next().Get()); args2.Set(args2.Get().Next().Get()); } LispError.Check(args2.Get() == null,LispError.KLispErrInvalidArg); aEnvironment.iEvaluator.Eval(aEnvironment, aResult, body); } catch (Yacasexception e) { throw e; } finally { aEnvironment.PopLocalFrame(); } } public static void InternalTrue(LispEnvironment aEnvironment, LispPtr aResult) throws Exception { aResult.Set(aEnvironment.iTrue.Copy(false)); } public static void InternalFalse(LispEnvironment aEnvironment, LispPtr aResult) throws Exception { aResult.Set(aEnvironment.iFalse.Copy(false)); } public static void InternalBoolean(LispEnvironment aEnvironment, LispPtr aResult, boolean aValue) throws Exception { if (aValue) { InternalTrue(aEnvironment, aResult); } else { InternalFalse(aEnvironment, aResult); } } public static void InternalNth(LispPtr aResult, LispPtr aArg, int n) throws Exception { LispError.Check(aArg.Get() != null,LispError.KLispErrInvalidArg); LispError.Check(aArg.Get().SubList() != null,LispError.KLispErrInvalidArg); LispError.Check(n>=0,LispError.KLispErrInvalidArg); LispIterator iter = new LispIterator(aArg.Get().SubList()); while (n>0) { LispError.Check(iter.GetObject() != null,LispError.KLispErrInvalidArg); iter.GoNext(); n--; } LispError.Check(iter.GetObject() != null,LispError.KLispErrInvalidArg); aResult.Set(iter.GetObject().Copy(false)); } public static void InternalTail(LispPtr aResult, LispPtr aArg) throws Exception { LispError.Check(aArg.Get() != null,LispError.KLispErrInvalidArg); LispError.Check(aArg.Get().SubList() != null,LispError.KLispErrInvalidArg); LispPtr iter = aArg.Get().SubList(); LispError.Check(iter.Get() != null,LispError.KLispErrInvalidArg); aResult.Set(LispSubList.New(iter.Get().Next().Get())); } public static boolean IsTrue(LispEnvironment aEnvironment, LispPtr aExpression) throws Exception { LispError.LISPASSERT(aExpression.Get() != null); return aExpression.Get().String() == aEnvironment.iTrue.String(); } public static boolean IsFalse(LispEnvironment aEnvironment, LispPtr aExpression) throws Exception { LispError.LISPASSERT(aExpression.Get() != null); return aExpression.Get().String() == aEnvironment.iFalse.String(); } public static String SymbolName(LispEnvironment aEnvironment, String aSymbol) { if (aSymbol.charAt(0) == '\"') { return aEnvironment.HashTable().LookUpUnStringify(aSymbol); } else { return aEnvironment.HashTable().LookUp(aSymbol); } } public static boolean InternalIsList(LispPtr aPtr) throws Exception { if (aPtr.Get() == null) return false; if (aPtr.Get().SubList() == null) return false; if (aPtr.Get().SubList().Get() == null) return false; //TODO this StrEqual is far from perfect. We could pass in a LispEnvironment object... if (!aPtr.Get().SubList().Get().String().equals("List")) return false; return true; } public static boolean InternalIsString(String aOriginal) { if (aOriginal != null) if (aOriginal.charAt(0) == '\"') if (aOriginal.charAt(aOriginal.length()-1) == '\"') return true; return false; } public static void InternalNot(LispPtr aResult, LispEnvironment aEnvironment, LispPtr aExpression) throws Exception { if (IsTrue(aEnvironment, aExpression)) { InternalFalse(aEnvironment,aResult); } else { LispError.Check(IsFalse(aEnvironment, aExpression),LispError.KLispErrInvalidArg); InternalTrue(aEnvironment,aResult); } } public static void InternalFlatCopy(LispPtr aResult, LispPtr aOriginal) throws Exception { LispIterator orig = new LispIterator(aOriginal); LispIterator res = new LispIterator(aResult); while (orig.GetObject() != null) { res.Ptr().Set(orig.GetObject().Copy(false)); orig.GoNext(); res.GoNext(); } } public static boolean InternalEquals(LispEnvironment aEnvironment, LispPtr aExpression1, LispPtr aExpression2) throws Exception { // Handle pointers to same, or null if (aExpression1.Get() == aExpression2.Get()) { return true; } BigNumber n1 = aExpression1.Get().Number(aEnvironment.Precision()); BigNumber n2 = aExpression2.Get().Number(aEnvironment.Precision()); if (!(n1 == null && n2 == null) ) { if (n1 == n2) { return true; } if (n1 == null) return false; if (n2 == null) return false; if (n1.Equals(n2)) return true; return false; } //Pointers to strings should be the same if (aExpression1.Get().String() != aExpression2.Get().String()) { return false; } // Handle same sublists, or null if (aExpression1.Get().SubList() == aExpression2.Get().SubList()) { return true; } // Now check the sublists if (aExpression1.Get().SubList() != null) { if (aExpression2.Get().SubList() == null) { return false; } LispIterator iter1 = new LispIterator(aExpression1.Get().SubList()); LispIterator iter2 = new LispIterator(aExpression2.Get().SubList()); while (iter1.GetObject() != null && iter2.GetObject() != null) { // compare two list elements if (!InternalEquals(aEnvironment, iter1.Ptr(),iter2.Ptr())) { return false; } // Step to next iter1.GoNext(); iter2.GoNext(); } // Lists don't have the same length if (iter1.GetObject() != iter2.GetObject()) return false; // Same! return true; } // expressions sublists are not the same! return false; } public static void InternalSubstitute(LispPtr aTarget, LispPtr aSource, SubstBehaviourBase aBehaviour) throws Exception { LispObject object = aSource.Get(); LispError.LISPASSERT(object != null); if (!aBehaviour.Matches(aTarget,aSource)) { LispPtr oldList = object.SubList(); if (oldList != null) { LispPtr newList = new LispPtr(); LispPtr next = newList; while (oldList.Get() != null) { InternalSubstitute(next, oldList, aBehaviour); oldList = oldList.Get().Next(); next = next.Get().Next(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -