From 154b1c4dccf6cb61c3d6ee2e5e6539bd04df4867 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Wed, 13 Nov 2024 21:37:49 +0000 Subject: [PATCH] frontend: compile: pass all input files through the lexer --- ivy/CMakeLists.txt | 4 +++ ivy/cmd/compile.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/ivy/CMakeLists.txt b/ivy/CMakeLists.txt index c926bc8..9e9d7a4 100644 --- a/ivy/CMakeLists.txt +++ b/ivy/CMakeLists.txt @@ -3,6 +3,10 @@ file(GLOB_RECURSE ivy_sources *.c *.h) add_executable(ivy ${ivy_sources}) target_link_libraries( ivy + ivy-rt + ivy-asm + ivy-lang + ivy-common Bluelib::Core Bluelib::Object Bluelib::Cmd) diff --git a/ivy/cmd/compile.c b/ivy/cmd/compile.c index 55b6f85..85e10c6 100644 --- a/ivy/cmd/compile.c +++ b/ivy/cmd/compile.c @@ -1,6 +1,78 @@ #include "cmd.h" #include +#include +#include +#include +#include +#include + +enum { + ARG_SOURCE_FILE, +}; + +static int compile_file(const char *path) +{ + + FILE *fp = fopen(path, "r"); + if (!fp) { + b_err("cannot open source file '%s'", path); + b_i("reason: %s", strerror(errno)); + return -1; + } + + struct ivy_file *src = ivy_file_from_fp(fp); + struct ivy_lexer lex; + if (ivy_lexer_init(&lex) != IVY_OK) { + b_err("failed to initialise Ivy lexer"); + ivy_file_close(src); + return -1; + } + + lex.lex_source = &src->f_base; + + while (true) { + struct ivy_token *tok = ivy_lexer_read(&lex); + if (lex.lex_status == IVY_ERR_EOF) { + break; + } + + if (lex.lex_status != IVY_OK) { + b_err("failed to parse '%s'", path); + b_i("reason: lex error (%s)", + ivy_status_to_string(lex.lex_status)); + break; + } + + printf("read token!\n"); + } + + int r = (lex.lex_status == IVY_OK || lex.lex_status == IVY_ERR_EOF) ? 0 + : -1; + ivy_file_close(src); + ivy_lexer_finish(&lex); + return r; +} + +static int compile(const b_command *cmd, const b_arglist *args, const b_array *_) +{ + b_arglist_iterator it; + b_arglist_foreach_filtered(&it, args, B_COMMAND_INVALID_ID, ARG_SOURCE_FILE) + { + b_arglist_value *path = it.value; + if (path->val_type != B_COMMAND_ARG_STRING) { + continue; + } + + printf("%s\n", path->val_str); + int r = compile_file(path->val_str); + if (r != 0) { + return r; + } + } + + return 0; +} B_COMMAND(CMD_COMPILE, CMD_ROOT) { @@ -9,6 +81,14 @@ B_COMMAND(CMD_COMPILE, CMD_ROOT) B_COMMAND_DESC( "compile one or more Ivy source files into Ivy object files."); B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT); + B_COMMAND_FUNCTION(compile); + + B_COMMAND_ARG(ARG_SOURCE_FILE) + { + B_ARG_NAME("source file"); + B_ARG_DESC("the .im source files to compile."); + B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES); + } B_COMMAND_HELP_OPTION(); }