From 8bb2a6039dcaf557533eb58387daea932589063a Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Wed, 30 Jun 2010 11:03:08 +0200 Subject: [PATCH] modified comma handling to be morse consistent with original script language --- components/compiler/exprparser.cpp | 28 ++++++++++++++++++++++++---- components/compiler/exprparser.hpp | 1 + components/compiler/scanner.cpp | 23 +++++++++++++++-------- components/compiler/scanner.hpp | 14 ++++++++++---- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/components/compiler/exprparser.cpp b/components/compiler/exprparser.cpp index 79b6f3447..ffb6fd336 100644 --- a/components/compiler/exprparser.cpp +++ b/components/compiler/exprparser.cpp @@ -177,11 +177,13 @@ namespace Compiler ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals, Literals& literals) : Parser (errorHandler, context), mLocals (locals), mLiterals (literals), - mNextOperand (true) + mNextOperand (true), mFirst (true) {} bool ExprParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) { + mFirst = false; + if (mNextOperand) { pushIntegerLiteral (value); @@ -194,6 +196,8 @@ namespace Compiler bool ExprParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) { + mFirst = false; + if (mNextOperand) { pushFloatLiteral (value); @@ -207,11 +211,15 @@ namespace Compiler bool ExprParser::parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner) { + mFirst = false; + return Parser::parseName (name, loc, scanner); } bool ExprParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) { + mFirst = false; + if (keyword==Scanner::K_getsquareroot && mNextOperand) { mTokenLoc = loc; @@ -230,16 +238,27 @@ namespace Compiler { if (code==Scanner::S_comma) { - // end marker mTokenLoc = loc; + + if (mFirst) + { + // leading comma + mFirst = false; + return true; + } + + // end marker + scanner.putbackSpecial (code, loc); return false; } - + + mFirst = false; + if (code==Scanner::S_newline) { // end marker mTokenLoc = loc; - scanner.putbackNewline (loc); + scanner.putbackSpecial (code, loc); return false; } @@ -286,6 +305,7 @@ namespace Compiler mOperators.clear(); mNextOperand = true; mCode.clear(); + mFirst = true; } char ExprParser::append (std::vector& code) diff --git a/components/compiler/exprparser.hpp b/components/compiler/exprparser.hpp index 6dba519ea..14c530010 100644 --- a/components/compiler/exprparser.hpp +++ b/components/compiler/exprparser.hpp @@ -22,6 +22,7 @@ namespace Compiler bool mNextOperand; TokenLoc mTokenLoc; std::vector mCode; + bool mFirst; int getPriority (char op) const; diff --git a/components/compiler/scanner.cpp b/components/compiler/scanner.cpp index e04e2114b..a8514f956 100644 --- a/components/compiler/scanner.cpp +++ b/components/compiler/scanner.cpp @@ -43,10 +43,17 @@ namespace Compiler bool Scanner::scanToken (Parser& parser) { - if (mNewline) + switch (mPutback) { - mNewline = false; - return parser.parseSpecial (S_newline, mPutbackLoc, *this); + case Putback_Special: + { + mPutback = Putback_None; + return parser.parseSpecial (mPutbackCode, mPutbackLoc, *this); + } + + case Putback_None: + + break; } char c; @@ -419,20 +426,20 @@ namespace Compiler // constructor Scanner::Scanner (ErrorHandler& errorHandler, std::istream& inputStream) - : mErrorHandler (errorHandler), mStream (inputStream), mNewline (false) + : mErrorHandler (errorHandler), mStream (inputStream), mPutback (Putback_None) { } void Scanner::scan (Parser& parser) { - mNewline = false; while (scanToken (parser)); } - void Scanner::putbackNewline (const TokenLoc& loc) + void Scanner::putbackSpecial (int code, const TokenLoc& loc) { - mNewline = true; - mPutbackLoc = loc; + mPutback = Putback_Special; + mPutbackCode = code; + mPutbackLoc = loc; } } diff --git a/components/compiler/scanner.hpp b/components/compiler/scanner.hpp index 5a490d70e..271639c97 100644 --- a/components/compiler/scanner.hpp +++ b/components/compiler/scanner.hpp @@ -18,11 +18,17 @@ namespace Compiler class Scanner { + enum putback_type + { + Putback_None, Putback_Special, + }; + ErrorHandler& mErrorHandler; TokenLoc mLoc; TokenLoc mPrevLoc; std::istream& mStream; - bool mNewline; + putback_type mPutback; + int mPutbackCode; TokenLoc mPutbackLoc; public: @@ -81,9 +87,9 @@ namespace Compiler void scan (Parser& parser); ///< Scan a token and deliver it to the parser. - - void putbackNewline (const TokenLoc& loc); - ///< put back a newline token + + void putbackSpecial (int code, const TokenLoc& loc); + ///< put back a special token }; }