From 0580d6a85fda454b05620aa4415a53f3dbcd3475 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 19 Dec 2016 15:52:04 -0700 Subject: It actually sorta' works again! --- src/main.cpp | 29 ++++++++++++++++++++++++++++- src/parser.cpp | 6 ++++++ src/scriptengine.cpp | 31 ++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 667d19a..1365f1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include "token.h" #include "parser.h" #include "options.h" +#include "scriptengine.h" + #include #include @@ -14,6 +16,21 @@ #include using namespace Bu; +void numResult( const class Number &num ) +{ + println("==> %1").arg( num ); +} + +void dispError( const Bu::String &sMsg ) +{ + println(sMsg); +} + +void dispMessage( const Bu::String &sMsg ) +{ + println(sMsg); +} + int main( int argc, char *argv[] ) { try @@ -28,7 +45,17 @@ int main( int argc, char *argv[] ) lex.setScale( opt.getScale() ); lex.setRadix( opt.getRadix() ); Parser parser( lex ); - parser.parse(); + + ScriptEngine se; + se.sigNumResult = Bu::slot(numResult); + se.sigError = Bu::slot(dispError); + se.sigMessage = Bu::slot(dispMessage); + for(;;) + { + Expression *pExpr = parser.parse(); + se.exec( pExpr ); + delete pExpr; + } } catch( std::exception &e ) { diff --git a/src/parser.cpp b/src/parser.cpp index c83c156..cdb1064 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -26,6 +26,8 @@ Parser::~Parser() Expression *Parser::parse() { + while( lex[0].eType == Token::tEndOfLine ) + lex.nextToken(); pCurExp = new Expression(); statement(); while( !tsTerminal.isEmpty() ) @@ -33,6 +35,9 @@ Expression *Parser::parse() reduce(); } + if( lex[0].eType == Token::tEndOfLine ) + lex.nextToken(); + printState("Final"); tsTerminal.clear(); @@ -247,6 +252,7 @@ void Parser::statement() break; } lex.setMode( Lexer::modeNormal ); + lex.nextToken(); } else { diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index e4c5c2e..6a00af8 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -105,7 +105,7 @@ void ScriptEngine::exec( Expression *pExpr ) } } - if( sigNumResult.isSet() ) + if( sigNumResult.isSet() && !sNums.isEmpty() ) sigNumResult( sNums.peekPop() ); } @@ -134,12 +134,34 @@ void ScriptEngine::command( Expression::iterator &i ) } if( sigMessage.isSet() ) sigMessage(Bu::String("Changed scale to: %1").arg( iScale )); - for( VarHash::iterator i = hVarState.begin(); i; i++ ) - (*i).setScale( iScale ); + for( VarHash::iterator j = hVarState.begin(); j; j++ ) + (*j).setScale( iScale ); + i++; } break; case Token::tCmdRadix: + { + if( !(++i) ) + { + if( sigError.isSet() ) + sigError("You must provide a positive integer."); + return; + } + int32_t iRadix = strtol( (*i).sVal->getStr(), 0, 10 ); + if( iRadix < 0 ) + { + if( sigError.isSet() ) + sigError("You must provide a positive integer."); + return; + } + if( sigMessage.isSet() ) + sigMessage(Bu::String("Changed radix to: %1").arg( iRadix )); + hVarState.clear(); +// for( VarHash::iterator i = hVarState.begin(); i; i++ ) +// (*i).setRadix( iRadix ); + i++; + } break; case Token::tCmdExtended: @@ -155,5 +177,8 @@ void ScriptEngine::command( Expression::iterator &i ) }*/ break; } + + if( (*i).eType != Token::tCmdEndParams ) + sigError("Too many parameters."); } -- cgit v1.2.3