From dc758810b463c1674991601edb0ba41d40831e7a Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 11 Oct 2021 09:59:42 +0200 Subject: Remove most code for step-by-step guide --- src/bootstrap/main.c | 124 +++++++-------------------------------------------- 1 file changed, 17 insertions(+), 107 deletions(-) (limited to 'src/bootstrap/main.c') diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index 66c3780..c98f60c 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -1,151 +1,59 @@ -#include #include +#include #include #include -#include - -#include "shorthand.h" -#include "string_view.c" -#include "readline.c" -#include "lexer.c" -#include "objects.c" -#include "parser.c" -#include "environment.c" -#include "primitives.c" - -// FIXME: We are not worried right now about freeing memory, but we should in -// the future. -// TODO: Better error messages. - -#define REPL_PROMPT "bdl> " void -init(void) { - // Clear env. - for (size_t i = 0; i < ENV_SIZE; i++) { - environment[i] = (EnvSymbol){0}; - } - - // Initialize singletons. - obj_nil = make_empty_list(); - obj_true = make_boolean(true); - obj_false = make_boolean(false); - - // Add primitive functions. - environment[env_n++] = (EnvSymbol){MAKE_SYM("+"), make_procedure(proc_add)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("-"), make_procedure(proc_sub)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("*"), make_procedure(proc_mul)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("/"), make_procedure(proc_div)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("%"), make_procedure(proc_mod)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("<"), make_procedure(proc_num_less_than)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM(">"), make_procedure(proc_num_greater_than)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("="), make_procedure(proc_num_equal)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("<="), make_procedure(proc_num_lesseq_than)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM(">="), make_procedure(proc_num_greatereq_than)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("null?"), make_procedure(proc_is_null)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("boolean?"), make_procedure(proc_is_boolean)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("symbol?"), make_procedure(proc_is_symbol)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("string?"), make_procedure(proc_is_string)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("fixnum?"), make_procedure(proc_is_fixnum)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("pair?"), make_procedure(proc_is_pair)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("procedure?"), make_procedure(proc_is_procedure)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("not"), make_procedure(proc_not)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("and"), make_procedure(proc_and)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("or"), make_procedure(proc_or)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("if"), make_procedure(proc_if)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("else"), obj_true}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("true"), obj_true}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("false"), obj_false}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("nil"), obj_nil}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("cond"), make_procedure(proc_cond)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("car"), make_procedure(proc_car)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("cdr"), make_procedure(proc_cdr)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("cons"), make_procedure(proc_cons)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("list"), make_procedure(proc_list)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("eq?"), make_procedure(proc_equal)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("display"), make_procedure(proc_display)}; - environment[env_n++] = (EnvSymbol){MAKE_SYM("print"), make_procedure(proc_print)}; +process_input(FILE *file) { + // TODO: Implement. + getchar(); + (void)file; } -void -eval_line(FILE *fd, char delimiter) { - StringView line = read_line(fd, delimiter); - Tokens tokens = tokenize(line); -#if DEBUG - printf("N_TOKENS: %ld\n", tokens.n); - for (size_t i = 0; i < tokens.n; i++) { - printf("\tTYPE: %3d ", tokens.start[i].type); - printf("N: %3ld ", tokens.start[i].value.n); - printf("VALUE: "); - sv_write(tokens.start[i].value); - printf("\n"); - } -#endif - while (tokens.n > 0) { - Object *ast = parse(&tokens); - if (ast) { -#if DEBUG - printf("AST: "); - display(ast); - printf("\n"); - printf("EVAL: "); -#endif - if (display(eval(ast))) { - printf("\n"); - }; - } - } -} +#define REPL_PROMPT "bdl> " void run_repl(void) { printf("BDL REPL (Press Ctrl-C to exit)\n"); while (true) { printf(REPL_PROMPT); - eval_line(stdin, '\n'); + process_input(stdin); } } void run_file(char *file_name) { -#if DEBUG - printf("Executing file: %s\n", file_name); -#endif FILE *fd = fopen(file_name, "r"); if (!fd) { - fprintf(stderr, "couldn't open file: %s\n", file_name); + fprintf(stderr, "error: couldn't open input file: %s\n", file_name); exit(EXIT_FAILURE); } - eval_line(fd, EOF); + process_input(fd); fclose(fd); } +#ifndef BIN_NAME +#define BIN_NAME "bdl" +#endif + void print_usage(void) { - printf("Usage: %s [options] \n", BIN_NAME); + printf("Usage: %s [options] \n", BIN_NAME); printf("\n"); printf("\t-i\tInteractive mode (REPL).\n"); - printf("\t-x\tExecute expression from stdin.\n"); printf("\n"); } int main(int argc, char *argv[]) { - init(); - int option; - while ((option = getopt(argc, argv, "ix")) != -1) { + while ((option = getopt(argc, argv, "i")) != -1) { switch (option) { case 'i': { // Interactive mode. run_repl(); return EXIT_SUCCESS; } break; - case 'x': { - // Execute expression from stdin. - eval_line(stdin, EOF); - return EXIT_SUCCESS; - } break; default: { print_usage(); return EXIT_FAILURE; @@ -153,6 +61,8 @@ main(int argc, char *argv[]) { } } + // TODO: Run from stdin if no file is given. + // Run from file. if (optind != argc - 1) { fprintf(stderr, "%s: No input file given.\n", BIN_NAME); -- cgit v1.2.1