summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-11-14 15:35:47 -0700
committerMike Buland <mike@xagasoft.com>2013-11-14 15:35:47 -0700
commitabe7e449143052b07fae688da690c2a7d387a291 (patch)
tree00742d14bce96777ce65589f9abeeca4cdadbc25
parentbc0484899acb3495f95d7400ff2eb804ae580096 (diff)
downloadclic-abe7e449143052b07fae688da690c2a7d387a291.tar.gz
clic-abe7e449143052b07fae688da690c2a7d387a291.tar.bz2
clic-abe7e449143052b07fae688da690c2a7d387a291.tar.xz
clic-abe7e449143052b07fae688da690c2a7d387a291.zip
Fixed (?) parsing order of operations bug, added unit tests.
-rw-r--r--src/options.cpp2
-rw-r--r--src/parser.cpp4
-rw-r--r--src/unitparser.cpp39
-rw-r--r--src/unitparser.h15
4 files changed, 58 insertions, 2 deletions
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 @@
1#include "options.h" 1#include "options.h"
2 2
3#include "unitnumber.h" 3#include "unitnumber.h"
4#include "unitparser.h"
4#include "number.h" 5#include "number.h"
5 6
6#include <bu/sio.h> 7#include <bu/sio.h>
@@ -37,6 +38,7 @@ Options::~Options()
37int Options::selfTest( Bu::StringArray ) 38int Options::selfTest( Bu::StringArray )
38{ 39{
39 UnitNumber().run(); 40 UnitNumber().run();
41 UnitParser().run();
40 42
41 exit( 0 ); 43 exit( 0 );
42 return 0; 44 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 @@
5#include <bu/sio.h> 5#include <bu/sio.h>
6#include <stdlib.h> 6#include <stdlib.h>
7 7
8//#define DEBUG 8#define DEBUG
9 9
10Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : 10Parser::Parser( Lexer &lex, Bu::Stream &rOut ) :
11 lex( lex ), 11 lex( lex ),
@@ -155,7 +155,7 @@ void Parser::parse()
155 155
156 default: 156 default:
157 if( tsNonTerminal.getSize() == 0 || 157 if( tsNonTerminal.getSize() == 0 ||
158 getPriority( tsNonTerminal.peek().eType ) <= 158 getPriority( tsNonTerminal.peek().eType ) <
159 getPriority( t.eType ) ) 159 getPriority( t.eType ) )
160 { 160 {
161#ifdef DEBUG 161#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 @@
1#include "unitparser.h"
2#include "parser.h"
3#include "lexer.h"
4
5#include <bu/sio.h>
6#include <bu/streamstack.h>
7#include <bu/membuf.h>
8
9UnitParser::UnitParser()
10{
11 setName("Parser");
12 add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1),
13 "order1", Bu::UnitSuite::expectPass );
14}
15
16UnitParser::~UnitParser()
17{
18}
19
20Bu::String parse( const Bu::String sEq, int iScale=0, int iRadix=10 )
21{
22 Bu::MemBuf mbIn( sEq );
23 Bu::MemBuf mbOut;
24 Lexer lex( mbIn );
25 lex.setScale( 5 );
26 Parser parser( lex, mbOut );
27 parser.parse();
28 return mbOut.getString().trimWhitespace();
29}
30
31void UnitParser::order1()
32{
33 unitTest(parse("2+3*5") == "17");
34 unitTest(parse("2+3-5") == "0");
35 unitTest(parse("(2+3)*5") == "25");
36 unitTest(parse("1.59*40/24*21", 5) == "55.125");
37 unitTest(parse("1.59*40/(24*21)", 5) == "0.125"); // bc says it's this: "0.12619");
38}
39
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 @@
1#ifndef UNIT_PARSER_H
2#define UNIT_PARSER_H
3
4#include <bu/unitsuite.h>
5
6class UnitParser : public Bu::UnitSuite
7{
8public:
9 UnitParser();
10 virtual ~UnitParser();
11
12 void order1();
13};
14
15#endif