-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cpp
More file actions
52 lines (42 loc) · 1.95 KB
/
Compiler.cpp
File metadata and controls
52 lines (42 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Compiler.h"
#include "Bytecode/BytecodeVisitor.h"
#include "Optimisations/ConstantFoldingVisitor.h"
namespace Noble::Compiler
{
bool Compiler::Compile(const std::string& NGPLSource, const std::string& frameName)
{
//We use the compiler's copy of the source and keep it around as long as the compiler it around
//because the Lexer and Parser's Tokens point directly to the source for their lexemes.
source = NGPLSource;
Lexer lexer;
Parser parser;
Frame frame;
//Optimisations::ConstantFoldingVisitor constantRollerVisitor;
Bytecode::BytecodeVisitor bytecodeVisitor;
const std::vector<Token> tokens = lexer.Lex(source.c_str());
Debug::MakeTokenFile(tokens, frameName);
std::vector<AST::StatementPtr> AST = parser.Parse(tokens);
//constantRollerVisitor.FoldConstants(AST);
bytecodeVisitor.GenerateOps(AST, frame);
return WriteFrame(frame, frameName) && Debug::MakeDebugFile(frame, frameName);
}
bool Compiler::WriteFrame(const Frame &frame, const std::string& name)
{
std::ofstream ops(name + ".naf", std::ios::trunc | std::ios::binary);
if (!ops.is_open())
{
std::cerr << "Error: Unable to open assembly file '" << name << ".naf' while writing frame.\n";
return false;
}
ops.write(reinterpret_cast<const std::ostream::char_type*>(frame.GetOps().GetArray()), frame.GetOps().Count() * sizeof(Core::Op::Type));
ops.close();
std::ofstream constants(name + ".ndf", std::ios::trunc | std::ios::binary);
if (!constants.is_open())
{
std::cerr << "Error: Unable to open data file '" << name << ".ndf' while writing frame.\n";
return false;
}
constants.write(reinterpret_cast<const std::ostream::char_type*>(frame.GetConstants().GetArray()), frame.GetConstants().Count() * sizeof(ValueType));
return true;
}
}