From ae7c1f98d96e535af5e90a933d9ae89a18475d20 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 27 Nov 2014 20:42:55 +0100 Subject: [PATCH] Optionally read and write to file. --- main.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index a9e6909..5fd9db4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,18 @@ #include "Monicelli.tab.h" #include +#include + #include +#include using namespace monicelli; int lineNumber = 1; Program *program; +extern FILE *monicelli_in; + void monicelli_error(const char *message) { std::cerr << "At line " << lineNumber << ": " << message << std::endl; @@ -21,16 +26,32 @@ void monicelli_meta(const char *text) { std::cerr << "META: " << text << std::endl; } -int main() { +int main(int argc, char **argv) { #if YYDEBUG yydebug = 1; #endif + bool fromFile = argc > 1; + bool toFile = argc > 2; + program = new Program(); + + if (fromFile) { + monicelli_in = fopen(argv[1], "r"); + } monicelli_parse(); - - program->emit(std::cout); - + if (fromFile) { + fclose(monicelli_in); + } + if (toFile) { + std::ofstream out(argv[2]); + program->emit(out); + out.close(); + } + else { + program->emit(std::cout); + } + return 0; }