summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/options.cpp37
-rw-r--r--src/options.h1
-rw-r--r--src/unitparser.cpp4
3 files changed, 40 insertions, 2 deletions
diff --git a/src/options.cpp b/src/options.cpp
index 3581500..4d64b22 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -3,6 +3,8 @@
3#include "unitnumber.h" 3#include "unitnumber.h"
4#include "unitparser.h" 4#include "unitparser.h"
5#include "number.h" 5#include "number.h"
6#include "lexer.h"
7#include "parser.h"
6 8
7#include <bu/sio.h> 9#include <bu/sio.h>
8#include <stdlib.h> 10#include <stdlib.h>
@@ -26,6 +28,12 @@ Options::Options( int argc, char *argv[] ) :
26 "Convert the provided number to the given radix in the format " 28 "Convert the provided number to the given radix in the format "
27 "number:to-radix or from-radix:number:to-radix. The radix should " 29 "number:to-radix or from-radix:number:to-radix. The radix should "
28 "be provided in base-10"); 30 "be provided in base-10");
31 addOption( Bu::slot(this, &Options::execute), 'e', "execute",
32 "Execute the next parameters as an equation and print the result. "
33 "If you want a different scale or radix then set them first. This will "
34 "use all remaining parameters as the equation (you may wish to quote "
35 "your equation to prevent special characters from being interpreted by "
36 "your shell).");
29 addHelpOption('h', "help", "This help"); 37 addHelpOption('h', "help", "This help");
30 38
31 parse( argc, argv ); 39 parse( argc, argv );
@@ -158,3 +166,32 @@ int Options::convert( Bu::StringArray aArgs )
158 return 0; 166 return 0;
159} 167}
160 168
169int Options::execute( Bu::StringArray aArgs )
170{
171 if( aArgs.getSize() == 1 )
172 {
173 Bu::println("Must provide an equation to execute.");
174 exit( 0 );
175 return 0;
176 }
177 Bu::MemBuf mbIn(aArgs[1]);
178 mbIn.setPos( mbIn.getSize() );
179 for( int j = 2; j < aArgs.getSize(); j++ )
180 {
181 mbIn.write(" ");
182 mbIn.write( aArgs[j] );
183 }
184// Bu::println("eq: '''%1'''").arg( mbIn.getString() );
185 mbIn.setPos( 0 );
186 Bu::MemBuf mbOut;
187 Lexer lex( mbIn );
188 lex.setScale( iScale );
189 lex.setRadix( iRadix );
190 Parser parser( lex, mbOut );
191 parser.parse();
192 Bu::println( mbOut.getString() );
193 exit( 0 );
194 return aArgs.getSize();
195}
196
197
diff --git a/src/options.h b/src/options.h
index e0b7f33..cd0fd2d 100644
--- a/src/options.h
+++ b/src/options.h
@@ -17,6 +17,7 @@ private:
17 int textPrimes( Bu::StringArray aArgs ); 17 int textPrimes( Bu::StringArray aArgs );
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 21
21 int iScale; 22 int iScale;
22 int iRadix; 23 int iRadix;
diff --git a/src/unitparser.cpp b/src/unitparser.cpp
index 6a5dcf4..e23f76c 100644
--- a/src/unitparser.cpp
+++ b/src/unitparser.cpp
@@ -48,8 +48,8 @@ void UnitParser::assignment()
48 lex.setScale( 0 ); 48 lex.setScale( 0 );
49 Parser parser( lex, mbOut ); 49 Parser parser( lex, mbOut );
50 parser.parse(); 50 parser.parse();
51 Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() ) 51// Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() )
52 .arg( parser.getVariable("test") ); 52// .arg( parser.getVariable("test") );
53 unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() ); 53 unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() );
54} 54}
55 55