diff --git a/CMakeLists.txt b/CMakeLists.txt index fb36ff9..c44851b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,12 @@ project(forth C) set(CMAKE_C_STANDARD 99) add_executable(forth - main.c - fh_builtins.c - fh_runtime.c - fh_stack.c - fh_mem.c + src/main.c + src/fh_builtins.c + src/fh_runtime.c + src/fh_stack.c + src/fh_mem.c ) + +target_include_directories(forth PRIVATE include) + diff --git a/build.sh b/build.sh index e4411be..61c1023 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/bash -cc *.c -o forth +cc -I include src/*.c -o forth diff --git a/forth b/forth index 86a2b5d..b4f3a2d 100755 Binary files a/forth and b/forth differ diff --git a/forth.h b/forth.h deleted file mode 100644 index 00e0d8f..0000000 --- a/forth.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * TODO file description - * - * Created on 2021/11/13. - */ - -#ifndef FORTH_H -#define FORTH_H - -#include -#include - -/* for printing */ -#include -#include - -#include "fh_config.h" -#include "fh_error.h" - -struct fh_thread_s; -struct fh_word_s; -struct fh_instruction_s; - -/** Word handler typedef */ -typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh); - -/** Forth runtime global state */ -struct fh_global_s { - /** Verbose logging enabled */ - bool verbose; - /** Interactive mode (i.e. not started with a file argument) */ - bool interactive; -}; - -extern struct fh_global_s fh_globals; - -/* logging */ -#define LOG(format, ...) do { if(fh_globals.verbose) { fprintf(stderr, format "\n", ##__VA_ARGS__); } } while (0) -#define LOGI(format, ...) fprintf(stderr, "\x1b[32m" format "\x1b[m\n", ##__VA_ARGS__) -#define LOGE(format, ...) fprintf(stderr, "\x1b[31;1m" format "\x1b[m\n", ##__VA_ARGS__) -/* Forth standard output. XXX should be stdout, but then colors get mangled if logging is used */ -#define FHPRINT(format, ...) fprintf(stderr, "\x1b[33;1m" format "\x1b[m", ##__VA_ARGS__) -#define FHPRINT_SVC(format, ...) fprintf(stderr, "" format "", ##__VA_ARGS__) - -enum fh_error fh_init_thread(struct fh_thread_s *fh); -enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf); - -#endif //FORTH_H diff --git a/fh_builtins.h b/include/fh_builtins.h similarity index 100% rename from fh_builtins.h rename to include/fh_builtins.h diff --git a/fh_config.h b/include/fh_config.h similarity index 100% rename from fh_config.h rename to include/fh_config.h diff --git a/fh_error.h b/include/fh_error.h similarity index 100% rename from fh_error.h rename to include/fh_error.h diff --git a/include/fh_globals.h b/include/fh_globals.h new file mode 100644 index 0000000..17c8683 --- /dev/null +++ b/include/fh_globals.h @@ -0,0 +1,20 @@ +/** + * Global state + * + * Created on 2021/11/13. + */ + +#ifndef FORTH_FH_GLOBALS_H +#define FORTH_FH_GLOBALS_H + +/** Forth runtime global state */ +struct fh_global_s { + /** Verbose logging enabled */ + bool verbose; + /** Interactive mode (i.e. not started with a file argument) */ + bool interactive; +}; + +extern struct fh_global_s fh_globals; + +#endif //FORTH_FH_GLOBALS_H diff --git a/fh_mem.h b/include/fh_mem.h similarity index 100% rename from fh_mem.h rename to include/fh_mem.h diff --git a/include/fh_print.h b/include/fh_print.h new file mode 100644 index 0000000..61307ec --- /dev/null +++ b/include/fh_print.h @@ -0,0 +1,24 @@ +/** + * Forth printing + * + * Created on 2021/11/13. + */ + +#ifndef FORTH_FH_PRINT_H +#define FORTH_FH_PRINT_H + +/* for printing */ +#include +#include +#include "fh_globals.h" + +/* logging */ +#define LOG(format, ...) do { if(fh_globals.verbose) { fprintf(stderr, format "\n", ##__VA_ARGS__); } } while (0) +#define LOGI(format, ...) fprintf(stderr, "\x1b[32m" format "\x1b[m\n", ##__VA_ARGS__) +#define LOGE(format, ...) fprintf(stderr, "\x1b[31;1m" format "\x1b[m\n", ##__VA_ARGS__) + +/* Forth standard output. XXX should be stdout, but then colors get mangled if logging is used */ +#define FHPRINT(format, ...) fprintf(stderr, "\x1b[33;1m" format "\x1b[m", ##__VA_ARGS__) +#define FHPRINT_SVC(format, ...) fprintf(stderr, "" format "", ##__VA_ARGS__) + +#endif //FORTH_FH_PRINT_H diff --git a/fh_runtime.h b/include/fh_runtime.h similarity index 95% rename from fh_runtime.h rename to include/fh_runtime.h index 605116d..3db86f6 100644 --- a/fh_runtime.h +++ b/include/fh_runtime.h @@ -9,6 +9,17 @@ #include #include +#include +#include + +#include "fh_config.h" + +struct fh_word_s; +struct fh_instruction_s; +struct fh_thread_s; + +/** Word handler typedef */ +typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh); /** Bytecode instruction type marker */ enum fb_instruction_kind { diff --git a/fh_stack.h b/include/fh_stack.h similarity index 93% rename from fh_stack.h rename to include/fh_stack.h index f5f7584..e513de7 100644 --- a/fh_stack.h +++ b/include/fh_stack.h @@ -1,5 +1,5 @@ /** - * Forth internal stack operations + * Forth stack operations * * Created on 2021/11/13. */ diff --git a/include/forth.h b/include/forth.h new file mode 100644 index 0000000..cd3af4b --- /dev/null +++ b/include/forth.h @@ -0,0 +1,21 @@ +/** + * Forth main entry-point + * + * Created on 2021/11/13. + */ + +#ifndef FORTH_H +#define FORTH_H + +#include +#include + +#include "fh_config.h" +#include "fh_error.h" + +struct fh_thread_s; + +enum fh_error fh_init(struct fh_thread_s *fh); +enum fh_error fh_process_line(struct fh_thread_s *fh, const char *linebuf); + +#endif //FORTH_H diff --git a/fh_builtins.c b/src/fh_builtins.c similarity index 97% rename from fh_builtins.c rename to src/fh_builtins.c index 7a08955..9c00a65 100644 --- a/fh_builtins.c +++ b/src/fh_builtins.c @@ -1,6 +1,9 @@ #include -#include "forth.h" +#include #include "fh_runtime.h" +#include "fh_config.h" +#include "fh_error.h" +#include "fh_print.h" #include "fh_builtins.h" #include "fh_stack.h" #include "fh_mem.h" diff --git a/fh_mem.c b/src/fh_mem.c similarity index 100% rename from fh_mem.c rename to src/fh_mem.c diff --git a/fh_runtime.c b/src/fh_runtime.c similarity index 97% rename from fh_runtime.c rename to src/fh_runtime.c index 3d4cb39..6c03b16 100644 --- a/fh_runtime.c +++ b/src/fh_runtime.c @@ -1,11 +1,14 @@ +#include +#include +#include + #include "forth.h" #include "fh_runtime.h" #include "fh_builtins.h" #include "fh_stack.h" #include "fh_mem.h" -#include -#include -#include +#include "fh_globals.h" +#include "fh_print.h" struct fh_global_s fh_globals = {}; @@ -168,7 +171,7 @@ enum fh_error w_user_word(struct fh_thread_s *fh) /** Initialize a runtime */ -enum fh_error fh_init_thread(struct fh_thread_s *fh) +enum fh_error fh_init(struct fh_thread_s *fh) { enum fh_error rv; @@ -184,7 +187,7 @@ enum fh_error fh_init_thread(struct fh_thread_s *fh) /** Process a quoted string read from input */ static enum fh_error fh_handle_quoted_string( struct fh_thread_s *fh, - char *start, + const char *start, size_t len ) { @@ -229,7 +232,7 @@ static enum fh_error fh_handle_quoted_string( /** Process a word read from input */ static enum fh_error fh_handle_word( struct fh_thread_s *fh, - char *start, + const char *start, size_t len ) { @@ -291,10 +294,10 @@ static inline bool isnl(char c) } /** Process a line read from input */ -enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf) +enum fh_error fh_process_line(struct fh_thread_s *fh, const char *linebuf) { enum fh_error rv; - char *rp = linebuf; + const char *rp = linebuf; char c; if (!fh_globals.interactive) { diff --git a/fh_stack.c b/src/fh_stack.c similarity index 96% rename from fh_stack.c rename to src/fh_stack.c index 1898550..aa2de25 100644 --- a/fh_stack.c +++ b/src/fh_stack.c @@ -1,6 +1,8 @@ -#include "forth.h" +#include "fh_error.h" +#include "fh_config.h" #include "fh_runtime.h" #include "fh_stack.h" +#include "fh_print.h" /** Pop from data stack */ enum fh_error ds_pop(struct fh_thread_s *fh, uint32_t *out) diff --git a/main.c b/src/main.c similarity index 95% rename from main.c rename to src/main.c index df7b18d..e376bc4 100644 --- a/main.c +++ b/src/main.c @@ -1,20 +1,18 @@ #include -#include #include -#include #include -#include #include #include #include "forth.h" #include "fh_runtime.h" +#include "fh_print.h" int main(int argc, char *argv[]) { enum fh_error rv; struct fh_thread_s fh; - rv = fh_init_thread(&fh); + rv = fh_init(&fh); if (rv != FH_OK) { LOGE("Error in forth init: %s", fherr_name(rv)); return 1;