From cf1a0cd99bb7d277da7ce680db9c2dfe1fe74c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 13 Nov 2021 17:35:20 +0100 Subject: [PATCH] move to folders, more cleaning and fixes --- CMakeLists.txt | 13 ++++--- build.sh | 2 +- forth | Bin 27080 -> 27072 bytes forth.h | 48 ------------------------- fh_builtins.h => include/fh_builtins.h | 0 fh_config.h => include/fh_config.h | 0 fh_error.h => include/fh_error.h | 0 include/fh_globals.h | 20 +++++++++++ fh_mem.h => include/fh_mem.h | 0 include/fh_print.h | 24 +++++++++++++ fh_runtime.h => include/fh_runtime.h | 11 ++++++ fh_stack.h => include/fh_stack.h | 2 +- include/forth.h | 21 +++++++++++ fh_builtins.c => src/fh_builtins.c | 5 ++- fh_mem.c => src/fh_mem.c | 0 fh_runtime.c => src/fh_runtime.c | 19 +++++----- fh_stack.c => src/fh_stack.c | 4 ++- main.c => src/main.c | 6 ++-- 18 files changed, 106 insertions(+), 69 deletions(-) delete mode 100644 forth.h rename fh_builtins.h => include/fh_builtins.h (100%) rename fh_config.h => include/fh_config.h (100%) rename fh_error.h => include/fh_error.h (100%) create mode 100644 include/fh_globals.h rename fh_mem.h => include/fh_mem.h (100%) create mode 100644 include/fh_print.h rename fh_runtime.h => include/fh_runtime.h (95%) rename fh_stack.h => include/fh_stack.h (93%) create mode 100644 include/forth.h rename fh_builtins.c => src/fh_builtins.c (97%) rename fh_mem.c => src/fh_mem.c (100%) rename fh_runtime.c => src/fh_runtime.c (97%) rename fh_stack.c => src/fh_stack.c (96%) rename main.c => src/main.c (95%) 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 86a2b5d0c2308c5c5aadb37c2734e1b8e8d88690..b4f3a2d26b7c9951bbea6ceb38c2b41f7bbd92e7 100755 GIT binary patch delta 663 zcmY*XO(;Y`9G`h>U9jyg8jrRm%2&Q39LR1?dmu}_XLrkbLf!$XRoIh@k3#snh-7mS z<)b~@D8-HCKn}P`E-1Mu=Dqn}%whg-eqS^5pLcmnPjBgR3H2P8hAmmH_TTibVqfFe z-bLBNcI@Up`!W>Yv_v|kqy$c7iOxezi9(&4<+zdvaS|JOEhOgfgI9IaJJ3`{pTMgy z`f1J?@WGLMI8&KiV-cRHOL(H9kY+<8cOd&xA>ealRWo7SoM*w`&Ez#y?pOX-ZWtQt zA!qa9SjwacGdIcC4k?H1_F^EGM}zgfV^+Caz=;%5e+(C~6XFqTgh?9zFz$U+FYp0T zNFI0tOs++CsZ_uLw6(%mpo%IGvDzUtRhZBNNzP7SLTa9Xn~kYIBVtVl($)kWD delta 678 zcmY*XPbkAt9DnaOTe-+)y!}Y?uULywLUOQ@KPv|g``OkuKW*9$CfdmqZZ6jJMTrZg z#Dx|Y5k-`f95~SqxG4_gg7^EqZ^=8n?|YwrzxTescX~^&ZfT~D_U=2v2J7-`Zh5%2 z=i}(7Bfj!IIe$}qU7TGvBnKrA1&cRLGqB-JL9=2t-${fN5*x{=L=12%cKQIS!ss)& zy^Mb8vlZSrQVVAaliLi0SV#d$l?zJ>tzDW%R*-$E7w|czc{Pa50ziCIp&fKT#7HSlWYHzb{6k1(u zvxP+d>pHSq(8Spt^!8(au|4EKKieg;tEIv;hTc0^^fUUXY3Mtg`I+&XD@6xw@Wyj% zjN;DYc+_KbI-xSaZSU8@DC&S6uWK@li fa5SPO)odbU$;-}+ -#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;