From abe7e449143052b07fae688da690c2a7d387a291 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 14 Nov 2013 15:35:47 -0700 Subject: Fixed (?) parsing order of operations bug, added unit tests. --- src/options.cpp | 2 ++ src/parser.cpp | 4 ++-- src/unitparser.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/unitparser.h | 15 +++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/unitparser.cpp create mode 100644 src/unitparser.h diff --git a/src/options.cpp b/src/options.cpp index 7fbd70e..6e264f9 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -1,6 +1,7 @@ #include "options.h" #include "unitnumber.h" +#include "unitparser.h" #include "number.h" #include @@ -37,6 +38,7 @@ Options::~Options() int Options::selfTest( Bu::StringArray ) { UnitNumber().run(); + UnitParser().run(); exit( 0 ); return 0; diff --git a/src/parser.cpp b/src/parser.cpp index 643b7bb..cf3bb3f 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -5,7 +5,7 @@ #include #include -//#define DEBUG +#define DEBUG Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : lex( lex ), @@ -155,7 +155,7 @@ void Parser::parse() default: if( tsNonTerminal.getSize() == 0 || - getPriority( tsNonTerminal.peek().eType ) <= + getPriority( tsNonTerminal.peek().eType ) < getPriority( t.eType ) ) { #ifdef DEBUG diff --git a/src/unitparser.cpp b/src/unitparser.cpp new file mode 100644 index 0000000..891f7b3 --- /dev/null +++ b/src/unitparser.cpp @@ -0,0 +1,39 @@ +#include "unitparser.h" +#include "parser.h" +#include "lexer.h" + +#include +#include +#include + +UnitParser::UnitParser() +{ + setName("Parser"); + add( static_cast(&UnitParser::order1), + "order1", Bu::UnitSuite::expectPass ); +} + +UnitParser::~UnitParser() +{ +} + +Bu::String parse( const Bu::String sEq, int iScale=0, int iRadix=10 ) +{ + Bu::MemBuf mbIn( sEq ); + Bu::MemBuf mbOut; + Lexer lex( mbIn ); + lex.setScale( 5 ); + Parser parser( lex, mbOut ); + parser.parse(); + return mbOut.getString().trimWhitespace(); +} + +void UnitParser::order1() +{ + unitTest(parse("2+3*5") == "17"); + unitTest(parse("2+3-5") == "0"); + unitTest(parse("(2+3)*5") == "25"); + unitTest(parse("1.59*40/24*21", 5) == "55.125"); + unitTest(parse("1.59*40/(24*21)", 5) == "0.125"); // bc says it's this: "0.12619"); +} + diff --git a/src/unitparser.h b/src/unitparser.h new file mode 100644 index 0000000..4f9bc33 --- /dev/null +++ b/src/unitparser.h @@ -0,0 +1,15 @@ +#ifndef UNIT_PARSER_H +#define UNIT_PARSER_H + +#include + +class UnitParser : public Bu::UnitSuite +{ +public: + UnitParser(); + virtual ~UnitParser(); + + void order1(); +}; + +#endif -- cgit v1.2.3