aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-23 22:02:15 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-23 22:02:15 +0000
commitd4cf48613dd9b1338e174edbf4f47a7736bb2505 (patch)
tree404daa83485d75325ef76019e557e5203ba522d4 /src
parentb94a6a42c1024c00fab7b678cccaecfaa7604d56 (diff)
downloadlibbu++-d4cf48613dd9b1338e174edbf4f47a7736bb2505.tar.gz
libbu++-d4cf48613dd9b1338e174edbf4f47a7736bb2505.tar.bz2
libbu++-d4cf48613dd9b1338e174edbf4f47a7736bb2505.tar.xz
libbu++-d4cf48613dd9b1338e174edbf4f47a7736bb2505.zip
Fixed a peculiar formula residue bug...eeew, formula residue. Also added a
clear function to Bu::Stack
Diffstat (limited to 'src')
-rw-r--r--src/formula.h21
-rw-r--r--src/stack.h6
-rw-r--r--src/tests/formula.cpp27
3 files changed, 47 insertions, 7 deletions
diff --git a/src/formula.h b/src/formula.h
index d1e77e3..20ffa5b 100644
--- a/src/formula.h
+++ b/src/formula.h
@@ -60,6 +60,14 @@ namespace Bu
60 60
61 prec run( const Bu::FString &sFormulaSrc ) 61 prec run( const Bu::FString &sFormulaSrc )
62 { 62 {
63 /*
64 if( !sOper.isEmpty() )
65 printf("sOper.isEmpty() == false!\n");
66 if( !sValue.isEmpty() )
67 printf("sValue.isEmpty() == false!\n");
68 if( !sFunc.isEmpty() )
69 printf("sFunc.isEmpty() == false!\n");
70 */
63 const char *sFormula = sFormulaSrc.getStr(); 71 const char *sFormula = sFormulaSrc.getStr();
64 for(;;) 72 for(;;)
65 { 73 {
@@ -91,8 +99,11 @@ namespace Bu
91 if( tOpr == symEOS ) 99 if( tOpr == symEOS )
92 { 100 {
93 reduce(); 101 reduce();
94 return sValue.top(); 102 prec ret = sValue.top();
95 break; 103 sValue.clear();
104 sFunc.clear();
105 sOper.clear();
106 return ret;
96 } 107 }
97 if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) ) 108 if( !sOper.isEmpty() && getPrec( sOper.top() ) > getPrec( tOpr ) )
98 { 109 {
@@ -108,7 +119,11 @@ namespace Bu
108 goto oppart; 119 goto oppart;
109 } 120 }
110 } 121 }
111 return sValue.top(); 122 prec ret = sValue.top();
123 sValue.clear();
124 sFunc.clear();
125 sOper.clear();
126 return ret;
112 } 127 }
113 128
114 varHash hVars; 129 varHash hVars;
diff --git a/src/stack.h b/src/stack.h
index 99ce6e8..bbc12f4 100644
--- a/src/stack.h
+++ b/src/stack.h
@@ -64,6 +64,12 @@ namespace Bu
64 return ret; 64 return ret;
65 } 65 }
66 66
67 void clear()
68 {
69 while( !isEmpty() )
70 pop();
71 }
72
67 bool isEmpty() 73 bool isEmpty()
68 { 74 {
69 return pTop == NULL; 75 return pTop == NULL;
diff --git a/src/tests/formula.cpp b/src/tests/formula.cpp
index b3b0d5c..40b10a5 100644
--- a/src/tests/formula.cpp
+++ b/src/tests/formula.cpp
@@ -18,7 +18,7 @@ int main( int argc, char *argv[] )
18 Bu::Formula<uint32_t> uForm; 18 Bu::Formula<uint32_t> uForm;
19 Bu::Formula<double> dForm; 19 Bu::Formula<double> dForm;
20 20
21 class CeilFunc : public Bu::Formula<double>::Func 21 class DblCeilFunc : public Bu::Formula<double>::Func
22 { 22 {
23 public: 23 public:
24 virtual double operator()( double x ) 24 virtual double operator()( double x )
@@ -27,10 +27,29 @@ int main( int argc, char *argv[] )
27 } 27 }
28 }; 28 };
29 29
30 dForm.hFunc.insert( "ceil", new CeilFunc() ); 30 dForm.hFunc.insert( "ceil", new DblCeilFunc() );
31
32 class IntCeilFunc : public Bu::Formula<uint32_t>::Func
33 {
34 public:
35 virtual uint32_t operator()( uint32_t x )
36 {
37 return x;
38 }
39 };
31 40
32// printf("u: %s = %u\n", argv[1], uForm.run( argv[1] ) ); 41 uForm.hFunc.insert( "ceil", new IntCeilFunc() );
33 printf("d: %s = %f\n", argv[1], dForm.run( argv[1] ) ); 42
43 uForm.hVars.insert("x", 10 );
44 dForm.hVars.insert("x", 10.00 );
45 uForm.hVars.insert("y", 10 );
46 dForm.hVars.insert("y", 10.00 );
47
48 for( int j = 1; j < argc; j++ )
49 {
50 printf("u: %s = %u\n", argv[j], uForm.run( argv[j] ) );
51 printf("d: %s = %f\n", argv[j], dForm.run( argv[j] ) );
52 }
34 53
35 return 0; 54 return 0;
36} 55}