summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2014-11-24 15:51:08 -0700
committerMike Buland <mike@xagasoft.com>2014-11-24 15:51:08 -0700
commit3320e1496b1d099f63dea869f4a1cc19630dc3a9 (patch)
treec56f8262951481376122650e9bc1c49358e304f1
parentf2f92156531b49da71067336ef0724f411226644 (diff)
downloadclic-3320e1496b1d099f63dea869f4a1cc19630dc3a9.tar.gz
clic-3320e1496b1d099f63dea869f4a1cc19630dc3a9.tar.bz2
clic-3320e1496b1d099f63dea869f4a1cc19630dc3a9.tar.xz
clic-3320e1496b1d099f63dea869f4a1cc19630dc3a9.zip
Fixed an annoying parsing bug.0.10
-rw-r--r--src/main.cpp2
-rw-r--r--src/number.cpp4
-rw-r--r--src/parser.cpp21
-rw-r--r--src/parser.h2
-rw-r--r--src/unitparser.cpp16
-rw-r--r--src/unitparser.h1
6 files changed, 42 insertions, 4 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 3665fc4..a0faa6a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,7 +17,7 @@ int main( int argc, char *argv[] )
17 { 17 {
18 Options opt( argc, argv ); 18 Options opt( argc, argv );
19 19
20 println("CliC - 0.09"); 20 println("CliC - 0.10");
21 println("Try \\exit, \\help, \\scale, and \\radix commands."); 21 println("Try \\exit, \\help, \\scale, and \\radix commands.");
22 println(""); 22 println("");
23 23
diff --git a/src/number.cpp b/src/number.cpp
index 0f99792..8fc1d7f 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -407,6 +407,8 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
407 if( rhs.isZero() ) 407 if( rhs.isZero() )
408 { 408 {
409 q = rhs; 409 q = rhs;
410 // This should be infinity (or negative infinity?)
411 // but we don't support that yet. Still have to figure that bit out.
410 r.set( 0 ); 412 r.set( 0 );
411 return; 413 return;
412 } 414 }
@@ -416,6 +418,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
416 r.set( 0 ); 418 r.set( 0 );
417 return; 419 return;
418 } 420 }
421
419 DBS( DIVIDE, Bu::println("divide: %1 / %2").arg( *this ).arg( rhs ) ); 422 DBS( DIVIDE, Bu::println("divide: %1 / %2").arg( *this ).arg( rhs ) );
420 423
421 // iNumShift is how many digits we've shifted the entire equation, 424 // iNumShift is how many digits we've shifted the entire equation,
@@ -532,7 +535,6 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
532 DBS( DIVIDE, Bu::println("Quotient: %1").arg( q ) ); 535 DBS( DIVIDE, Bu::println("Quotient: %1").arg( q ) );
533 DBS( DIVIDE, Bu::println("Final numerator? %1").arg( nNum ) ); 536 DBS( DIVIDE, Bu::println("Final numerator? %1").arg( nNum ) );
534 DBS( DIVIDE, Bu::println("Remainder? %1").arg( r ) ); 537 DBS( DIVIDE, Bu::println("Remainder? %1").arg( r ) );
535
536} 538}
537 539
538bool Number::isZero() const 540bool Number::isZero() const
diff --git a/src/parser.cpp b/src/parser.cpp
index 8675a7c..f97078a 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -25,10 +25,13 @@ void Parser::parse()
25 switch( t.eType ) 25 switch( t.eType )
26 { 26 {
27 case Token::tEndOfInput: 27 case Token::tEndOfInput:
28 while( !tsNonTerminal.isEmpty() )
29 unwind();
28 return; 30 return;
29 31
30 case Token::tEndOfLine: 32 case Token::tEndOfLine:
31 unwind(); 33 while( !tsNonTerminal.isEmpty() )
34 unwind();
32 if( !tsTerminal.isEmpty() ) 35 if( !tsTerminal.isEmpty() )
33 { 36 {
34 Bu::println( rOut, "%1").arg( deref( tsTerminal.peek() ) ); 37 Bu::println( rOut, "%1").arg( deref( tsTerminal.peek() ) );
@@ -176,6 +179,16 @@ void Parser::parse()
176 } 179 }
177} 180}
178 181
182Number Parser::getVariable( const Bu::String &sName )
183{
184 return hVars.get( sName );
185}
186
187void Parser::setVariable( const Bu::String &sName, const Number &rValue )
188{
189 hVars.insert( sName, rValue );
190}
191
179void Parser::unwind() 192void Parser::unwind()
180{ 193{
181 for(;;) 194 for(;;)
@@ -200,7 +213,7 @@ void Parser::unwind()
200 { 213 {
201 return; 214 return;
202 } 215 }
203 216
204 Token t = tsNonTerminal.peekPop(); 217 Token t = tsNonTerminal.peekPop();
205 switch( t.eType ) 218 switch( t.eType )
206 { 219 {
@@ -312,6 +325,10 @@ void Parser::unwind()
312 // These should never show up at all 325 // These should never show up at all
313 break; 326 break;
314 } 327 }
328 if( !tsNonTerminal.isEmpty() &&
329 getPriority( tsNonTerminal.peek().eType ) <
330 getPriority( t.eType ) )
331 return;
315 } 332 }
316} 333}
317 334
diff --git a/src/parser.h b/src/parser.h
index 2b1a4af..e163212 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -20,6 +20,8 @@ public:
20 virtual ~Parser(); 20 virtual ~Parser();
21 21
22 void parse(); 22 void parse();
23 Number getVariable( const Bu::String &sName );
24 void setVariable( const Bu::String &sName, const Number &rValue );
23 25
24private: 26private:
25 void unwind(); 27 void unwind();
diff --git a/src/unitparser.cpp b/src/unitparser.cpp
index 43dacfc..6a5dcf4 100644
--- a/src/unitparser.cpp
+++ b/src/unitparser.cpp
@@ -11,6 +11,8 @@ UnitParser::UnitParser()
11 setName("Parser"); 11 setName("Parser");
12 add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1), 12 add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1),
13 "order1", Bu::UnitSuite::expectPass ); 13 "order1", Bu::UnitSuite::expectPass );
14 add( static_cast<Bu::UnitSuite::Test>(&UnitParser::assignment),
15 "assignment", Bu::UnitSuite::expectPass );
14} 16}
15 17
16UnitParser::~UnitParser() 18UnitParser::~UnitParser()
@@ -35,5 +37,19 @@ void UnitParser::order1()
35 unitTest(parse("(2+3)*5") == "25"); 37 unitTest(parse("(2+3)*5") == "25");
36 unitTest(parse("1.59*40/24*21", 5) == "55.65"); 38 unitTest(parse("1.59*40/24*21", 5) == "55.65");
37 unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); 39 unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619");
40 unitTest(parse("10+2*2*2+2") == "20");
41}
42
43void UnitParser::assignment()
44{
45 Bu::MemBuf mbIn("$test = 2*2*2");
46 Bu::MemBuf mbOut;
47 Lexer lex( mbIn );
48 lex.setScale( 0 );
49 Parser parser( lex, mbOut );
50 parser.parse();
51 Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() )
52 .arg( parser.getVariable("test") );
53 unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() );
38} 54}
39 55
diff --git a/src/unitparser.h b/src/unitparser.h
index 4f9bc33..744deef 100644
--- a/src/unitparser.h
+++ b/src/unitparser.h
@@ -10,6 +10,7 @@ public:
10 virtual ~UnitParser(); 10 virtual ~UnitParser();
11 11
12 void order1(); 12 void order1();
13 void assignment();
13}; 14};
14 15
15#endif 16#endif