mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-27 16:41:38 +00:00
implemented naked expressions in lineparser (used in console; result is send through messagebox interface)
This commit is contained in:
parent
01edd8deb5
commit
cf7150e585
3 changed files with 129 additions and 6 deletions
|
@ -40,18 +40,20 @@ namespace MWGui
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
ErrorHandler::reset();
|
||||||
|
|
||||||
std::istringstream input (cmd + '\n');
|
std::istringstream input (cmd + '\n');
|
||||||
|
|
||||||
Compiler::Scanner scanner (*this, input, mCompilerContext.getExtensions());
|
Compiler::Scanner scanner (*this, input, mCompilerContext.getExtensions());
|
||||||
|
|
||||||
Compiler::LineParser parser (*this, mCompilerContext, output.getLocals(),
|
Compiler::LineParser parser (*this, mCompilerContext, output.getLocals(),
|
||||||
output.getLiterals(), output.getCode());
|
output.getLiterals(), output.getCode(), true);
|
||||||
|
|
||||||
scanner.scan (parser);
|
scanner.scan (parser);
|
||||||
|
|
||||||
return isGood();
|
return isGood();
|
||||||
}
|
}
|
||||||
catch (const Compiler::SourceException&)
|
catch (const Compiler::SourceException& error)
|
||||||
{
|
{
|
||||||
// error has already been reported via error handler
|
// error has already been reported via error handler
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,19 +11,67 @@
|
||||||
|
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
|
void LineParser::parseExpression (Scanner& scanner, const TokenLoc& loc)
|
||||||
|
{
|
||||||
|
mExprParser.reset();
|
||||||
|
|
||||||
|
if (!mExplicit.empty())
|
||||||
|
{
|
||||||
|
mExprParser.parseName (mExplicit, loc, scanner);
|
||||||
|
mExprParser.parseSpecial (Scanner::S_ref, loc, scanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.scan (mExprParser);
|
||||||
|
|
||||||
|
char type = mExprParser.append (mCode);
|
||||||
|
mState = EndState;
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case 'l':
|
||||||
|
|
||||||
|
Generator::message (mCode, mLiterals, "%g", 0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'f':
|
||||||
|
|
||||||
|
Generator::message (mCode, mLiterals, "%f", 0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
throw std::runtime_error ("unknown expression result type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
||||||
Literals& literals, std::vector<Interpreter::Type_Code>& code)
|
Literals& literals, std::vector<Interpreter::Type_Code>& code, bool allowExpression)
|
||||||
: Parser (errorHandler, context), mLocals (locals), mLiterals (literals), mCode (code),
|
: Parser (errorHandler, context), mLocals (locals), mLiterals (literals), mCode (code),
|
||||||
mState (BeginState), mExprParser (errorHandler, context, locals, literals)
|
mState (BeginState), mExprParser (errorHandler, context, locals, literals),
|
||||||
|
mAllowExpression (allowExpression)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
||||||
{
|
{
|
||||||
|
if (mAllowExpression && mState==BeginState)
|
||||||
|
{
|
||||||
|
scanner.putbackInt (value, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return Parser::parseInt (value, loc, scanner);
|
return Parser::parseInt (value, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LineParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner)
|
bool LineParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner)
|
||||||
{
|
{
|
||||||
|
if (mAllowExpression && mState==BeginState)
|
||||||
|
{
|
||||||
|
scanner.putbackFloat (value, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return Parser::parseFloat (value, loc, scanner);
|
return Parser::parseFloat (value, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +177,29 @@ namespace Compiler
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mState==BeginState && mAllowExpression)
|
||||||
|
{
|
||||||
|
std::string name2 = toLower (name);
|
||||||
|
|
||||||
|
char type = mLocals.getType (name2);
|
||||||
|
|
||||||
|
if (type!=' ')
|
||||||
|
{
|
||||||
|
scanner.putbackName (name, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
type = getContext().getGlobalType (name2);
|
||||||
|
|
||||||
|
if (type!=' ')
|
||||||
|
{
|
||||||
|
scanner.putbackName (name, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (mState==BeginState && getContext().isId (name))
|
if (mState==BeginState && getContext().isId (name))
|
||||||
{
|
{
|
||||||
mState = PotentialExplicitState;
|
mState = PotentialExplicitState;
|
||||||
|
@ -172,6 +243,30 @@ namespace Compiler
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mAllowExpression)
|
||||||
|
{
|
||||||
|
if (keyword==Scanner::K_getdisabled || keyword==Scanner::K_getdistance)
|
||||||
|
{
|
||||||
|
scanner.putbackKeyword (keyword, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const Extensions *extensions = getContext().getExtensions())
|
||||||
|
{
|
||||||
|
char returnType;
|
||||||
|
std::string argumentType;
|
||||||
|
|
||||||
|
if (extensions->isFunction (keyword, returnType, argumentType,
|
||||||
|
!mExplicit.empty()))
|
||||||
|
{
|
||||||
|
scanner.putbackKeyword (keyword, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mState==BeginState)
|
if (mState==BeginState)
|
||||||
|
@ -233,12 +328,24 @@ namespace Compiler
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mAllowExpression)
|
||||||
|
{
|
||||||
|
if (keyword==Scanner::K_getsquareroot || keyword==Scanner::K_menumode ||
|
||||||
|
keyword==Scanner::K_random || keyword==Scanner::K_scriptrunning ||
|
||||||
|
keyword==Scanner::K_getsecondspassed)
|
||||||
|
{
|
||||||
|
scanner.putbackKeyword (keyword, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Parser::parseKeyword (keyword, loc, scanner);
|
return Parser::parseKeyword (keyword, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LineParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
bool LineParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
||||||
{
|
{
|
||||||
if (code==Scanner::S_newline && mState==EndState)
|
if (code==Scanner::S_newline && (mState==EndState || mState==BeginState))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (code==Scanner::S_comma && mState==MessageState)
|
if (code==Scanner::S_comma && mState==MessageState)
|
||||||
|
@ -253,6 +360,14 @@ namespace Compiler
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mAllowExpression && mState==BeginState &&
|
||||||
|
(code==Scanner::S_open || code==Scanner::S_minus))
|
||||||
|
{
|
||||||
|
scanner.putbackSpecial (code, loc);
|
||||||
|
parseExpression (scanner, loc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return Parser::parseSpecial (code, loc, scanner);
|
return Parser::parseSpecial (code, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,11 +35,17 @@ namespace Compiler
|
||||||
std::string mExplicit;
|
std::string mExplicit;
|
||||||
char mType;
|
char mType;
|
||||||
ExprParser mExprParser;
|
ExprParser mExprParser;
|
||||||
|
bool mAllowExpression;
|
||||||
|
|
||||||
|
void parseExpression (Scanner& scanner, const TokenLoc& loc);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
||||||
Literals& literals, std::vector<Interpreter::Type_Code>& code);
|
Literals& literals, std::vector<Interpreter::Type_Code>& code,
|
||||||
|
bool allowExpression = false);
|
||||||
|
///< \param allowExpression Allow lines consisting of a naked expression
|
||||||
|
/// (result is send to the messagebox interface)
|
||||||
|
|
||||||
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner);
|
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner);
|
||||||
///< Handle an int token.
|
///< Handle an int token.
|
||||||
|
|
Loading…
Reference in a new issue