From b96daa4e9849ac9caf5c0dfcea8daac28267ea19 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 1 Dec 2016 10:40:43 -0700 Subject: Scripts are executable now! The main interactive interface doesn't work, and there's a lot left to do with the unit tests, other command line options, etc. but it's pretty exciting. I also still have to figure out how commands will work. I'm thinking they'll be stored in an Expression and executed by the engine as normal. --- src/parser.cpp | 65 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index 04a81ce..c7cf994 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3,14 +3,13 @@ #include "lexer.h" #include "number.h" #include "datafiles.h" +#include "expression.h" #include #include - -Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : - lex( lex ), - rOut( rOut ) +Parser::Parser( Lexer &lex ) : + lex( lex ) { } @@ -18,22 +17,19 @@ Parser::~Parser() { } -void Parser::parse() +Expression *Parser::parse() { - for(;;) - { - lex.nextToken(); - if( lex[0].eType == Token::tEndOfInput ) - break; + pCurExp = new Expression(); + exprR(); + reduce(); - exprR(); - reduce(); + printState("Final"); - printState("Final"); + tsParse.clear(); - tsParse.clear(); - tsScript.clear(); - } + Expression *pTmp = pCurExp; + pCurExp = NULL; + return pTmp; } void Parser::exprR() @@ -44,7 +40,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; @@ -61,7 +57,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(); @@ -74,10 +70,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 ) { @@ -115,14 +111,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; @@ -161,7 +157,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] ); @@ -283,7 +279,7 @@ void Parser::reduce() { case Token::tNumber: case Token::tVariable: - tsScript.append( tOp ); + output( tOp ); tsParse.push( Token( Token::tComputedValue ) ); break; @@ -303,14 +299,14 @@ void Parser::reduce() if( t.eType == Token::tNumber || t.eType == Token::tVariable ) { - tsScript.append( t ); + output( t ); } else if( t.eType == Token::tComputedValue ) { // Nope, we don't care } } - tsScript.append( tOp ); + output( tOp ); shift( Token( Token::tComputedValue ) ); break; @@ -318,8 +314,8 @@ void Parser::reduce() { Token t = tsParse.peekPop(); if( t.eType != Token::tComputedValue ) - tsScript.append( t ); - tsScript.append( tOp ); + output( t ); + output( tOp ); shift( Token( Token::tComputedValue ) ); } break; @@ -345,6 +341,11 @@ void Parser::reduce() } } +void Parser::output( const Token &t ) +{ + pCurExp->append( t ); +} + int Parser::reqTokens( Token::Type eType ) { switch( eType ) @@ -402,6 +403,7 @@ 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++ ) @@ -410,11 +412,12 @@ void Parser::printState( const Bu::String &sTag ) } Bu::sio << Bu::sio.nl; Bu::sio << sTag << " script:"; - for( TokenStack::iterator i = tsScript.begin(); i; i++ ) + for( TokenStack::iterator i = pCurExp->begin(); i; i++ ) { Bu::sio << " " << (*i); } Bu::sio << Bu::sio.nl; Bu::sio << "----------------" << Bu::sio.nl; + */ } -- cgit v1.2.3