From 1674d406dcae70f91b4d960e0811e63e150f5f54 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Thu, 1 Jul 2010 11:07:21 +0200 Subject: [PATCH] made usage of comma as argument separator optional (largely untested, because we don't have anything that takes more than one argument) --- components/compiler/exprparser.cpp | 66 ++++++++++++++++++++++-------- components/compiler/scanner.cpp | 51 ++++++++++++++++++++++- components/compiler/scanner.hpp | 19 ++++++++- 3 files changed, 116 insertions(+), 20 deletions(-) diff --git a/components/compiler/exprparser.cpp b/components/compiler/exprparser.cpp index e2b68f70f..4b572b6b1 100644 --- a/components/compiler/exprparser.cpp +++ b/components/compiler/exprparser.cpp @@ -197,8 +197,12 @@ namespace Compiler mTokenLoc = loc; return true; } - - return Parser::parseInt (value, loc, scanner); + else + { + // no comma was used between arguments + scanner.putbackInt (value, loc); + return false; + } } bool ExprParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) @@ -211,8 +215,12 @@ namespace Compiler mTokenLoc = loc; return true; } - - return Parser::parseFloat (value, loc, scanner); + else + { + // no comma was used between arguments + scanner.putbackFloat (value, loc); + return false; + } } bool ExprParser::parseName (const std::string& name, const TokenLoc& loc, @@ -232,6 +240,12 @@ namespace Compiler return true; } } + else + { + // no comma was used between arguments + scanner.putbackName (name, loc); + return false; + } return Parser::parseName (name, loc, scanner); } @@ -239,16 +253,25 @@ namespace Compiler bool ExprParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) { mFirst = false; - - if (keyword==Scanner::K_getsquareroot && mNextOperand) - { - mTokenLoc = loc; - parseArguments ("f", scanner); - Generator::squareRoot (mCode); - - mNextOperand = false; - return true; + if (mNextOperand) + { + if (keyword==Scanner::K_getsquareroot) + { + mTokenLoc = loc; + parseArguments ("f", scanner); + + Generator::squareRoot (mCode); + + mNextOperand = false; + return true; + } + } + else + { + // no comma was used between arguments + scanner.putbackKeyword (keyword, loc); + return false; } return Parser::parseKeyword (keyword, loc, scanner); @@ -290,11 +313,20 @@ namespace Compiler return true; } - if (code==Scanner::S_open && mNextOperand) + if (code==Scanner::S_open) { - mOperators.push_back ('('); - mTokenLoc = loc; - return true; + if (mNextOperand) + { + mOperators.push_back ('('); + mTokenLoc = loc; + return true; + } + else + { + // no comma was used between arguments + scanner.putbackKeyword (code, loc); + return false; + } } if (code==Scanner::S_close && !mNextOperand) diff --git a/components/compiler/scanner.cpp b/components/compiler/scanner.cpp index a8514f956..e4869cba5 100644 --- a/components/compiler/scanner.cpp +++ b/components/compiler/scanner.cpp @@ -46,10 +46,29 @@ namespace Compiler switch (mPutback) { case Putback_Special: - { + mPutback = Putback_None; return parser.parseSpecial (mPutbackCode, mPutbackLoc, *this); - } + + case Putback_Integer: + + mPutback = Putback_None; + return parser.parseInt (mPutbackInteger, mPutbackLoc, *this); + + case Putback_Float: + + mPutback = Putback_None; + return parser.parseFloat (mPutbackFloat, mPutbackLoc, *this); + + case Putback_Name: + + mPutback = Putback_None; + return parser.parseName (mPutbackName, mPutbackLoc, *this); + + case Putback_Keyword: + + mPutback = Putback_None; + return parser.parseKeyword (mPutbackCode, mPutbackLoc, *this); case Putback_None: @@ -441,5 +460,33 @@ namespace Compiler mPutbackCode = code; mPutbackLoc = loc; } + + void Scanner::putbackInt (int value, const TokenLoc& loc) + { + mPutback = Putback_Integer; + mPutbackInteger = value; + mPutbackLoc = loc; + } + + void Scanner::putbackFloat (float value, const TokenLoc& loc) + { + mPutback = Putback_Float; + mPutbackFloat = value; + mPutbackLoc = loc; + } + + void Scanner::putbackName (const std::string& name, const TokenLoc& loc) + { + mPutback = Putback_Name; + mPutbackName = name; + mPutbackLoc = loc; + } + + void Scanner::putbackKeyword (int keyword, const TokenLoc& loc) + { + mPutback = Putback_Keyword; + mPutbackCode = keyword; + mPutbackLoc = loc; + } } diff --git a/components/compiler/scanner.hpp b/components/compiler/scanner.hpp index 044e02375..2dd13d5df 100644 --- a/components/compiler/scanner.hpp +++ b/components/compiler/scanner.hpp @@ -20,7 +20,8 @@ namespace Compiler { enum putback_type { - Putback_None, Putback_Special, + Putback_None, Putback_Special, Putback_Integer, Putback_Float, + Putback_Name, Putback_Keyword }; ErrorHandler& mErrorHandler; @@ -29,6 +30,9 @@ namespace Compiler std::istream& mStream; putback_type mPutback; int mPutbackCode; + int mPutbackInteger; + float mPutbackFloat; + std::string mPutbackName; TokenLoc mPutbackLoc; public: @@ -90,6 +94,19 @@ namespace Compiler void putbackSpecial (int code, const TokenLoc& loc); ///< put back a special token + + void putbackInt (int value, const TokenLoc& loc); + ///< put back an integer token + + void putbackFloat (float value, const TokenLoc& loc); + ///< put back a float token + + void putbackName (const std::string& name, const TokenLoc& loc); + ///< put back a name toekn + + void putbackKeyword (int keyword, const TokenLoc& loc); + ///< put back a keyword token + }; }