1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <iostream>
- #include <string>
- #include <fstream>
- #include "Parser.h"
- #include "Optimizer.h"
- #include "AssemblyGenerator.h"
- using namespace std;
- using zp::Parser;
- using zp::Optimizer;
- using zp::AssemblyGenerator;
- int main(int argc, char** argv)
- {
- string filename = "";
- string outfilename = "";
- bool outputAssembly = false;
- for (int i = 1; i < argc; i++) {
- string arg = argv[i];
- if (arg == "") {
- }
- else if (arg[0] == '-') {
- if (arg == "-o" && argc > i + 1) {
- outfilename = argv[i + 1];
- i++;
- }
- else if (arg == "-S") {
- outputAssembly = true;
- }
- }
- else {
- filename = arg;
- }
- }
- istream* in = &cin;
- ostream* out = &cout;
- if (filename != "") {
- in = new ifstream{filename.c_str(), fstream::in};
- out = new ofstream{(filename + ".s").c_str(), fstream::out};
- }
- Parser parser{ *in };
- unique_ptr<zp::Block> ast;
- try {
- ast = parser.parse();
- }
- catch(zp::ParserException& pe) {
- cerr << "Error: " << pe.what() << endl;
- return 1;
- }
- Optimizer optimizer;
- auto ir = ast->accept(optimizer);
-
- AssemblyGenerator ag{ *out };
- ir->accept(ag);
- ag.finish();
- if (filename != "") {
- dynamic_cast<ifstream*> (in)->close();
- dynamic_cast<ofstream*> (out)->close();
- delete in;
- delete out;
- in = &cin;
- out = &cout;
- }
- }
|