write code file after successfull parsing

actorid
Marc Zinnschlag 15 years ago
parent 0cfeab622d
commit a3ad61b4d0

@ -75,9 +75,13 @@ set(COMPILER components/compiler/errorhandler.cpp
file(GLOB COMPILER_HEADER components/compiler/*.hpp) file(GLOB COMPILER_HEADER components/compiler/*.hpp)
source_group(compiler FILES ${COMPILER} ${COMPILER_HEADER}) 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 ${BSA} ${NIF} ${NIFOGRE} ${ESM_STORE} ${OGRE} ${INPUT} ${MISC} ${COMPILER})
set(COMPONENTS_HEADER ${BSA_HEADER} ${NIF_HEADER} ${NIFOGRE_HEADER} ${ESM_STORE_HEADER} 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 # source directory: libs

@ -3,7 +3,9 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector>
#include <components/interpreter/types.hpp>
#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>
@ -18,11 +20,11 @@ int main (int argc, char **argv)
SACompiler::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);
std::string filename = argc>1 ? argv[1] : "test.mwscript";
try try
{ {
std::string filename = argc>1 ? argv[1] : "test.mwscript";
std::ifstream file (filename.c_str()); std::ifstream file (filename.c_str());
if (!file.is_open()) if (!file.is_open())
@ -50,7 +52,14 @@ int main (int argc, char **argv)
if (errorHandler.isGood()) if (errorHandler.isGood())
{ {
std::cout << "parsed script: " << parser.getName() << std::endl; std::vector<Interpreter::Type_Code> code;
parser.getCode (code);
std::ofstream codeFile ((filename + ".code").c_str());
codeFile.write (reinterpret_cast<const char *> (&code[0]),
code.size()*sizeof (Interpreter::Type_Code));
return 0; return 0;
} }

@ -17,6 +17,11 @@ namespace Compiler
{ {
return mName; return mName;
} }
void FileParser::getCode (std::vector<Interpreter::Type_Code>& code) const
{
mScriptParser.getCode (code);
}
bool FileParser::parseName (const std::string& name, const TokenLoc& loc, bool FileParser::parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner) Scanner& scanner)

@ -29,6 +29,9 @@ namespace Compiler
std::string getName() const; std::string getName() const;
///< Return script name. ///< Return script name.
void getCode (std::vector<Interpreter::Type_Code>& code) const;
///< store generated code in \æ code.
virtual bool parseName (const std::string& name, const TokenLoc& loc, virtual bool parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner); Scanner& scanner);
///< Handle a name token. ///< Handle a name token.

@ -1,8 +1,6 @@
#include "lineparser.hpp" #include "lineparser.hpp"
#include <iostream>
#include "scanner.hpp" #include "scanner.hpp"
#include "context.hpp" #include "context.hpp"
#include "errorhandler.hpp" #include "errorhandler.hpp"
@ -11,8 +9,9 @@
namespace Compiler namespace Compiler
{ {
LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals) LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
: Parser (errorHandler, context), mLocals (locals), mState (BeginState) std::vector<Interpreter::Type_Code>& code)
: Parser (errorHandler, context), mLocals (locals), mCode (code), mState (BeginState)
{} {}
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
@ -48,7 +47,6 @@ namespace Compiler
return false; return false;
} }
std::cout << "declaring local variable: " << name << std::endl;
mLocals.declare (mState==ShortState ? 's' : (mState==LongState ? 'l' : 'f'), mLocals.declare (mState==ShortState ? 's' : (mState==LongState ? 'l' : 'f'),
name); name);

@ -1,6 +1,10 @@
#ifndef COMPILER_LINEPARSER_H_INCLUDED #ifndef COMPILER_LINEPARSER_H_INCLUDED
#define COMPILER_LINEPARSER_H_INCLUDED #define COMPILER_LINEPARSER_H_INCLUDED
#include <vector>
#include <components/interpreter/types.hpp>
#include "parser.hpp" #include "parser.hpp"
namespace Compiler namespace Compiler
@ -18,12 +22,14 @@ namespace Compiler
EndState EndState
}; };
Locals& mLocals; Locals& mLocals;
std::vector<Interpreter::Type_Code>& mCode;
State mState; State mState;
public: public:
LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals); LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
std::vector<Interpreter::Type_Code>& code);
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.

@ -7,10 +7,15 @@ namespace Compiler
{ {
ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context, ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context,
Locals& locals, bool end) Locals& locals, bool end)
: Parser (errorHandler, context), mLineParser (errorHandler, context, locals), : Parser (errorHandler, context), mLineParser (errorHandler, context, locals, mCode),
mLocals (locals), mEnd (end) mLocals (locals), mEnd (end)
{} {}
void ScriptParser::getCode (std::vector<Interpreter::Type_Code>& code) const
{
code = mCode;
}
bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc, bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner) Scanner& scanner)
{ {
@ -56,6 +61,7 @@ namespace Compiler
void ScriptParser::reset() void ScriptParser::reset()
{ {
mLineParser.reset(); mLineParser.reset();
mCode.clear();
} }
} }

@ -1,6 +1,10 @@
#ifndef COMPILER_SCRIPTPARSER_H_INCLUDED #ifndef COMPILER_SCRIPTPARSER_H_INCLUDED
#define COMPILER_SCRIPTPARSER_H_INCLUDED #define COMPILER_SCRIPTPARSER_H_INCLUDED
#include <vector>
#include <components/interpreter/types.hpp>
#include "parser.hpp" #include "parser.hpp"
#include "lineparser.hpp" #include "lineparser.hpp"
@ -15,6 +19,7 @@ namespace Compiler
LineParser mLineParser; LineParser mLineParser;
Locals& mLocals; Locals& mLocals;
bool mEnd; bool mEnd;
std::vector<Interpreter::Type_Code> mCode;
public: public:
@ -22,6 +27,9 @@ namespace Compiler
ScriptParser (ErrorHandler& errorHandler, Context& context, Locals& locals, ScriptParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
bool end = false); bool end = false);
void getCode (std::vector<Interpreter::Type_Code>& code) const;
///< store generated code in \æ code.
virtual bool parseName (const std::string& name, const TokenLoc& loc, virtual bool parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner); Scanner& scanner);
///< Handle a name token. ///< Handle a name token.

@ -0,0 +1,11 @@
#ifndef INTERPRETER_TYPES_H_INCLUDED
#define INTERPRETER_TYPES_H_INCLUDED
namespace Interpreter
{
typedef unsigned int Type_Code; // 32 bit
}
#endif
Loading…
Cancel
Save