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)
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

@ -3,7 +3,9 @@
#include <exception>
#include <iostream>
#include <fstream>
#include <vector>
#include <components/interpreter/types.hpp>
#include <components/compiler/streamerrorhandler.hpp>
#include <components/compiler/scanner.hpp>
#include <components/compiler/fileparser.hpp>
@ -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<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;
}

@ -17,6 +17,11 @@ namespace Compiler
{
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,
Scanner& scanner)

@ -29,6 +29,9 @@ namespace Compiler
std::string getName() const;
///< 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,
Scanner& scanner);
///< Handle a name token.

@ -1,8 +1,6 @@
#include "lineparser.hpp"
#include <iostream>
#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<Interpreter::Type_Code>& 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);

@ -1,6 +1,10 @@
#ifndef COMPILER_LINEPARSER_H_INCLUDED
#define COMPILER_LINEPARSER_H_INCLUDED
#include <vector>
#include <components/interpreter/types.hpp>
#include "parser.hpp"
namespace Compiler
@ -18,12 +22,14 @@ namespace Compiler
EndState
};
Locals& mLocals;
Locals& mLocals;
std::vector<Interpreter::Type_Code>& mCode;
State mState;
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);
///< Handle an int token.

@ -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<Interpreter::Type_Code>& 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();
}
}

@ -1,6 +1,10 @@
#ifndef COMPILER_SCRIPTPARSER_H_INCLUDED
#define COMPILER_SCRIPTPARSER_H_INCLUDED
#include <vector>
#include <components/interpreter/types.hpp>
#include "parser.hpp"
#include "lineparser.hpp"
@ -15,6 +19,7 @@ namespace Compiler
LineParser mLineParser;
Locals& mLocals;
bool mEnd;
std::vector<Interpreter::Type_Code> mCode;
public:
@ -22,6 +27,9 @@ namespace Compiler
ScriptParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
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,
Scanner& scanner);
///< 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