Skip to content

Commit

Permalink
Merge pull request #119 from billhails/snippets
Browse files Browse the repository at this point in the history
better command-line option handling, new `--exec` option
  • Loading branch information
billhails authored Oct 8, 2024
2 parents 76aa340 + 086f163 commit ef6d9ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion fn/fact2.fn
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print(1000!);
print 1000!
73 changes: 55 additions & 18 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern bool assertions_failed;
extern int assertions_accumulate;
static char *binary_output_file = NULL;
static char *binary_input_file = NULL;
static char *snippet = NULL;

extern AstStringArray *include_paths;

Expand Down Expand Up @@ -86,24 +87,31 @@ static void report_build_mode(char *prog) {

static void usage(char *prog, int status) {
report_build_mode(prog);
printf("usage: %s <options> [<filename>] [<arguments> ...]\n", prog);
printf("usage: %s <options> [---] [<filename> [<arguments> ...]]\n", prog);
printf("options:\n%s",
" --anf Display the generated ANF.\n"
" --assertions-accumulate Don't exit on the first assertion failure.\n"
" --ast Display the parsed AST before lambda conversion.\n"
" --binary-in=file Read byte code from file.\n"
" --binary-out=file Write byte code to file.\n"
" --binary-in=<file> Read byte code from file.\n"
" --binary-out=<file> Write byte code to file.\n"
" --dump-bytecode Dump the generated bytecode.\n"
" -e<snippet>\n"
" --exec=<snippet> Execute the snippet of code directly\n"
" -h\n"
" --help This help.\n"
" --include=dir Add dir to the list of directories to be\n"
" -i<dir>\n"
" --include=<dir> Add dir to the list of directories to be\n"
" searched.\n"
" --inline-all Display the entire intermediate code\n"
" --inline Display the entire intermediate code\n"
" after inlining.\n"
" --lambda=function Display the intermediate code\n"
" -l<function>\n"
" --lambda=<function> Display the intermediate code\n"
" generated for the function.\n"
" --lambda-all Display the entire intermediate code.\n"
" -l\n"
" --lambda Display the entire intermediate code.\n"
" --report Report statistics.\n"
" --tpmc=function Produce a mermaid graph of the\n"
" -m <function>\n"
" --tpmc=<function> Produce a mermaid graph of the\n"
" function's TPMC state table.\n"
);
exit(status);
Expand All @@ -118,20 +126,20 @@ static int processArgs(int argc, char *argv[]) {
{ "anf", no_argument, &anf_flag, 1 },
{ "ast", no_argument, &ast_flag, 1 },
{ "dump-bytecode", no_argument, &dump_bytecode_flag, 1 },
{ "help", no_argument, &help_flag, 1 },
{ "lambda-all", no_argument, &lambda_flag, 1 },
{ "inline-all", no_argument, &inline_flag, 1 },
{ "exec", required_argument, 0, 'e' },
{ "help", no_argument, 0, 'h' },
{ "inline", no_argument, &inline_flag, 1 },
{ "assertions-accumulate", no_argument, &assertions_accumulate, 1 },
{ "tpmc", required_argument, 0, 'm' },
{ "lambda", required_argument, 0, 'l' },
{ "lambda", optional_argument, 0, 'l' },
{ "include", required_argument, 0, 'i' },
{ "binary-out", required_argument, 0, 'o' },
{ "binary-in", required_argument, 0, 'b' },
{ 0, 0, 0, 0 }
};
int option_index = 0;

c = getopt_long(argc, argv, "", long_options, &option_index);
c = getopt_long(argc, argv, "l::hm:e:i:o:b:", long_options, &option_index);

if (c == -1)
break;
Expand All @@ -141,7 +149,11 @@ static int processArgs(int argc, char *argv[]) {
}

if (c == 'l') {
lambda_conversion_function = optarg;
if (optarg) {
lambda_conversion_function = optarg;
} else {
lambda_flag = 1;
}
}

if (c == 'o') {
Expand All @@ -152,11 +164,15 @@ static int processArgs(int argc, char *argv[]) {
binary_input_file = optarg;
}

if (c == 'e') {
snippet = optarg;
}

if (c == 'i') {
pushAstStringArray(include_paths, strdup(optarg));
}

if (c == '?') {
if (c == '?' || c == 'h') {
help_flag = 1;
}
}
Expand All @@ -170,8 +186,8 @@ static int processArgs(int argc, char *argv[]) {
usage(argv[0], 1);
}

if (optind >= argc && !binary_input_file) {
eprintf("need filename or --binary-in argument\n");
if (optind >= argc && !binary_input_file && !snippet) {
eprintf("need filename or --binary-in or --e argument\n");
exit(1);
}

Expand All @@ -194,6 +210,22 @@ static AstProg *parseFile(char *file) {
return prog;
}

static AstProg *parseString(char *string, char*origin) {
AstProg *prog = prattParseString(string, origin);
int save = PROTECT(prog);
if (hadErrors()) {
exit(1);
}
if (ast_flag) {
PrattUTF8 *dest = newPrattUTF8();
PROTECT(dest);
ppAstProg(dest, prog);
printf("%s\n", dest->entries);
}
UNPROTECT(save);
return prog;
}

static LamExp *convertProg(AstProg *prog) {
LamExp *exp = lamConvertProg(prog);
int save = PROTECT(exp);
Expand Down Expand Up @@ -299,7 +331,12 @@ int main(int argc, char *argv[]) {
clock_t end = clock();
report(argv[0], begin, compiled, end);
} else {
AstProg *prog = parseFile(argv[nextargc++]);
AstProg *prog = NULL;
if (snippet) {
prog = parseString(snippet, "command-line");
} else {
prog = parseFile(argv[nextargc++]);
}
int save2 = PROTECT(prog);

LamExp *exp = convertProg(prog);
Expand Down

0 comments on commit ef6d9ba

Please sign in to comment.