jitters: add input file option

This commit is contained in:
Bruno BELANYI 2020-10-02 14:37:11 +02:00
parent ce9b80b32f
commit c9628162bd

View file

@ -15,6 +15,8 @@
#include "print/printer.h" #include "print/printer.h"
#include "vm/vm.h" #include "vm/vm.h"
extern FILE *yyin; // Flex input file
const char *argp_program_version = PACKAGE_STRING; const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
@ -28,6 +30,7 @@ static struct argp_option options[] = {
{"compile", 'c', 0, 0, "Compile input to assembly", 0 }, {"compile", 'c', 0, 0, "Compile input to assembly", 0 },
{"jit", 'j', 0, 0, "JIT-compile input and evaluate", 0 }, {"jit", 'j', 0, 0, "JIT-compile input and evaluate", 0 },
{"print", 'p', 0, 0, "Print parsed expression", 0 }, {"print", 'p', 0, 0, "Print parsed expression", 0 },
{"input", 'i', "FILE", 0, "Input from FILE instead of standard input", 0 },
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output", 0 }, {"output", 'o', "FILE", 0, "Output to FILE instead of standard output", 0 },
{"debug", 'd', 0, 0, "Emit debug output from parser", 0 }, {"debug", 'd', 0, 0, "Emit debug output from parser", 0 },
{ 0 } { 0 }
@ -41,6 +44,7 @@ struct arguments
bool virtual; // Whether to run on VM bool virtual; // Whether to run on VM
bool evaluate; // Whether to evaluate the input bool evaluate; // Whether to evaluate the input
bool print; // Whether to print the input bool print; // Whether to print the input
const char *input_file; // Where to input
const char *output_file; // Where to output const char *output_file; // Where to output
}; };
@ -59,6 +63,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 'e': case 'e':
arguments->evaluate = true; arguments->evaluate = true;
break; break;
case 'i':
arguments->input_file = arg;
break;
case 'j': case 'j':
arguments->jit = true; arguments->jit = true;
break; break;
@ -90,6 +97,7 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
struct arguments arguments = { struct arguments arguments = {
.output_file = "-", .output_file = "-",
.input_file = "-",
}; };
FILE *output = stdout; FILE *output = stdout;
@ -99,6 +107,8 @@ int main(int argc, char *argv[])
if (strcmp(arguments.output_file, "-")) if (strcmp(arguments.output_file, "-"))
output = fopen(arguments.output_file, "w"); output = fopen(arguments.output_file, "w");
if (strcmp(arguments.input_file, "-"))
yyin = fopen(arguments.input_file, "r");
if ((ret = yyparse(&ast)) == 0) { if ((ret = yyparse(&ast)) == 0) {
if (arguments.compile) if (arguments.compile)
@ -114,6 +124,8 @@ int main(int argc, char *argv[])
} }
destroy_ast(ast); destroy_ast(ast);
if (strcmp(arguments.input_file, "-"))
fclose(yyin);
if (strcmp(arguments.output_file, "-")) if (strcmp(arguments.output_file, "-"))
fclose(output); fclose(output);