From 145c88f23e6587e310d3ca974365d20d373d8e1b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 25 Nov 2014 09:42:47 -0700 Subject: Added a new 'execute' command line parameter. --- src/options.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/options.h | 1 + src/unitparser.cpp | 4 ++-- 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 @@ #include "unitnumber.h" #include "unitparser.h" #include "number.h" +#include "lexer.h" +#include "parser.h" #include #include @@ -26,6 +28,12 @@ Options::Options( int argc, char *argv[] ) : "Convert the provided number to the given radix in the format " "number:to-radix or from-radix:number:to-radix. The radix should " "be provided in base-10"); + addOption( Bu::slot(this, &Options::execute), 'e', "execute", + "Execute the next parameters as an equation and print the result. " + "If you want a different scale or radix then set them first. This will " + "use all remaining parameters as the equation (you may wish to quote " + "your equation to prevent special characters from being interpreted by " + "your shell)."); addHelpOption('h', "help", "This help"); parse( argc, argv ); @@ -158,3 +166,32 @@ int Options::convert( Bu::StringArray aArgs ) return 0; } +int Options::execute( Bu::StringArray aArgs ) +{ + if( aArgs.getSize() == 1 ) + { + Bu::println("Must provide an equation to execute."); + exit( 0 ); + return 0; + } + Bu::MemBuf mbIn(aArgs[1]); + mbIn.setPos( mbIn.getSize() ); + for( int j = 2; j < aArgs.getSize(); j++ ) + { + mbIn.write(" "); + mbIn.write( aArgs[j] ); + } +// Bu::println("eq: '''%1'''").arg( mbIn.getString() ); + mbIn.setPos( 0 ); + Bu::MemBuf mbOut; + Lexer lex( mbIn ); + lex.setScale( iScale ); + lex.setRadix( iRadix ); + Parser parser( lex, mbOut ); + parser.parse(); + Bu::println( mbOut.getString() ); + exit( 0 ); + return aArgs.getSize(); +} + + 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: int textPrimes( Bu::StringArray aArgs ); int isPrime( Bu::StringArray aArgs ); int convert( Bu::StringArray aArgs ); + int execute( Bu::StringArray aArgs ); int iScale; 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() lex.setScale( 0 ); Parser parser( lex, mbOut ); parser.parse(); - Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() ) - .arg( parser.getVariable("test") ); +// Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() ) +// .arg( parser.getVariable("test") ); unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() ); } -- cgit v1.2.3