diff options
author | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2025-05-22 13:23:18 +0200 |
---|---|---|
committer | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2025-05-22 13:23:18 +0200 |
commit | 6ef8829451871b534572d47312ae255dee088588 (patch) | |
tree | 3652795378f330452d8752dd9d1783a74a620d78 | |
parent | 027fc783faa9a25b373099e0e0f86d3859b7235d (diff) | |
download | imp-6ef8829451871b534572d47312ae255dee088588.tar.gz imp-6ef8829451871b534572d47312ae255dee088588.zip |
cleanup
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | examples/example.imp | 21 | ||||
-rw-r--r-- | examples/factorial.imp | 9 | ||||
-rw-r--r-- | src/driver.c | 8 | ||||
-rw-r--r-- | src/interpreter.c | 2 | ||||
-rw-r--r-- | src/repl.c | 14 |
7 files changed, 38 insertions, 32 deletions
@@ -53,7 +53,7 @@ $(LEXER_O): $(LEXER_C) $(CC) $(CFLAGS) -c $< -o $@ example: $(TARGET) - ./$(TARGET) -i example/example.imp + ./$(TARGET) -i examples/example.imp repl: $(TARGET) ./$(TARGET) @@ -34,7 +34,7 @@ All build artifacts are created in the build folder `./build`, including the imp ``` Usage: imp [ARGS] (no args) start REPL - -i <program.imp> interpret program and exit + -i <program.imp> interpret program -a <program.imp> print ast -h print this message ``` @@ -44,12 +44,12 @@ REPL: ``` IMP REPL (type IMP statements or commands starting with '%') Commands: - %quit exit - %run <path/to/file.imp> interpret program - %set <var> <val> set variable - %print [<var>] print variable, or all variables - %proc print declared procedures - %help show this message + %quit exit + %run <program.imp> interpret program + %set <var> <val> set variable + %print [<var>] print variable, or all variables + %procedures list declared procedures + %help show this message ``` ## IMP diff --git a/examples/example.imp b/examples/example.imp index a5bc5dd..de76b27 100644 --- a/examples/example.imp +++ b/examples/example.imp @@ -1,12 +1,11 @@ -procedure factorial(n;r) begin - if n <= 0 then - r := 1; - else - m := n - 1; - factorial(m;r); - r := r * n; - end; -end; - +f1 := 0; +f2 := 1; n := 5; -factorial(n;r);
\ No newline at end of file + +while n # 0 do + var tmp := f2 in + f2 := f1 + f2; + f1 := tmp; + end; + n := n - 1; +end;
\ No newline at end of file diff --git a/examples/factorial.imp b/examples/factorial.imp new file mode 100644 index 0000000..0ab74cd --- /dev/null +++ b/examples/factorial.imp @@ -0,0 +1,9 @@ +procedure factorial(n;r) begin + if n <= 0 then + r := 1; + else + m := n - 1; + factorial(m;r); + r := r * n; + end; +end;
\ No newline at end of file diff --git a/src/driver.c b/src/driver.c index 8902a55..441ee40 100644 --- a/src/driver.c +++ b/src/driver.c @@ -24,17 +24,15 @@ int main(int argc, char **argv) { while ((opt = getopt(argc, argv, "i:a:h")) != -1) { switch (opt) { case 'i': - interpret_file(optarg); - return EXIT_SUCCESS; + return interpret_file(optarg) ? EXIT_FAILURE : EXIT_SUCCESS; case 'a': - print_ast_file(optarg); - return EXIT_SUCCESS; + return print_ast_file(optarg) ? EXIT_FAILURE : EXIT_SUCCESS; case 'h': default: fprintf(stderr, "Usage: %s [ARGS]\n" " (no args) start REPL\n" - " -i <program.imp> interpret program and exit\n" + " -i <program.imp> interpret program\n" " -a <program.imp> print ast\n" " -h print this message\n", argv[0]); diff --git a/src/interpreter.c b/src/interpreter.c index 2de5b63..8176bad 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -92,8 +92,8 @@ void context_print_proc_table(Context *context) { vargs = vargs->next; if (vargs) printf(", "); } + printf(")\n"); } - printf(")\n"); hashmap_keys_iter_free(iter); } @@ -14,12 +14,12 @@ static void print_help(void) { printf( "IMP REPL (type IMP statements or commands starting with '%%')\n" "Commands:\n" - " %%quit exit\n" - " %%run <path/to/file.imp> interpret program\n" - " %%set <var> <val> set variable\n" - " %%print [<var>] print variable, or all variables\n" - " %%proc print declared procedures\n" - " %%help show this message\n"); + " %%quit exit\n" + " %%run <program.imp> interpret program\n" + " %%set <var> <val> set variable\n" + " %%print [<var>] print variable, or all variables\n" + " %%procedures list declared procedures\n" + " %%help show this message\n"); } static void repl_exec_command(context_t context, char *command) { @@ -44,7 +44,7 @@ static void repl_exec_command(context_t context, char *command) { char *var = strtok(NULL, " \t"); if (var) printf("%s = %d\n", var, context_get_var(context, var)); else context_print_var_table(context); - } else if (strcmp(cmd, "%proc") == 0) { + } else if (strcmp(cmd, "%procedures") == 0) { context_print_proc_table(context); } else { fprintf(stderr, "Unknown command: %s\n", cmd); |