#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 ); add( static_cast(&UnitParser::assignment), "assignment", 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.65"); unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); unitTest(parse("10+2*2*2+2") == "20"); } void UnitParser::assignment() { Bu::MemBuf mbIn("$test = 2*2*2"); Bu::MemBuf mbOut; Lexer lex( mbIn ); lex.setScale( 0 ); Parser parser( lex, mbOut ); parser.parse(); // Bu::println("%1 == %2").arg( mbOut.getString().trimWhitespace() ) // .arg( parser.getVariable("test") ); unitTest( mbOut.getString().trimWhitespace() == parser.getVariable("test").toString() ); }