From 3320e1496b1d099f63dea869f4a1cc19630dc3a9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 24 Nov 2014 15:51:08 -0700 Subject: Fixed an annoying parsing bug. --- src/main.cpp | 2 +- src/number.cpp | 4 +++- src/parser.cpp | 21 +++++++++++++++++++-- src/parser.h | 2 ++ src/unitparser.cpp | 16 ++++++++++++++++ src/unitparser.h | 1 + 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[] ) { Options opt( argc, argv ); - println("CliC - 0.09"); + println("CliC - 0.10"); println("Try \\exit, \\help, \\scale, and \\radix commands."); println(""); 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 if( rhs.isZero() ) { q = rhs; + // This should be infinity (or negative infinity?) + // but we don't support that yet. Still have to figure that bit out. r.set( 0 ); return; } @@ -416,6 +418,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const r.set( 0 ); return; } + DBS( DIVIDE, Bu::println("divide: %1 / %2").arg( *this ).arg( rhs ) ); // 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 DBS( DIVIDE, Bu::println("Quotient: %1").arg( q ) ); DBS( DIVIDE, Bu::println("Final numerator? %1").arg( nNum ) ); DBS( DIVIDE, Bu::println("Remainder? %1").arg( r ) ); - } bool 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() switch( t.eType ) { case Token::tEndOfInput: + while( !tsNonTerminal.isEmpty() ) + unwind(); return; case Token::tEndOfLine: - unwind(); + while( !tsNonTerminal.isEmpty() ) + unwind(); if( !tsTerminal.isEmpty() ) { Bu::println( rOut, "%1").arg( deref( tsTerminal.peek() ) ); @@ -176,6 +179,16 @@ void Parser::parse() } } +Number Parser::getVariable( const Bu::String &sName ) +{ + return hVars.get( sName ); +} + +void Parser::setVariable( const Bu::String &sName, const Number &rValue ) +{ + hVars.insert( sName, rValue ); +} + void Parser::unwind() { for(;;) @@ -200,7 +213,7 @@ void Parser::unwind() { return; } - + Token t = tsNonTerminal.peekPop(); switch( t.eType ) { @@ -312,6 +325,10 @@ void Parser::unwind() // These should never show up at all break; } + if( !tsNonTerminal.isEmpty() && + getPriority( tsNonTerminal.peek().eType ) < + getPriority( t.eType ) ) + return; } } 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: virtual ~Parser(); void parse(); + Number getVariable( const Bu::String &sName ); + void setVariable( const Bu::String &sName, const Number &rValue ); private: 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() setName("Parser"); add( static_cast(&UnitParser::order1), "order1", Bu::UnitSuite::expectPass ); + add( static_cast(&UnitParser::assignment), + "assignment", Bu::UnitSuite::expectPass ); } UnitParser::~UnitParser() @@ -35,5 +37,19 @@ void UnitParser::order1() unitTest(parse("(2+3)*5") == "25"); unitTest(parse("1.59*40/24*21", 5) == "55.65"); unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); + unitTest(parse("10+2*2*2+2") == "20"); +} + +void UnitParser::assignment() +{ + Bu::MemBuf mbIn("$test = 2*2*2"); + Bu::MemBuf mbOut; + Lexer lex( mbIn ); + lex.setScale( 0 ); + Parser parser( lex, mbOut ); + parser.parse(); + Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() ) + .arg( parser.getVariable("test") ); + unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() ); } 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: virtual ~UnitParser(); void order1(); + void assignment(); }; #endif -- cgit v1.2.3