-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor main into a compiler class (#37)
- Loading branch information
1 parent
4c83ea2
commit 8595de6
Showing
7 changed files
with
150 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "ANTLRFileStream.h" | ||
#include "FusionLexer.h" | ||
#include "ParseTree.h" | ||
#include "ast/builder.h" | ||
#include "ast/symbol/symbol_table.h" | ||
#include "backend/backend.h" | ||
|
||
using std::shared_ptr, std::unique_ptr; | ||
|
||
class Compiler { | ||
private: | ||
shared_ptr<SymbolTable> symbol_table; | ||
unique_ptr<Backend> backend; | ||
unique_ptr<Builder> builder; | ||
|
||
antlr4::ANTLRFileStream* file; | ||
fusion::FusionLexer* lexer; | ||
antlr4::tree::ParseTree* tree; | ||
antlr4::CommonTokenStream* tokens; | ||
fusion::FusionParser* parser; | ||
|
||
public: | ||
Compiler(std::string filename, | ||
shared_ptr<SymbolTable> symbol_table, | ||
unique_ptr<Backend> backend, | ||
unique_ptr<Builder> builder); | ||
~Compiler(); | ||
|
||
void build_ast(); | ||
void run_passes(); | ||
void xml(); | ||
|
||
void build_backend(); | ||
void to_object(std::string filename); | ||
void codegen(std::ofstream& outfile); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <cassert> | ||
|
||
#include "ANTLRFileStream.h" | ||
#include "CommonTokenStream.h" | ||
#include "FusionLexer.h" | ||
#include "FusionParser.h" | ||
#include "ast/passes/pass.h" | ||
#include "compiler.h" | ||
#include "errors.h" | ||
|
||
Compiler::Compiler(std::string filename, | ||
shared_ptr<SymbolTable> symbol_table, | ||
unique_ptr<Backend> backend, | ||
unique_ptr<Builder> builder) { | ||
this->symbol_table = symbol_table; | ||
this->backend = std::move(backend); | ||
this->builder = std::move(builder); | ||
|
||
file = new antlr4::ANTLRFileStream(); | ||
file->loadFromFile(filename); | ||
|
||
lexer = new fusion::FusionLexer(file); | ||
tokens = new antlr4::CommonTokenStream(lexer); | ||
parser = new fusion::FusionParser(tokens); | ||
|
||
tree = parser->file(); | ||
} | ||
|
||
Compiler::~Compiler() { | ||
delete file; | ||
delete lexer; | ||
delete tokens; | ||
delete parser; | ||
} | ||
|
||
void Compiler::build_ast() { | ||
try { | ||
builder->visit(tree); | ||
assert(builder->has_ast()); | ||
} catch (CompileTimeException const& e) { | ||
std::cerr << e.what() << std::endl; | ||
exit(1); | ||
} | ||
} | ||
|
||
void Compiler::run_passes() { | ||
try { | ||
Pass::run_passes(builder->get_ast(), symbol_table); | ||
assert(builder->has_ast()); | ||
} catch (CompileTimeException const& e) { | ||
std::cerr << e.what() << std::endl; | ||
exit(1); | ||
} | ||
} | ||
|
||
void Compiler::xml() { | ||
builder->get_ast()->xml(0); | ||
} | ||
|
||
void Compiler::build_backend() { | ||
backend->traverse(this->builder->get_ast()); | ||
} | ||
|
||
void Compiler::to_object(std::string filename) { | ||
backend->to_object(filename + ".o"); | ||
|
||
std::string command = "clang " + filename + ".o -o " + filename; | ||
system(command.c_str()); | ||
|
||
command = "rm " + filename + ".o"; | ||
system(command.c_str()); | ||
} | ||
|
||
void Compiler::codegen(std::ofstream& outfile) { | ||
backend->codegen(outfile); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters