Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/hartmath/lib/Session.java


1   /*
2    *  Session.java
3    *  Copyright (C) 2001 Klaus Hartlage
4    *
5    *  This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU General Public License
7    *  as published by the Free Software Foundation; either version 2
8    *  of the License, or any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.hartmath.lib;
20  
21  import com.hartmath.util.*;
22  import com.hartmath.mapping.*;
23  import com.hartmath.patternmatching.*;
24  import com.hartmath.Main;
25  
26  import java.io.*;
27  import java.net.*;
28  import java.lang.*;
29  import java.applet.*;
30  import java.awt.Label;
31  import java.util.Properties;
32  import java.util.Stack;
33  import java.util.HashMap;
34  
35  import com.hartmath.expression.HObject;
36  import com.hartmath.expression.HSignedNumber;
37  import com.hartmath.expression.HSymbolData;
38  import com.hartmath.expression.HSymbol;
39  import com.hartmath.expression.HSymbolFunctionData;
40  import com.hartmath.expression.HFunction;
41  import com.hartmath.expression.HArrayList;
42  import com.hartmath.exceptions.HThrowException;
43  import com.hartmath.exceptions.HReturnException;
44  import com.hartmath.exceptions.HContinueException;
45  import com.hartmath.exceptions.HBreakException;
46  
47  /**
48   *  Defines the 'Session' or 'NameSpace' for an evalution. This class can be
49   *  mapped in servlets to distinguish different SessionIDs.
50   *
51   *@author     khartlage
52   *@created    31. Juli 2001
53   */
54  public class Session {
55    private boolean numericFlag;
56    private boolean holdNumericEvaluationFlag;
57    private boolean traceEval = false;
58    //  private HFunction traceList = null;
59    private HFunction lastTraceList = null;
60    //    private Stack traceStack= null;
61    private int recursionLimit = -1;
62    private int iterationLimit = -1;
63    private int recursionCounter;
64    private int iterationCounter;
65  
66    private int digits = 0;
67  
68    // session history
69    private int count;
70    // input history
71    private HArrayList InArray = new HArrayList();
72    // output history
73    private HArrayList OutArray = new HArrayList();
74    private Writer resultOut;
75    private Writer errorOut;
76    /**
77     *  couner for IOOut or IOIn
78     */
79    private int IOCounter;
80    /**
81     *  contains expressions for WriteObject()
82     */
83    private HashMap IOOut = new HashMap();
84  
85    /**
86     *  contains expressions for ReadObject()
87     */
88    private HashMap IOIn = new HashMap();
89  
90    /**
91     *  contains the all symbol to symbolData mappings for a particular session
92     */
93    private HashMap SymbolDataSet;
94  
95    private static String lineSeparator = System.getProperty("line.separator");
96  
97  
98    /**
99     *  Constructor for the S object
100    */
101   public Session() {
102     numericFlag = false;
103     holdNumericEvaluationFlag = false;
104 
105     traceEval = false;
106     //traceList = null;
107     lastTraceList = null;
108     //traceStack = null;
109     recursionLimit = -1;
110     iterationLimit = -1;
111     recursionCounter = 0;
112     iterationCounter = 0;
113 
114     digits = 0;
115 
116     count = 0;
117     InArray = new HArrayList();
118     OutArray = new HArrayList();
119 
120     resultOut = null;
121     errorOut = null;
122 
123     IOCounter = 0;
124     IOOut = new HashMap();
125     IOIn = new HashMap();
126 
127     SymbolDataSet = new HashMap();
128   }
129 
130 
131   /**
132    *  Sets the numericFlag attribute of the S object
133    *
134    *@param  numericFlag  The new numericFlag value
135    */
136   public final void setNumericFlag(boolean numericFlag) {
137     this.numericFlag = numericFlag;
138   }
139 
140 
141   /**
142    *  Sets the holdNumericEvaluationFlag attribute of the Session object
143    *
144    *@param  holdNumericEvaluationFlag  The new holdNumericEvaluationFlag value
145    */
146   public final void setHoldNumericEvaluationFlag(boolean holdNumericEvaluationFlag) {
147     this.holdNumericEvaluationFlag = holdNumericEvaluationFlag;
148   }
149 
150 
151   /**
152    *  Sets the recursionLimit attribute of the S object
153    *
154    *@param  l  The new recursionLimit value
155    */
156   public final void setRecursionLimit(int l) {
157     recursionLimit = l;
158   }
159 
160 
161   /**
162    *  Sets the iterationLimit attribute of the S object
163    *
164    *@param  l  The new iterationLimit value
165    */
166   public final void setIterationLimit(int l) {
167     iterationLimit = l;
168   }
169 
170 
171   /**
172    *  Sets the recursionCounter attribute of the S object
173    *
174    *@param  cnt  The new recursionCounter value
175    */
176   public final void setRecursionCounter(int cnt) {
177     recursionCounter = cnt;
178   }
179 
180 
181   /**
182    *  Sets the iterationCounter attribute of the S object
183    *
184    *@param  cnt  The new iterationCounter value
185    */
186   public final void setIterationCounter(int cnt) {
187     iterationCounter = cnt;
188   }
189 
190 
191   /**
192    *  Sets the digits attribute of the S object
193    *
194    *@param  dig  The new digits value
195    */
196   public final void setDigits(int dig) {
197     digits = dig;
198   }
199 
200 
201   /**
202    *  Sets the resultOut attribute of the S object
203    *
204    *@param  out  The new resultOut value
205    */
206   public final void setResultOut(Writer out) {
207     resultOut = out;
208   }
209 
210 
211   /**
212    *  Sets the errorOut attribute of the S object
213    *
214    *@param  out  The new errorOut value
215    */
216   public final void setErrorOut(Writer out) {
217     errorOut = out;
218   }
219 
220 
221   /**
222    *  Sets the iOCounter attribute of the S object
223    *
224    *@param  cnt  The new iOCounter value
225    */
226   public final void setIOCounter(int cnt) {
227     IOCounter = cnt;
228   }
229 
230 
231   /**
232    *  Sets the symbolData attribute of the S object
233    *
234    *@param  sym   The new symbolData value
235    *@param  dSet  The new symbolData value
236    */
237   public final void setSymbolData(HSymbol sym, HSymbolFunctionData dSet) {
238     if (C.DEBUG)
239     {
240       HSymbolData symData = (HSymbolData) SymbolDataSet.get(sym);
241       if (symData != null)
242       {
243         throw new HThrowException(sym, C.Error);
244       }
245     }
246     SymbolDataSet.put(sym, dSet);
247     // ELoadBeanShell.loadInitialBeanShell(sym, this);
248   }
249 
250   /**
251    *  Sets the symbolData attribute of the S object
252    *
253    *@param  sym  The new symbolData value
254    */
255   public final void setSymbolData(HSymbol sym) {
256     if (C.DEBUG)
257     {
258       HSymbolData symData = (HSymbolData) SymbolDataSet.get(sym);
259       if (symData != null)
260       {
261         throw new HThrowException(sym, C.Error);
262       }
263     }
264     SymbolDataSet.put(sym,
265                       new HSymbolFunctionData(sym,
266                                               HSymbol.NOATTRIBUTE,
267                                               sym.getBuiltinFunction()));
268     //  ELoadBeanShell.loadInitialBeanShell(sym, this);
269   }
270 
271 
272   /**
273    *  Sets the symbolData attribute of the S object
274    *
275    *@param  sym         The new symbolData value
276    *@param  attributes  The new symbolData value
277    */
278   public final void setSymbolData(HSymbol sym, int attributes) {
279     if (C.DEBUG)
280     {
281       HSymbolData symData = (HSymbolData) SymbolDataSet.get(sym);
282       if (symData != null)
283       {
284         throw new HThrowException(sym, C.Error);
285       }
286     }
287     SymbolDataSet.put(sym,
288                       new HSymbolFunctionData(sym,
289                                               attributes,
290                                               sym.getBuiltinFunction()));
291     /*   if ( (attributes & HSymbol.BEANSHELLONCE) != HSymbol.BEANSHELLONCE)
292        {
293          ELoadBeanShell.loadInitialBeanShell(sym, this);
294        }
295        */
296 
297   }
298 
299 
300   /**
301    *  Sets the rules attribute of the S object
302    *
303    *@param  larr  The new rules value
304    *@param  barr  The new rules value
305    *@return       Description of the Returned Value
306    */
307   public HObject setRules(long larr[], byte barr[]) {
308     try
309     {
310       ByteArrayOutputStream boStream = new ByteArrayOutputStream();
311       DataOutputStream out = new DataOutputStream(boStream);
312       for (int i = 0; i < larr.length; i++)
313       {
314         out.writeLong(larr[i]);
315       }
316       for (int i = 0; i < barr.length; i++)
317       {
318         out.writeByte(barr[i]);
319       }
320       ByteArrayInputStream baStream = new ByteArrayInputStream(boStream.toByteArray());
321       DataInputStream in = new DataInputStream(baStream);
322       HObject obj = IOMapper.map(in, this);
323       in.close();
324       return obj;
325     }
326     catch (Exception e)
327     {
328       appendErrorOut(e.toString());
329       return null;
330     }
331   }
332 
333 
334   /**
335    *  Sets the traceEval attribute of the S object
336    *
337    *@param  flag  The new traceEval value
338    */
339   public void setTraceEval(boolean flag) {
340     traceEval = flag;
341   }
342 
343 
344   /**
345    *  Sets the traceList attribute of the S object
346    *
347    *@param  list  The new traceList value
348    */
349   public void setTraceList(HFunction list) {
350     //traceList = list;
351     lastTraceList = list;
352     //  traceStack = new Stack();
353   }
354 
355 
356   /**
357    *  Gets the numericFlag attribute of the S object
358    *
359    *@return    The numericFlag value
360    */
361   public final boolean getNumericFlag() {
362     return numericFlag;
363   }
364 
365 
366   /**
367    *  Gets the holdNumericEvaluationFlag attribute of the Session object
368    *
369    *@return    The holdNumericEvaluationFlag value
370    */
371   public final boolean getHoldNumericEvaluationFlag() {
372     return holdNumericEvaluationFlag;
373   }
374 
375 
376   /**
377    *  Gets the recursionLimit attribute of the S object
378    *
379    *@return    The recursionLimit value
380    */
381   public final int getRecursionLimit() {
382     return recursionLimit;
383   }
384 
385 
386   /**
387    *  Gets the iterationLimit attribute of the S object
388    *
389    *@return    The iterationLimit value
390    */
391   public final int getIterationLimit() {
392     return iterationLimit;
393   }
394 
395 
396   /**
397    *  Gets the digits attribute of the S object
398    *
399    *@return    The digits value
400    */
401   public final int getDigits() {
402     return digits;
403   }
404 
405 
406   /**
407    *  Gets the inHistory attribute of the S object
408    *
409    *@param  cnt  Description of Parameter
410    *@return      The inHistory value
411    */
412   public final HObject getInHistory(int cnt) {
413     return (HObject) InArray.get(cnt);
414   }
415 
416 
417   /**
418    *  Gets the outHistory attribute of the S object
419    *
420    *@param  cnt  Description of Parameter
421    *@return      The outHistory value
422    */
423   public final HObject getOutHistory(int cnt) {
424     return (HObject) OutArray.get(cnt);
425   }
426 
427 
428   /**
429    *  Gets the counter attribute of the S object
430    *
431    *@return    The counter value
432    */
433   public final int getCounter() {
434     return count;
435   }
436 
437 
438   /**
439    *  Gets the iOCounter attribute of the S object
440    *
441    *@return    The iOCounter value
442    */
443   public final int getIOCounter() {
444     return IOCounter;
445   }
446 
447 
448   /**
449    *  Gets the iOIn attribute of the S object
450    *
451    *@param  num  Description of Parameter
452    *@return      The iOIn value
453    */
454   public final HSymbol getIOIn(Integer num) {
455     return (HSymbol) IOIn.get(num);
456   }
457 
458 
459   /**
460    *  Gets the iOOut attribute of the S object
461    *
462    *@param  str  Description of Parameter
463    *@return      The iOOut value
464    */
465   public final Short getIOOut(String str) {
466     return (Short) IOIn.get(str);
467   }
468 
469 
470   /**
471    *  Gets the symbolData attribute of the S object
472    *
473    *@param  sym  Description of Parameter
474    *@return      The symbolData value
475    */
476   public final HSymbolData getSymbolData(HSymbol sym) {
477     return (HSymbolData) SymbolDataSet.get(sym);
478   }
479 
480 
481   /**
482    *  Gets the localVarValue attribute of the S object
483    *
484    *@param  sym  Description of Parameter
485    *@return      The localVarValue value
486    */
487   public final HObject getLocalVarValue(HSymbol sym) {
488     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
489     if (dSet != null)
490     {
491       return dSet.getLocalVarValue();
492     }
493     return null;
494   }
495 
496 
497   /**
498    *  Gets the localPatternValue attribute of the S object
499    *
500    *@param  sym  Description of Parameter
501    *@return      The localPatternValue value
502    */
503   public final HObject getLocalPatternValue(HSymbol sym) {
504     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
505     if (dSet != null)
506     {
507       return dSet.getLocalPatternValue();
508     }
509     return null;
510   }
511 
512 
513   /**
514    *  Gets the traceEval attribute of the S object
515    *
516    *@return    The traceEval value
517    */
518   public boolean getTraceEval() {
519     return traceEval;
520   }
521 
522 
523   /**
524    *  Gets the traceList attribute of the S object
525    *
526    *@return    The traceList value
527    */
528   public HFunction getTraceList() {
529     return lastTraceList;
530   }
531 
532 
533   /**
534    *  Resets the traceList attribute of the S object
535    */
536   public void resetTraceList() {
537     lastTraceList = null;
538   }
539 
540 
541   /**
542    *  Description of the Method
543    *
544    *@return    Description of the Returned Value
545    */
546   public final boolean recursionExceeded() {
547     return (recursionLimit > 0) && (++recursionCounter > recursionLimit);
548   }
549 
550 
551   /**
552    *  Description of the Method
553    *
554    *@return    Description of the Returned Value
555    */
556   public final boolean iterationExceeded() {
557     return (iterationLimit > 0) && (++iterationCounter > iterationLimit);
558   }
559 
560 
561   /**
562    *  Description of the Method
563    */
564   public final void incRecursionCounter() {
565     recursionCounter++;
566   }
567 
568 
569   /**
570    *  Description of the Method
571    */
572   public final void incIterationCounter() {
573     iterationCounter++;
574   }
575 
576 
577   /**
578    *  Description of the Method
579    */
580   public final void decRecursionCounter() {
581     recursionCounter--;
582   }
583 
584 
585   /**
586    *  Description of the Method
587    */
588   public final void decIterationCounter() {
589     iterationCounter--;
590   }
591 
592 
593   /**
594    *  Adds a feature to the History attribute of the S object
595    */
596   public final void addHistory() {
597     count++;
598     InArray.add(C.Null);
599     OutArray.add(C.Null);
600   }
601 
602 
603   /**
604    *  Description of the Method
605    *
606    *@param  obj  Description of Parameter
607    */
608   public final void changeCurrentInHistory(HObject obj) {
609     InArray.set(count - 1, obj);
610   }
611 
612 
613   /**
614    *  Description of the Method
615    *
616    *@param  obj  Description of Parameter
617    */
618   public final void changeCurrentOutHistory(HObject obj) {
619     OutArray.set(count - 1, obj);
620   }
621 
622 
623   /**
624    *  Description of the Method
625    *
626    *@param  str  Description of Parameter
627    */
628   public final void appendResultOut(String str) {
629     try
630     {
631       resultOut.write(str);
632     }
633     catch (IOException ioe)
634     {
635       ioe.printStackTrace();
636     }
637   }
638 
639 
640   /**
641    *  Description of the Method
642    *
643    *@param  str  Description of Parameter
644    */
645   public final void appendLineResultOut(String str) {
646     try
647     {
648       resultOut.write(str);
649       resultOut.write(lineSeparator);
650     }
651     catch (IOException ioe)
652     {
653       ioe.printStackTrace();
654     }
655   }
656 
657 
658   /**
659    *  Description of the Method
660    *
661    *@param  str  Description of Parameter
662    */
663   public final void appendErrorOut(String str) {
664     try
665     {
666       errorOut.write(str);
667     }
668     catch (IOException ioe)
669     {
670       ioe.printStackTrace();
671     }
672   }
673 
674 
675   /**
676    *  Description of the Method
677    *
678    *@param  str  Description of Parameter
679    */
680   public final void appendLineErrorOut(String str) {
681     try
682     {
683       errorOut.write(str);
684       errorOut.write(lineSeparator);
685     }
686     catch (IOException ioe)
687     {
688       ioe.printStackTrace();
689     }
690   }
691 
692 
693   /**
694    *  Description of the Method
695    */
696   public final void flushBuffers() {
697     try
698     {
699       resultOut.flush();
700       errorOut.flush();
701     }
702     catch (IOException ioe)
703     {
704       ioe.printStackTrace();
705     }
706   }
707 
708 
709   /**
710    *  Description of the Method
711    */
712   public final void incIOCounter() {
713     IOCounter++;
714   }
715 
716 
717   /**
718    *  Description of the Method
719    */
720   public final void clearIOIn() {
721     IOIn.clear();
722   }
723 
724 
725   /**
726    *  Description of the Method
727    */
728   public final void clearIOOut() {
729     IOOut.clear();
730   }
731 
732 
733   /**
734    *  Description of the Method
735    *
736    *@param  num  Description of Parameter
737    *@param  obj  Description of Parameter
738    */
739   public final void putIOIn(Integer num, HSymbol obj) {
740     IOIn.put(num, obj);
741   }
742 
743 
744   /**
745    *  Description of the Method
746    *
747    *@param  str  Description of Parameter
748    *@param  num  Description of Parameter
749    */
750   public final void putIOOut(String str, Short num) {
751     IOIn.put(str, num);
752   }
753 
754 
755   /**
756    *  Adds a symbol to the SymbolData attribute of the S object
757    *
758    *@param  sym  The symbol to be added to the SymbolData attribute
759    *@return      Description of the Returned Value
760    */
761   public final HSymbol addSymbolData(HSymbol sym) {
762     HSymbolData symData = (HSymbolData) SymbolDataSet.get(sym);
763     if (symData != null)
764     {
765       return symData.getSymbol();
766     }
767     if (sym.getBuiltinFunction() == C.loadBeanShell)
768     {
769       SymbolDataSet.put(sym,
770                         new HSymbolFunctionData(sym,
771                                                 HSymbol.NOATTRIBUTE,
772                                                 C.loadBeanShell));
773     }
774     else
775     {
776       SymbolDataSet.put(sym, new HSymbolData(sym, HSymbol.NOATTRIBUTE));
777     }
778     ELoadBeanShell.loadInitialBeanShell(sym, this);
779     return sym;
780   }
781 
782 
783   /**
784    *  Adds a feature to the SymbolData attribute of the S object
785    *
786    *@param  sym         The feature to be added to the SymbolData attribute
787 
788    *@return             Description of the Returned Value
789   /**
790    *  Adds a symbol to the SymbolData attribute of the S object
791    *
792    *@param  sym  The symbol to be added to the SymbolData attribute
793    *@param  attributes  The attributes to be added to the SymbolData attribute
794    *@return      Description of the Returned Value
795    */
796   public final HSymbol addSymbolData(HSymbol sym, int attributes) {
797     HSymbolData symData = (HSymbolData) SymbolDataSet.get(sym);
798     if (symData != null)
799     {
800       symData.setAttributes(attributes);
801       return symData.getSymbol();
802     }
803     SymbolDataSet.put(sym, new HSymbolData(sym, attributes));
804     ELoadBeanShell.loadInitialBeanShell(sym, this);
805     return sym;
806   }
807 
808 
809   /**
810    *  Description of the Method
811    *
812    *@param  sym  Description of Parameter
813    *@return      Description of the Returned Value
814    */
815   public final boolean hasNoLocalVar(HSymbol sym) {
816     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
817     if (dSet != null)
818     {
819       return dSet.hasNoLocalVar();
820     }
821     return true;
822   }
823 
824 
825   /**
826    *  Description of the Method
827    *
828    *@param  sym  Description of Parameter
829    *@param  obj  Description of Parameter
830    */
831   public final void createLocalVar(HSymbol sym, HObject obj) {
832     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
833     if (dSet == null)
834     {
835       dSet = new HSymbolData(sym);
836       SymbolDataSet.put(sym, dSet);
837     }
838     dSet.createLocalVar(obj);
839   }
840 
841 
842   /**
843    *  Description of the Method
844    *
845    *@param  sym  Description of Parameter
846    *@return      Description of the Returned Value
847    */
848   public final HObject deleteLocalVar(HSymbol sym) {
849     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
850     if (dSet != null)
851     {
852       return dSet.deleteLocalVar();
853     }
854     return null;
855   }
856 
857 
858   /**
859    *  Description of the Method
860    *
861    *@param  sym  Description of Parameter
862    *@return      Description of the Returned Value
863    */
864   public final boolean hasNoLocalPattern(HSymbol sym) {
865     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
866     if (dSet != null)
867     {
868       return dSet.hasNoLocalPattern();
869     }
870     return true;
871   }
872 
873 
874   /**
875    *  Description of the Method
876    *
877    *@param  sym  Description of Parameter
878    *@param  obj  Description of Parameter
879    */
880   public final void createLocalPattern(HSymbol sym, HObject obj) {
881     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
882     if (dSet == null)
883     {
884       dSet = new HSymbolData(sym);
885       SymbolDataSet.put(sym, dSet);
886     }
887     dSet.createLocalPattern(obj);
888   }
889 
890 
891   /**
892    *  Description of the Method
893    *
894    *@param  sym  Description of Parameter
895    *@return      Description of the Returned Value
896    */
897   public final HObject deleteLocalPattern(HSymbol sym) {
898     HSymbolData dSet = (HSymbolData) SymbolDataSet.get(sym);
899     if (dSet != null)
900     {
901       return dSet.deleteLocalPattern();
902     }
903     return null;
904   }
905 
906 
907   /**
908    *  Description of the Method
909    *
910    *@param  obj  Description of Parameter
911    *@return      Description of the Returned Value
912    */
913   public HObject ELoop(HObject obj) {
914     //SessionData sd = SessionData.currentSessionData();
915     HFunction localTrace = null;
916     if (C.DEBUG)
917     {
918       if (obj == null)
919       {
920         appendErrorOut("S.ELoop(): obj == null");
921         throw new RuntimeException("ELoop");
922         //          throw new UnsupportedOperationException();
923       }
924     }
925     if (recursionExceeded())
926     {
927       incRecursionCounter();
928       appendResultOut("RecursionLimit exceeded:");
929       appendResultOut(obj.toString());
930       appendResultOut("\n");
931       throw new HThrowException(C.Error, C.Error);
932       //return C.Hold.f( obj );
933     }
934 
935     HObject result = obj.evaluate(this);
936 
937     if (result == null)
938     {
939       decRecursionCounter();
940 
941       return null;
942     }
943 
944     if (traceEval)
945     {
946       localTrace = C.List.f();
947       if (lastTraceList == null)
948       {
949         localTrace.add(C.HoldForm.f(obj));
950       }
951       else
952       {
953         localTrace.add(C.HoldForm.f(lastTraceList));
954         lastTraceList = null;
955       }
956       localTrace.add(C.HoldForm.f(result));
957       //traceList.add(localTrace);
958     }
959 
960     HObject temp = result.evaluate(this);
961     setIterationCounter(0);
962     while (temp != null)
963     {
964       if (iterationExceeded())
965       {
966         decIterationCounter();
967         decRecursionCounter();
968         appendResultOut("IterationLimit exceeded:");
969         appendResultOut(temp.toString());
970         appendResultOut("\n");
971         throw new HThrowException(C.Error, C.Error);
972         //return C.Hold.f( temp );
973       }
974       if (traceEval)
975       {
976         //        localTrace.add(C.HoldForm.f(temp));
977         if (lastTraceList != null)
978         {
979           localTrace.add(C.HoldForm.f(lastTraceList));
980           lastTraceList = null;
981         }
982         else
983         {
984           localTrace.add(C.HoldForm.f(temp));
985         }
986       }
987 
988       result = temp;
989       temp = result.evaluate(this);
990     }
991 
992     decRecursionCounter();
993     if (traceEval)
994     {
995       lastTraceList = localTrace;
996     }
997 
998     return result;
999   }
1000
1001
1002  /**
1003   *  Symbolic evaluation
1004   *
1005   *@param  obj
1006   *@return
1007   *@see
1008   */
1009  public final HObject EV(HObject obj) {
1010    HObject result;
1011    if (C.DEBUG)
1012    {
1013      //SessionData sd = SessionData.currentSessionData();
1014      try
1015      {
1016        return ((result = ELoop(obj)) == null) ? obj : result;
1017      }
1018      catch (RuntimeException se)
1019      {
1020        se.printStackTrace();
1021        appendErrorOut(obj.toString());
1022      }
1023      return null;
1024    }
1025    else
1026    {
1027      return ((result = ELoop(obj)) == null) ? obj : result;
1028    }
1029  }
1030
1031
1032  /**
1033   *  Numerical evaluation [sets numericFlag=true]
1034   *
1035   *@param  obj
1036   *@return
1037   *@see
1038   */
1039  public final HObject NEV(HObject obj) {
1040    // Context ct = ((ContextThread) Thread.currentThread()).getContext();
1041    boolean flag = numericFlag;
1042    numericFlag = true;
1043
1044    HObject temp = EV(obj);
1045
1046    numericFlag = flag;
1047    return temp;
1048  }
1049
1050  public String interpreter(String expr) {
1051    return interpreter(expr, null, false);
1052  }
1053  
1054  public String interpreter(String expr, boolean quietMode) {
1055    return interpreter(expr, null, quietMode);
1056  }
1057
1058
1059  /**
1060    *
1061   */
1062  public String interpreter(String expr,
1063                            HFunction conv,
1064                            boolean quietMode) {
1065    HObject res = objectInterpreter(expr, conv, false, quietMode);
1066    StringBuffer strBuf = null;
1067    String strResult = null;
1068    try
1069    {
1070      if (res == null)
1071      {
1072        return null;
1073      }
1074      if (!res.equals(C.Null))
1075      {
1076        if (!quietMode)
1077        {
1078          appendLineResultOut(
1079            "Out[" +
1080            java.lang.Integer.toString(getCounter(), 10) +
1081            "]:"
1082          );
1083        }
1084        strBuf = new StringBuffer(100);
1085        res.toStringBuffer(strBuf, this);
1086        strResult = strBuf.toString();
1087        if (!quietMode)
1088        {
1089          appendLineResultOut(strResult);
1090        }
1091        return strResult;
1092      }
1093      return null;
1094    }
1095    catch (Throwable se)
1096    {
1097      appendErrorOut("Unknown Exception occured: " + se.getMessage());
1098      if (C.DEBUG)
1099      {
1100        se.printStackTrace();
1101      }
1102    }
1103    return null;
1104  }
1105
1106
1107  /**
1108   *  evaluate the expression
1109   *
1110   *@param  expression     a HartMath expression
1111   *@param  convertResult  Description of Parameter
1112   *@return                the evaluated HartMath object
1113   */
1114  public HObject objectInterpreter(String expression, 
1115                                   boolean convertResult, 
1116                                   boolean quietMode) {
1117    return objectInterpreter(expression, null, convertResult, quietMode);
1118  }
1119
1120
1121  /**
1122   *  evaluate the expression
1123   *
1124   *@param  expression          a HartMath expression
1125   *@param  conversionFunction  a Function which converts the result (ex.
1126   *      ToXML(), ToJava())
1127   *@param  convertResult       Description of Parameter
1128   *@return                     the evaluated HartMath object
1129   */
1130  public HObject objectInterpreter(String expression, 
1131                                   HFunction conversionFunction, 
1132                                   boolean convertResult,
1133                                   boolean quietMode) {
1134    return objectInterpreter(expression, conversionFunction, false, convertResult, quietMode);
1135  }
1136
1137
1138  /**
1139   *  evaluate the expression
1140   *
1141   *@param  conv           a Function which converts the result (ex. ToXML(),
1142   *      ToJava())
1143   *@param  rethrowError   if true throws the syntax error exception again
1144   *@param  expr           Description of Parameter
1145   *@param  convertResult  Description of Parameter
1146   *@param  quietMode      if true the interpreter method produces no output
1147   *@return                the evaluated HartMath object
1148   */
1149  public HObject objectInterpreter(String expr,
1150                                   HFunction conv,
1151                                   boolean rethrowError,
1152                                   boolean convertResult,
1153                                   boolean quietMode) {
1154    StringBuffer strBuf = null;
1155    if (!quietMode)
1156    {
1157      addHistory();
1158    }
1159
1160    try
1161    {
1162      Parser scan = new Parser(expr, this);
1163      HObject obj = scan.start();
1164      if (conv != null && convertResult == false)
1165      {
1166        HFunction tmp = (HFunction) conv.clone();
1167        tmp.add(obj);
1168        obj = tmp;
1169      }
1170
1171      setRecursionLimit(((HSignedNumber) EV(C.RecursionLimit)).intValue());
1172      setIterationLimit(((HSignedNumber) EV(C.IterationLimit)).intValue());
1173      //    setRecursionLimit( -1 );
1174      //    setIterationLimit( -1 );
1175      // C.InArray.set(C.count - 1, obj);
1176      if (!quietMode)
1177      {
1178        changeCurrentInHistory(obj);
1179        appendResultOut(
1180          "In[" +
1181          java.lang.Integer.toString(getCounter(), 10) +
1182          "]:"
1183        );
1184      }
1185      if (obj != null)
1186      {
1187        strBuf = new StringBuffer(100);
1188        obj.toStringBuffer(strBuf, this);
1189        if (!quietMode)
1190        {
1191          appendLineResultOut(strBuf.toString());
1192        }
1193      }
1194
1195      setNumericFlag(false);
1196      setRecursionCounter(0);
1197      setIterationCounter(0);
1198      //C.initLimits();
1199
1200      HObject res = EV(obj);
1201      if (res != null)
1202      {
1203        if (conv != null && convertResult == true)
1204        {
1205          HFunction tmp = (HFunction) conv.clone();
1206          tmp.add(res);
1207          res = EV(tmp);
1208        }
1209      }
1210
1211      // C.OutArray.set(C.count - 1, res);
1212      if (!quietMode)
1213      {
1214        changeCurrentOutHistory(res);
1215      }
1216      return res;
1217    }
1218    catch (SyntaxError se)
1219    {
1220      if (rethrowError)
1221      {
1222        throw se;
1223      }
1224      appendErrorOut("Syntax Error: " + se.getMessage());
1225
1226      if (C.DEBUG)
1227      {
1228        se.printStackTrace();
1229      }
1230    }
1231    catch (HBreakException se)
1232    {
1233      appendErrorOut("Break() occured outside For, While, Switch");
1234
1235      if (C.DEBUG)
1236      {
1237        se.printStackTrace();
1238      }
1239    }
1240    catch (HContinueException se)
1241    {
1242      appendErrorOut("Continue() occured outside For, While");
1243
1244      if (C.DEBUG)
1245      {
1246        se.printStackTrace();
1247      }
1248    }
1249    catch (HReturnException se)
1250    {
1251      appendErrorOut("Return() occured");
1252
1253      if (C.DEBUG)
1254      {
1255        se.printStackTrace();
1256      }
1257    }
1258    catch (HThrowException se)
1259    {
1260      appendErrorOut("Throw::" + se.why().toString());
1261
1262      if (C.DEBUG)
1263      {
1264        se.printStackTrace();
1265      }
1266    }
1267    catch (Throwable se)
1268    {
1269      appendErrorOut("Unknown Exception occured: " + se.getMessage());
1270      if (C.DEBUG)
1271      {
1272        se.printStackTrace();
1273      }
1274    }
1275    return null;
1276  }
1277}