mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 22:53:50 +00:00
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)
|
option(BUILD_MWCOMPILER "build standalone Morrowind script compiler" ON)
|
||||||
|
|
||||||
if (BUILD_MWCOMPILER)
|
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})
|
add_executable(mwcompiler ${TOOLS_MWCOMPILER})
|
||||||
endif()
|
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/streamerrorhandler.hpp>
|
||||||
#include <components/compiler/scanner.hpp>
|
#include <components/compiler/scanner.hpp>
|
||||||
#include <components/compiler/fileparser.hpp>
|
#include <components/compiler/fileparser.hpp>
|
||||||
#include <components/compiler/context.hpp>
|
|
||||||
#include <components/compiler/exception.hpp>
|
#include <components/compiler/exception.hpp>
|
||||||
|
|
||||||
|
#include "context.hpp"
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Compiler::Context context;
|
SACompiler::Context context;
|
||||||
Compiler::StreamErrorHandler errorHandler (std::cout);
|
Compiler::StreamErrorHandler errorHandler (std::cout);
|
||||||
Compiler::FileParser parser (errorHandler, context);
|
Compiler::FileParser parser (errorHandler, context);
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,12 @@ namespace Compiler
|
||||||
{
|
{
|
||||||
class Context
|
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 "lineparser.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "scanner.hpp"
|
#include "scanner.hpp"
|
||||||
|
#include "context.hpp"
|
||||||
|
#include "errorhandler.hpp"
|
||||||
|
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
LineParser::LineParser (ErrorHandler& errorHandler, Context& context)
|
LineParser::LineParser (ErrorHandler& errorHandler, Context& context)
|
||||||
: Parser (errorHandler, context)
|
: Parser (errorHandler, context), mState (BeginState)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
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,
|
bool LineParser::parseName (const std::string& name, const TokenLoc& loc,
|
||||||
Scanner& scanner)
|
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);
|
return Parser::parseName (name, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LineParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& 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);
|
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)
|
||||||
|
return false;
|
||||||
|
|
||||||
return Parser::parseSpecial (code, loc, scanner);
|
return Parser::parseSpecial (code, loc, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineParser::reset()
|
void LineParser::reset()
|
||||||
{
|
{
|
||||||
|
mState = BeginState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,15 @@ namespace Compiler
|
||||||
|
|
||||||
class LineParser : public Parser
|
class LineParser : public Parser
|
||||||
{
|
{
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
BeginState,
|
||||||
|
ShortState, LongState, FloatState,
|
||||||
|
EndState
|
||||||
|
};
|
||||||
|
|
||||||
|
State mState;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
LineParser (ErrorHandler& errorHandler, Context& context);
|
LineParser (ErrorHandler& errorHandler, Context& context);
|
||||||
|
|
Loading…
Reference in a new issue