forked from teamnwah/openmw-tes3coop
added parsing for local variable declarations
This commit is contained in:
parent
16f6f27a90
commit
bff0855af0
7 changed files with 78 additions and 6 deletions
|
@ -171,7 +171,8 @@ endif (APPLE)
|
|||
option(BUILD_MWCOMPILER "build standalone Morrowind script compiler" ON)
|
||||
|
||||
if (BUILD_MWCOMPILER)
|
||||
set(TOOLS_MWCOMPILER ${COMPILER} apps/mwcompiler/main.cpp)
|
||||
set(TOOLS_MWCOMPILER ${COMPILER} apps/mwcompiler/main.cpp apps/mwcompiler/context.cpp
|
||||
apps/mwcompiler/context.hpp)
|
||||
|
||||
add_executable(mwcompiler ${TOOLS_MWCOMPILER})
|
||||
endif()
|
||||
|
|
11
apps/mwcompiler/context.cpp
Normal file
11
apps/mwcompiler/context.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
|
||||
#include "context.hpp"
|
||||
|
||||
namespace SACompiler
|
||||
{
|
||||
bool Context::canDeclareLocals() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
18
apps/mwcompiler/context.hpp
Normal file
18
apps/mwcompiler/context.hpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef MWCOMPILER_CONTEXT_H_INCLUDED
|
||||
#define MWCOMPILER_CONTEXT_H_INCLUDED
|
||||
|
||||
#include <components/compiler/context.hpp>
|
||||
|
||||
namespace SACompiler
|
||||
{
|
||||
class Context : public Compiler::Context
|
||||
{
|
||||
public:
|
||||
|
||||
virtual bool canDeclareLocals() const;
|
||||
///< Is the compiler allowed to declare local variables?
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -7,14 +7,15 @@
|
|||
#include <components/compiler/streamerrorhandler.hpp>
|
||||
#include <components/compiler/scanner.hpp>
|
||||
#include <components/compiler/fileparser.hpp>
|
||||
#include <components/compiler/context.hpp>
|
||||
#include <components/compiler/exception.hpp>
|
||||
|
||||
#include "context.hpp"
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
Compiler::Context context;
|
||||
SACompiler::Context context;
|
||||
Compiler::StreamErrorHandler errorHandler (std::cout);
|
||||
Compiler::FileParser parser (errorHandler, context);
|
||||
|
||||
|
|
|
@ -5,7 +5,12 @@ namespace Compiler
|
|||
{
|
||||
class Context
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
virtual ~Context() {}
|
||||
|
||||
virtual bool canDeclareLocals() const = 0;
|
||||
///< Is the compiler allowed to declare local variables?
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
|
||||
#include "lineparser.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "scanner.hpp"
|
||||
#include "context.hpp"
|
||||
#include "errorhandler.hpp"
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
LineParser::LineParser (ErrorHandler& errorHandler, Context& context)
|
||||
: Parser (errorHandler, context)
|
||||
: Parser (errorHandler, context), mState (BeginState)
|
||||
{}
|
||||
|
||||
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
||||
|
@ -22,22 +26,45 @@ namespace Compiler
|
|||
bool LineParser::parseName (const std::string& name, const TokenLoc& loc,
|
||||
Scanner& scanner)
|
||||
{
|
||||
if (mState==ShortState || mState==LongState || mState==FloatState)
|
||||
{
|
||||
if (!getContext().canDeclareLocals())
|
||||
getErrorHandler().error ("local variables can't be declared in this context", loc);
|
||||
|
||||
std::cout << "declaring local variable: " << name << std::endl;
|
||||
mState = EndState;
|
||||
return true;
|
||||
}
|
||||
|
||||
return Parser::parseName (name, loc, scanner);
|
||||
}
|
||||
|
||||
bool LineParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
|
||||
{
|
||||
if (mState==BeginState)
|
||||
{
|
||||
switch (keyword)
|
||||
{
|
||||
case Scanner::K_short: mState = ShortState; return true;
|
||||
case Scanner::K_long: mState = LongState; return true;
|
||||
case Scanner::K_float: mState = FloatState; return true;
|
||||
}
|
||||
}
|
||||
|
||||
return Parser::parseKeyword (keyword, loc, scanner);
|
||||
}
|
||||
|
||||
bool LineParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
||||
{
|
||||
if (code==Scanner::S_newline && mState==EndState)
|
||||
return false;
|
||||
|
||||
return Parser::parseSpecial (code, loc, scanner);
|
||||
}
|
||||
|
||||
void LineParser::reset()
|
||||
{
|
||||
|
||||
mState = BeginState;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,15 @@ namespace Compiler
|
|||
|
||||
class LineParser : public Parser
|
||||
{
|
||||
enum State
|
||||
{
|
||||
BeginState,
|
||||
ShortState, LongState, FloatState,
|
||||
EndState
|
||||
};
|
||||
|
||||
State mState;
|
||||
|
||||
public:
|
||||
|
||||
LineParser (ErrorHandler& errorHandler, Context& context);
|
||||
|
|
Loading…
Reference in a new issue