diff --git a/CMakeLists.txt b/CMakeLists.txt index a3944ba57..c3a14278e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,9 +75,13 @@ set(COMPILER components/compiler/errorhandler.cpp file(GLOB COMPILER_HEADER components/compiler/*.hpp) source_group(compiler FILES ${COMPILER} ${COMPILER_HEADER}) +file(GLOB INTERPRETER_HEADER components/interpreter/*.hpp) +source_group(interpreter FILES ${INTERPRETER_HEADER}) + set(COMPONENTS ${BSA} ${NIF} ${NIFOGRE} ${ESM_STORE} ${OGRE} ${INPUT} ${MISC} ${COMPILER}) set(COMPONENTS_HEADER ${BSA_HEADER} ${NIF_HEADER} ${NIFOGRE_HEADER} ${ESM_STORE_HEADER} - ${ESM_HEADER} ${OGRE_HEADER} ${INPUT_HEADER} ${MISC_HEADER} ${COMPILER_HEADER}) + ${ESM_HEADER} ${OGRE_HEADER} ${INPUT_HEADER} ${MISC_HEADER} ${COMPILER_HEADER} + ${INTERPRETER_HEADER}) # source directory: libs diff --git a/apps/mwcompiler/main.cpp b/apps/mwcompiler/main.cpp index a4ec9a573..d55fe3a42 100644 --- a/apps/mwcompiler/main.cpp +++ b/apps/mwcompiler/main.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +#include #include #include #include @@ -18,11 +20,11 @@ int main (int argc, char **argv) SACompiler::Context context; Compiler::StreamErrorHandler errorHandler (std::cout); Compiler::FileParser parser (errorHandler, context); - + + std::string filename = argc>1 ? argv[1] : "test.mwscript"; + try { - std::string filename = argc>1 ? argv[1] : "test.mwscript"; - std::ifstream file (filename.c_str()); if (!file.is_open()) @@ -50,7 +52,14 @@ int main (int argc, char **argv) if (errorHandler.isGood()) { - std::cout << "parsed script: " << parser.getName() << std::endl; + std::vector code; + parser.getCode (code); + + std::ofstream codeFile ((filename + ".code").c_str()); + + codeFile.write (reinterpret_cast (&code[0]), + code.size()*sizeof (Interpreter::Type_Code)); + return 0; } diff --git a/components/compiler/fileparser.cpp b/components/compiler/fileparser.cpp index 676bbec95..59ec90286 100644 --- a/components/compiler/fileparser.cpp +++ b/components/compiler/fileparser.cpp @@ -17,6 +17,11 @@ namespace Compiler { return mName; } + + void FileParser::getCode (std::vector& code) const + { + mScriptParser.getCode (code); + } bool FileParser::parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner) diff --git a/components/compiler/fileparser.hpp b/components/compiler/fileparser.hpp index dc0353ea4..55b5a1509 100644 --- a/components/compiler/fileparser.hpp +++ b/components/compiler/fileparser.hpp @@ -29,6 +29,9 @@ namespace Compiler std::string getName() const; ///< Return script name. + void getCode (std::vector& code) const; + ///< store generated code in \æ code. + virtual bool parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner); ///< Handle a name token. diff --git a/components/compiler/lineparser.cpp b/components/compiler/lineparser.cpp index 6eda26748..7baf81f24 100644 --- a/components/compiler/lineparser.cpp +++ b/components/compiler/lineparser.cpp @@ -1,8 +1,6 @@ #include "lineparser.hpp" -#include - #include "scanner.hpp" #include "context.hpp" #include "errorhandler.hpp" @@ -11,8 +9,9 @@ namespace Compiler { - LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals) - : Parser (errorHandler, context), mLocals (locals), mState (BeginState) + LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals, + std::vector& code) + : Parser (errorHandler, context), mLocals (locals), mCode (code), mState (BeginState) {} bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) @@ -48,7 +47,6 @@ namespace Compiler return false; } - std::cout << "declaring local variable: " << name << std::endl; mLocals.declare (mState==ShortState ? 's' : (mState==LongState ? 'l' : 'f'), name); diff --git a/components/compiler/lineparser.hpp b/components/compiler/lineparser.hpp index 3c2ed309f..d7840af71 100644 --- a/components/compiler/lineparser.hpp +++ b/components/compiler/lineparser.hpp @@ -1,6 +1,10 @@ #ifndef COMPILER_LINEPARSER_H_INCLUDED #define COMPILER_LINEPARSER_H_INCLUDED +#include + +#include + #include "parser.hpp" namespace Compiler @@ -18,12 +22,14 @@ namespace Compiler EndState }; - Locals& mLocals; + Locals& mLocals; + std::vector& mCode; State mState; public: - LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals); + LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals, + std::vector& code); virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner); ///< Handle an int token. diff --git a/components/compiler/scriptparser.cpp b/components/compiler/scriptparser.cpp index fb3d47e66..338f4aecf 100644 --- a/components/compiler/scriptparser.cpp +++ b/components/compiler/scriptparser.cpp @@ -7,10 +7,15 @@ namespace Compiler { ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context, Locals& locals, bool end) - : Parser (errorHandler, context), mLineParser (errorHandler, context, locals), + : Parser (errorHandler, context), mLineParser (errorHandler, context, locals, mCode), mLocals (locals), mEnd (end) {} + void ScriptParser::getCode (std::vector& code) const + { + code = mCode; + } + bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner) { @@ -56,6 +61,7 @@ namespace Compiler void ScriptParser::reset() { mLineParser.reset(); + mCode.clear(); } } diff --git a/components/compiler/scriptparser.hpp b/components/compiler/scriptparser.hpp index 45c637947..dd31f0217 100644 --- a/components/compiler/scriptparser.hpp +++ b/components/compiler/scriptparser.hpp @@ -1,6 +1,10 @@ #ifndef COMPILER_SCRIPTPARSER_H_INCLUDED #define COMPILER_SCRIPTPARSER_H_INCLUDED +#include + +#include + #include "parser.hpp" #include "lineparser.hpp" @@ -15,6 +19,7 @@ namespace Compiler LineParser mLineParser; Locals& mLocals; bool mEnd; + std::vector mCode; public: @@ -22,6 +27,9 @@ namespace Compiler ScriptParser (ErrorHandler& errorHandler, Context& context, Locals& locals, bool end = false); + void getCode (std::vector& code) const; + ///< store generated code in \æ code. + virtual bool parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner); ///< Handle a name token. diff --git a/components/interpreter/types.hpp b/components/interpreter/types.hpp new file mode 100644 index 000000000..8ae59b115 --- /dev/null +++ b/components/interpreter/types.hpp @@ -0,0 +1,11 @@ +#ifndef INTERPRETER_TYPES_H_INCLUDED +#define INTERPRETER_TYPES_H_INCLUDED + +namespace Interpreter +{ + typedef unsigned int Type_Code; // 32 bit + +} + +#endif +