From 2297488eae424197dce4ed6b1dc50ae67c093074 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 30 Nov 2016 14:52:39 -0700 Subject: I managed to confuse myself pretty well. I'm doing some reading up on LR(n) parsers, I just sort of started without thinking about it this time, not a great approach. I feel like it can't hurt to have an update on how this works anyway. I think the idea was solid, but I was trying to do too much at once. One question is what my goal should be for this. I could just solve the equation as we go, or I could generate code that will let us solve the equation. The later is obviously attractive in that it will let us run an expression more than once, and maybe even define functions. I like that. --- src/lexer.cpp | 13 +++++++++---- src/parser.cpp | 34 +++++++++++++++++++++++++++++++--- src/parser.h | 2 +- src/token.cpp | 26 ++++++++++++++++++++++++++ src/token.h | 2 ++ 5 files changed, 69 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/lexer.cpp b/src/lexer.cpp index fbcaafb..55d155d 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -26,15 +26,15 @@ Lexer::~Lexer() void Lexer::nextToken() { - if( iLookAheadSize <= 1 ) + if( iLookAheadUsed <= 1 ) { - iLookAheadSize = 0; + iLookAheadUsed = 0; iLookAheadStart = 0; } else { iLookAheadStart = (iLookAheadStart+1)%iLookAheadSize; - iLookAheadSize--; + iLookAheadUsed--; } } @@ -55,11 +55,16 @@ void Lexer::fillToken() default: throw Bu::ExceptionBase("Invalid mode."); } - +/* Bu::sio << "read[" << ((iLookAheadUsed+iLookAheadStart)%iLookAheadSize) << "]: " << aLookAhead[(iLookAheadUsed+iLookAheadStart)%iLookAheadSize].eType; + if( aLookAhead[(iLookAheadUsed+iLookAheadStart)%iLookAheadSize].eType == Token::tNumber ) + Bu::sio << "(" + << aLookAhead[(iLookAheadUsed+iLookAheadStart)%iLookAheadSize].nVal->toString() << ")"; + Bu::sio << Bu::sio.nl; +*/ } Token Lexer::nextTokenNormal() diff --git a/src/parser.cpp b/src/parser.cpp index 1f9a193..0131ba3 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -23,25 +23,53 @@ void Parser::parse() for(;;) { lex.nextToken(); + if( lex[0].eType == Token::tEndOfInput ) + break; + expr(); + Bu::sio << "Final stack:"; for( TokenStack::iterator i = tsStack.begin(); i; i++ ) { - Bu::sio << *i << " "; + Bu::sio << " " << (*i).eType; + if( (*i).eType == Token::tNumber ) + { + Bu::sio << "(" << (*i).nVal->toString() << ")"; + } } + Bu::sio << Bu::sio.nl; } } void Parser::expr() { + if( lex[0].eType == Token::tEndOfInput ) + return; if( lex[0].eType == Token::tVariable && lex[1].eType == Token::tEquals ) { - // Assignment! + Token t = lex[0]; + lex.nextToken(); + lex.nextToken(); expr(); + return; } exprP(); + + switch( lex[0].eType ) + { + case Token::tPlus: + case Token::tMinus: + case Token::tMultiply: + case Token::tDivide: + case Token::tModulus: + Token t = lex[0]; + lex.nextToken(); + expr(); + tsStack.push( t ); + break; + } } void Parser::exprP() @@ -222,7 +250,7 @@ void Parser::setVariable( const Bu::String &sName, const Number &rValue ) hVars.insert( sName, rValue ); } -void Parser::unwind() +void Parser::reduce() { /* for(;;) diff --git a/src/parser.h b/src/parser.h index d50951b..05fab0b 100644 --- a/src/parser.h +++ b/src/parser.h @@ -57,7 +57,7 @@ private: void exprP(); private: - void unwind(); + void reduce(); int reqTokens( Token::Type eType ); int getPriority( Token::Type eType ); Number &deref( Token &t ); diff --git a/src/token.cpp b/src/token.cpp index 30f3566..c591e6a 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -55,6 +55,32 @@ Token::~Token() } } +Token &Token::operator=( const Token &rhs ) +{ + switch( eType ) + { + case tNumber: + delete nVal; + break; + + case tVariable: + case tCommand: + case tString: + delete sVal; + break; + + default: + break; + } + eType = rhs.eType; + sVal = rhs.sVal; + + Token &rMod = const_cast(rhs); + rMod.sVal = 0; + + return *this; +} + Bu::Formatter &operator<<( Bu::Formatter &f, Token::Type eType ) { switch( eType ) diff --git a/src/token.h b/src/token.h index 12a4904..8d7dc20 100644 --- a/src/token.h +++ b/src/token.h @@ -41,6 +41,8 @@ public: Token( const Token &rSrc ); ~Token(); + Token &operator=( const Token &rhs ); + Type eType; union { -- cgit v1.2.3