summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2014-11-28 13:45:37 -0700
committerMike Buland <mike@xagasoft.com>2014-11-28 13:45:37 -0700
commit9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff (patch)
tree561bd5cb20d17c0c5e5289763d140e42cdde8a3f /src
parent02573826558bd44f0ec3ed542964be0096d5e389 (diff)
downloadclic-9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff.tar.gz
clic-9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff.tar.bz2
clic-9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff.tar.xz
clic-9f7cbe624a91008a4cbe47c7b369c5e3f96a4eff.zip
Division fixes, better config, more tests.0.11
Other minor fixes and options such as --version being added.
Diffstat (limited to 'src')
-rw-r--r--src/config.h8
-rw-r--r--src/main.cpp3
-rw-r--r--src/number.cpp2
-rw-r--r--src/options.cpp12
-rw-r--r--src/options.h1
-rw-r--r--src/parser.cpp20
-rw-r--r--src/unitnumber.cpp2
7 files changed, 36 insertions, 12 deletions
diff --git a/src/config.h b/src/config.h
index 00fb067..a3f8b9e 100644
--- a/src/config.h
+++ b/src/config.h
@@ -1,8 +1,14 @@
1#ifndef CONFIG_H 1#ifndef CONFIG_H
2#define CONFIG_H 2#define CONFIG_H
3 3
4#define CLIC_VERSION "0.11"
5#define CLIC_VERSION_STR "Clic v" CLIC_VERSION
6
4#define DEBUG_DIVIDE false 7#define DEBUG_DIVIDE false
5#define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 8#define DEBUG_PARSE false
6 9
10#define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0
11#define DBS_START( s ) if( DEBUG_ ##s ) { (void)0
12#define DBS_END() } (void)0
7 13
8#endif 14#endif
diff --git a/src/main.cpp b/src/main.cpp
index a0faa6a..a1d7672 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,3 +1,4 @@
1#include "config.h"
1#include "number.h" 2#include "number.h"
2#include "packedintarray.h" 3#include "packedintarray.h"
3#include "lexer.h" 4#include "lexer.h"
@@ -17,7 +18,7 @@ int main( int argc, char *argv[] )
17 { 18 {
18 Options opt( argc, argv ); 19 Options opt( argc, argv );
19 20
20 println("CliC - 0.10"); 21 println(CLIC_VERSION_STR);
21 println("Try \\exit, \\help, \\scale, and \\radix commands."); 22 println("Try \\exit, \\help, \\scale, and \\radix commands.");
22 println(""); 23 println("");
23 24
diff --git a/src/number.cpp b/src/number.cpp
index 8fc1d7f..05e310c 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -434,6 +434,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
434 DBS( DIVIDE, Bu::println(" -> '%1'").arg( rhs.digit( j ) ) ); 434 DBS( DIVIDE, Bu::println(" -> '%1'").arg( rhs.digit( j ) ) );
435 nDiv.aInt.append( rhs.digit( j ) ); 435 nDiv.aInt.append( rhs.digit( j ) );
436 } 436 }
437 nDiv.aInt.trim();
437 DBS( DIVIDE, Bu::println("New divisor: %1").arg( nDiv ) ); 438 DBS( DIVIDE, Bu::println("New divisor: %1").arg( nDiv ) );
438 439
439 // Anchor is the position in the output the new digit will appear. 440 // Anchor is the position in the output the new digit will appear.
@@ -463,6 +464,7 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const
463 DBS( DIVIDE, Bu::println(" ->[%1] '%2'").arg( j ).arg( digit(j) ) ); 464 DBS( DIVIDE, Bu::println(" ->[%1] '%2'").arg( j ).arg( digit(j) ) );
464 nNum.aInt.append( digit( j ) ); 465 nNum.aInt.append( digit( j ) );
465 } 466 }
467 nNum.aInt.trim();
466 DBS( DIVIDE, Bu::println("New numerator: %1").arg( nNum ) ); 468 DBS( DIVIDE, Bu::println("New numerator: %1").arg( nNum ) );
467 while( iAnchor >= -iScale && (!nNum.isZero() || iAnchor > 0)) // division loop 469 while( iAnchor >= -iScale && (!nNum.isZero() || iAnchor > 0)) // division loop
468 { 470 {
diff --git a/src/options.cpp b/src/options.cpp
index 4d64b22..d9d34fa 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -1,5 +1,7 @@
1#include "options.h" 1#include "options.h"
2 2
3#include "config.h"
4
3#include "unitnumber.h" 5#include "unitnumber.h"
4#include "unitparser.h" 6#include "unitparser.h"
5#include "number.h" 7#include "number.h"
@@ -34,6 +36,8 @@ Options::Options( int argc, char *argv[] ) :
34 "use all remaining parameters as the equation (you may wish to quote " 36 "use all remaining parameters as the equation (you may wish to quote "
35 "your equation to prevent special characters from being interpreted by " 37 "your equation to prevent special characters from being interpreted by "
36 "your shell)."); 38 "your shell).");
39 addOption( Bu::slot(this, &Options::version), 'v', "version",
40 "Show the version string ('" CLIC_VERSION_STR "')");
37 addHelpOption('h', "help", "This help"); 41 addHelpOption('h', "help", "This help");
38 42
39 parse( argc, argv ); 43 parse( argc, argv );
@@ -189,9 +193,15 @@ int Options::execute( Bu::StringArray aArgs )
189 lex.setRadix( iRadix ); 193 lex.setRadix( iRadix );
190 Parser parser( lex, mbOut ); 194 Parser parser( lex, mbOut );
191 parser.parse(); 195 parser.parse();
192 Bu::println( mbOut.getString() ); 196 Bu::print( mbOut.getString() );
193 exit( 0 ); 197 exit( 0 );
194 return aArgs.getSize(); 198 return aArgs.getSize();
195} 199}
196 200
201int Options::version( Bu::StringArray aArgs )
202{
203 Bu::println( CLIC_VERSION_STR );
204 exit( 0 );
205 return 0;
206}
197 207
diff --git a/src/options.h b/src/options.h
index cd0fd2d..9afa0ac 100644
--- a/src/options.h
+++ b/src/options.h
@@ -18,6 +18,7 @@ private:
18 int isPrime( Bu::StringArray aArgs ); 18 int isPrime( Bu::StringArray aArgs );
19 int convert( Bu::StringArray aArgs ); 19 int convert( Bu::StringArray aArgs );
20 int execute( Bu::StringArray aArgs ); 20 int execute( Bu::StringArray aArgs );
21 int version( Bu::StringArray aArgs );
21 22
22 int iScale; 23 int iScale;
23 int iRadix; 24 int iRadix;
diff --git a/src/parser.cpp b/src/parser.cpp
index 72f7bb3..bd49c38 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -1,3 +1,4 @@
1#include "config.h"
1#include "parser.h" 2#include "parser.h"
2#include "lexer.h" 3#include "lexer.h"
3#include "number.h" 4#include "number.h"
@@ -6,7 +7,6 @@
6#include <bu/sio.h> 7#include <bu/sio.h>
7#include <stdlib.h> 8#include <stdlib.h>
8 9
9//#define DEBUG
10 10
11Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : 11Parser::Parser( Lexer &lex, Bu::Stream &rOut ) :
12 lex( lex ), 12 lex( lex ),
@@ -140,16 +140,17 @@ void Parser::parse()
140 getPriority( tsNonTerminal.peek().eType ) < 140 getPriority( tsNonTerminal.peek().eType ) <
141 getPriority( t.eType ) ) 141 getPriority( t.eType ) )
142 { 142 {
143#ifdef DEBUG 143 DBS( PARSE,
144 Bu::println("Pushing non-terminal: %1").arg( t.eType ); 144 Bu::println("Pushing non-terminal: %1").arg( t.eType )
145#endif 145 );
146 tsNonTerminal.push( t ); 146 tsNonTerminal.push( t );
147 } 147 }
148 else 148 else
149 { 149 {
150#ifdef DEBUG 150 DBS( PARSE,
151 Bu::println("Unwinding stack before pushing: %1").arg( t.eType ); 151 Bu::println("Unwinding stack before pushing: %1")
152#endif 152 .arg( t.eType )
153 );
153 unwind(); 154 unwind();
154 tsNonTerminal.push( t ); 155 tsNonTerminal.push( t );
155 } 156 }
@@ -172,7 +173,7 @@ void Parser::unwind()
172{ 173{
173 for(;;) 174 for(;;)
174 { 175 {
175#ifdef DEBUG 176 DBS_START( PARSE );
176 for( TokenStack::iterator i = tsTerminal.begin(); i; i++ ) 177 for( TokenStack::iterator i = tsTerminal.begin(); i; i++ )
177 { 178 {
178 if( (*i).eType == Token::tNumber ) 179 if( (*i).eType == Token::tNumber )
@@ -184,7 +185,8 @@ void Parser::unwind()
184 for( TokenStack::iterator i = tsNonTerminal.begin(); i; i++ ) 185 for( TokenStack::iterator i = tsNonTerminal.begin(); i; i++ )
185 Bu::print(" <%1>").arg( (*i).eType ); 186 Bu::print(" <%1>").arg( (*i).eType );
186 Bu::println(""); 187 Bu::println("");
187#endif 188 DBS_END();
189
188 if( tsNonTerminal.isEmpty() ) 190 if( tsNonTerminal.isEmpty() )
189 return; 191 return;
190 192
diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp
index c7b85b9..985f0d9 100644
--- a/src/unitnumber.cpp
+++ b/src/unitnumber.cpp
@@ -75,6 +75,8 @@ void UnitNumber::divide1()
75 unitTest(Number("4") / Number("10") == "0"); 75 unitTest(Number("4") / Number("10") == "0");
76 unitTest(Number("4") % Number("10") == "4"); 76 unitTest(Number("4") % Number("10") == "4");
77 unitTest(Number("200") / Number("2") == "100"); 77 unitTest(Number("200") / Number("2") == "100");
78 unitTest(Number("10", 5) / Number("0.01", 5) == "1000");
79 unitTest(Number("552", 5) / Number("0.051", 5) == "10823.52941");
78} 80}
79 81
80#define mathTest( anum, op, bnum, answ ) \ 82#define mathTest( anum, op, bnum, answ ) \