From 32b48aeb75db4ad40fe0173614e585f9bee72257 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 1 Dec 2016 11:16:25 -0700 Subject: Found a big problem with order of operations. I believe this is because I'm using right tail recursion, but I'm not really sure how I could change it for this program. order of operations is being observed perfectly, but then operations are being performed from right to left, not left to right. I think I may be reducing too frequently, honestly. --- src/parser.cpp | 38 +++++++++++++++++++++++++------------- src/parser.h | 1 + src/scriptengine.cpp | 24 ++++++++++++++++++++---- src/token.h | 1 - 4 files changed, 46 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/parser.cpp b/src/parser.cpp index c7cf994..701c4bd 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -20,8 +20,7 @@ Parser::~Parser() Expression *Parser::parse() { pCurExp = new Expression(); - exprR(); - reduce(); + statement(); printState("Final"); @@ -40,7 +39,7 @@ void Parser::exprR() void Parser::expr() { - //Bu::sio << "::expr " << lex[0] << Bu::sio.nl; + Bu::sio << "::expr " << lex[0] << Bu::sio.nl; if( lex[0].eType == Token::tEndOfInput ) return; @@ -57,7 +56,7 @@ void Parser::expr() case Token::tModulus: Token t = lex[0]; lex.nextToken(); - //Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl; + Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl; if( lex[0].eType == Token::tOpenParen ) { exprP(); @@ -70,10 +69,10 @@ void Parser::expr() if( lex[0].eType == Token::tNumber || lex[0].eType == Token::tVariable ) { - //Bu::sio << "->expr " << t << - // " " << lex[0] << - // " " << lex[1] << - // Bu::sio.nl; + Bu::sio << "->expr " << t << + " " << lex[0] << + " " << lex[1] << + Bu::sio.nl; shift( lex[0] ); if( lex[1].eType&Token::mMetaOperator ) { @@ -111,14 +110,14 @@ void Parser::expr() else if( lex[0].eType == Token::tMinus ) { // This is negation - //Bu::sio << "next token: " << lex[0] << Bu::sio.nl; + Bu::sio << "next token: " << lex[0] << Bu::sio.nl; printState("inline-negate"); exprP(); printState("inline-negate-post"); shift( t ); printState("inline-negate-post2"); - //Bu::sio << "??? " << lex[0] << Bu::sio.nl; + Bu::sio << "??? " << lex[0] << Bu::sio.nl; } } break; @@ -157,7 +156,7 @@ void Parser::exprP() exprR(); if( lex[0].eType != Token::tCloseParen ) { - //Bu::sio << "::exprP " << lex[0] << Bu::sio.nl; + Bu::sio << "::exprP " << lex[0] << Bu::sio.nl; throw Bu::ExceptionBase("Expected close paren"); } shift( lex[0] ); @@ -175,6 +174,21 @@ void Parser::exprP() break; } } + +void Parser::statement() +{ + if( lex[0].eType == Token::tCommand ) + { + lex.setMode( Lexer::modeCommand ); + lex.setMode( Lexer::modeNormal ); + } + else + { + exprR(); + reduce(); + } +} + /* case Token::tCommand: lex.setMode( Lexer::modeCommand ); @@ -403,7 +417,6 @@ int Parser::getPriority( Token::Type eType ) void Parser::printState( const Bu::String &sTag ) { - /* Bu::sio << "----------------" << Bu::sio.nl; Bu::sio << sTag << " stack:"; for( TokenStack::iterator i = tsParse.begin(); i; i++ ) @@ -418,6 +431,5 @@ void Parser::printState( const Bu::String &sTag ) } Bu::sio << Bu::sio.nl; Bu::sio << "----------------" << Bu::sio.nl; - */ } diff --git a/src/parser.h b/src/parser.h index 1e0d1c4..60dd8e1 100644 --- a/src/parser.h +++ b/src/parser.h @@ -55,6 +55,7 @@ private: void expr(); void exprP(); void exprR(); + void statement(); private: void shift( const Token &t ); diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index 7b5df03..b828409 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -56,19 +56,35 @@ Number ScriptEngine::exec( Expression *pExpr ) break; case Token::tPlus: - sNums.push( sNums.peekPop() + sNums.peekPop() ); + { + Number a = sNums.peekPop(); + Number b = sNums.peekPop(); + sNums.push( a + b ); + } break; case Token::tMinus: - sNums.push( sNums.peekPop() - sNums.peekPop() ); + { + Number a = sNums.peekPop(); + Number b = sNums.peekPop(); + sNums.push( a - b ); + } break; case Token::tDivide: - sNums.push( sNums.peekPop() / sNums.peekPop() ); + { + Number a = sNums.peekPop(); + Number b = sNums.peekPop(); + sNums.push( a / b ); + } break; case Token::tMultiply: - sNums.push( sNums.peekPop() * sNums.peekPop() ); + { + Number a = sNums.peekPop(); + Number b = sNums.peekPop(); + sNums.push( a * b ); + } break; case Token::tNegate: diff --git a/src/token.h b/src/token.h index 4ad1136..5bede41 100644 --- a/src/token.h +++ b/src/token.h @@ -40,7 +40,6 @@ public: mMetaOperator = 0x4000, mMetaAltOp = 0x8000, mMetaMeta = 0x0100, - }; Token(); -- cgit v1.2.3