modified comma handling to be morse consistent with original script language

actorid
Marc Zinnschlag 15 years ago
parent 5fb4abae4f
commit 8bb2a6039d

@ -177,11 +177,13 @@ namespace Compiler
ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals, ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
Literals& literals) Literals& literals)
: Parser (errorHandler, context), mLocals (locals), mLiterals (literals), : Parser (errorHandler, context), mLocals (locals), mLiterals (literals),
mNextOperand (true) mNextOperand (true), mFirst (true)
{} {}
bool ExprParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) bool ExprParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
{ {
mFirst = false;
if (mNextOperand) if (mNextOperand)
{ {
pushIntegerLiteral (value); pushIntegerLiteral (value);
@ -194,6 +196,8 @@ namespace Compiler
bool ExprParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) bool ExprParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner)
{ {
mFirst = false;
if (mNextOperand) if (mNextOperand)
{ {
pushFloatLiteral (value); pushFloatLiteral (value);
@ -207,11 +211,15 @@ namespace Compiler
bool ExprParser::parseName (const std::string& name, const TokenLoc& loc, bool ExprParser::parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner) Scanner& scanner)
{ {
mFirst = false;
return Parser::parseName (name, loc, scanner); return Parser::parseName (name, loc, scanner);
} }
bool ExprParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) bool ExprParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
{ {
mFirst = false;
if (keyword==Scanner::K_getsquareroot && mNextOperand) if (keyword==Scanner::K_getsquareroot && mNextOperand)
{ {
mTokenLoc = loc; mTokenLoc = loc;
@ -230,16 +238,27 @@ namespace Compiler
{ {
if (code==Scanner::S_comma) if (code==Scanner::S_comma)
{ {
// end marker
mTokenLoc = loc; mTokenLoc = loc;
if (mFirst)
{
// leading comma
mFirst = false;
return true;
}
// end marker
scanner.putbackSpecial (code, loc);
return false; return false;
} }
mFirst = false;
if (code==Scanner::S_newline) if (code==Scanner::S_newline)
{ {
// end marker // end marker
mTokenLoc = loc; mTokenLoc = loc;
scanner.putbackNewline (loc); scanner.putbackSpecial (code, loc);
return false; return false;
} }
@ -286,6 +305,7 @@ namespace Compiler
mOperators.clear(); mOperators.clear();
mNextOperand = true; mNextOperand = true;
mCode.clear(); mCode.clear();
mFirst = true;
} }
char ExprParser::append (std::vector<Interpreter::Type_Code>& code) char ExprParser::append (std::vector<Interpreter::Type_Code>& code)

@ -22,6 +22,7 @@ namespace Compiler
bool mNextOperand; bool mNextOperand;
TokenLoc mTokenLoc; TokenLoc mTokenLoc;
std::vector<Interpreter::Type_Code> mCode; std::vector<Interpreter::Type_Code> mCode;
bool mFirst;
int getPriority (char op) const; int getPriority (char op) const;

@ -43,10 +43,17 @@ namespace Compiler
bool Scanner::scanToken (Parser& parser) bool Scanner::scanToken (Parser& parser)
{ {
if (mNewline) switch (mPutback)
{ {
mNewline = false; case Putback_Special:
return parser.parseSpecial (S_newline, mPutbackLoc, *this); {
mPutback = Putback_None;
return parser.parseSpecial (mPutbackCode, mPutbackLoc, *this);
}
case Putback_None:
break;
} }
char c; char c;
@ -419,20 +426,20 @@ namespace Compiler
// constructor // constructor
Scanner::Scanner (ErrorHandler& errorHandler, std::istream& inputStream) 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) void Scanner::scan (Parser& parser)
{ {
mNewline = false;
while (scanToken (parser)); while (scanToken (parser));
} }
void Scanner::putbackNewline (const TokenLoc& loc) void Scanner::putbackSpecial (int code, const TokenLoc& loc)
{ {
mNewline = true; mPutback = Putback_Special;
mPutbackLoc = loc; mPutbackCode = code;
mPutbackLoc = loc;
} }
} }

@ -18,11 +18,17 @@ namespace Compiler
class Scanner class Scanner
{ {
enum putback_type
{
Putback_None, Putback_Special,
};
ErrorHandler& mErrorHandler; ErrorHandler& mErrorHandler;
TokenLoc mLoc; TokenLoc mLoc;
TokenLoc mPrevLoc; TokenLoc mPrevLoc;
std::istream& mStream; std::istream& mStream;
bool mNewline; putback_type mPutback;
int mPutbackCode;
TokenLoc mPutbackLoc; TokenLoc mPutbackLoc;
public: public:
@ -81,9 +87,9 @@ namespace Compiler
void scan (Parser& parser); void scan (Parser& parser);
///< Scan a token and deliver it to the parser. ///< Scan a token and deliver it to the parser.
void putbackNewline (const TokenLoc& loc); void putbackSpecial (int code, const TokenLoc& loc);
///< put back a newline token ///< put back a special token
}; };
} }

Loading…
Cancel
Save