From 0373b731c28ae6c8c1761796b0a2ea88f15b22de Mon Sep 17 00:00:00 2001 From: Bryce Carson Date: Wed, 25 Sep 2024 22:02:07 -0600 Subject: [PATCH] Checkin all changes related to fixing build-time issues The only build-time error encountered is the following, now: /home/bryce/src/c/TIC-80/src/api/r.c:175:10: fatal error: ../build/assets/rdemo.tic.dat: No such file or directory 175 | #include "../build/assets/rdemo.tic.dat" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. make[2]: *** [CMakeFiles/r.dir/build.make:76: CMakeFiles/r.dir/src/api/r.c.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:283: CMakeFiles/r.dir/all] Error 2 make: *** [Makefile:156: all] Error 2 --- cmake/r.cmake | 15 ++++++++-- src/api/r.c | 65 +++++++++++++++++++++------------------- src/api/r.org | 83 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 100 insertions(+), 63 deletions(-) diff --git a/cmake/r.cmake b/cmake/r.cmake index f01af8751..2a310e57d 100644 --- a/cmake/r.cmake +++ b/cmake/r.cmake @@ -11,7 +11,7 @@ if(BUILD_WITH_R AND NOT N3DS AND NOT BAREMETALAPI) endif() if(NOT DEFINED ENV{LD_LIBRARY_PATH} AND NOT LD_LIBRARY_PATH) - set(LD_LIBRARY_PATH "${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server") + set(LD_LIBRARY_PATH "/usr/include/R:${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server") else() # Add these entries to the this *PATH variable set(LD_LIBRARY_PATH "${LD_LIBRARY_PATH}:${PREFIX}/lib64/R/lib:${PREFIX}/lib/jvm/jre/lib/server") @@ -34,7 +34,18 @@ if(BUILD_WITH_R AND NOT N3DS AND NOT BAREMETALAPI) list(APPEND R_SRC "${PREFIX}/lib/jvm/jre/lib/server" R_SHARE_DIR R_DOC_DIR) endif() - list(APPEND R_SRC ${CMAKE_SOURCE_DIR}/src/api/r.c) + set(R_LIB_DIR "/usr/include/R") + include_directories(SYSTEM "/usr/include/R") + list( + APPEND R_SRC + ${CMAKE_SOURCE_DIR}/src/api/r.c + ${R_LIB_DIR}/R.h + ${R_LIB_DIR}/Rembedded.h + ${R_LIB_DIR}/Rinterface.h + ${R_LIB_DIR}/Rinternals.h + ${R_LIB_DIR}/Rconfig.h + ${R_LIB_DIR}/Rdefines.h + ) add_library(r ${TIC_RUNTIME} ${R_SRC}) set_target_properties(r PROPERTIES PREFIX "") diff --git a/src/api/r.c b/src/api/r.c index 7c1b5dbaa..56d949528 100644 --- a/src/api/r.c +++ b/src/api/r.c @@ -35,36 +35,29 @@ #include #include -tic_core *getTICCore(tic_mem* tic, const char* code) { - tic_core *core; - while (core == NULL && (!initR(tic, code))) { - core = (castTicMemory)->currentVM; - } - return core; -} - void evalR(tic_mem *memory, char *code) { - Rf_eval(Rf_mkString(code)); + SEXP result = Rf_eval(Rf_mkString(code), R_GlobalEnv); } -#define killer(x) \ - if ((tic_core *core = (castTicMemory)->currentVM) != NULL) { \ - Rf_endEmbeddedR(x); \ - core->currentVM = NULL; \ +#define killer \ + tic_core *core; \ + if ((core = (((tic_core *) tic))->currentVM) != NULL) { \ + Rf_endEmbeddedR(0); \ + core->currentVM = NULL; \ } +tic_core *getTICCore(tic_mem* tic, const char* code); + static bool initR(tic_mem *tic, const char *code) { - killer(0); + killer; int tries = 1; - tic_core *core = getTicCore(tic, code); + core = getTICCore(tic, code); tryOnceMoreOnly: /* embdRAV: embedded R argument vector. */ char *embdRAV[]= { "REmbeddedInTIC80", "--silent" }; - core = (tic_core *) Rf_initEmbeddedR(sizeof(embdRAV)/sizeof(embdRAV[0]), embdRAV); - - bool rc = (bool) *core; + bool rc = (bool) Rf_initEmbeddedR(sizeof(embdRAV)/sizeof(embdRAV[0]), embdRAV); if (rc) return rc; else if (tries--) goto tryOnceMoreOnly; @@ -72,23 +65,22 @@ static bool initR(tic_mem *tic, const char *code) { } static void closeR(tic_mem *tic) { - killer(0); + killer; } static void callRfn_TIC80() { - /* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */ - Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\ - "`TIC-80`()")); + /* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */ + Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\ + "`TIC-80`()"), + R_GlobalEnv); } -#define defineCallRFn_(x) \ - static void callRfn_##x(tic_mem *tic) { \ - Rf_eval(Rf_mkString("if (exists(\""#x"\") && is.function("#x")) "#x"()")); \ + +tic_core *getTICCore(tic_mem* tic, const char* code) { + tic_core *core; + while (core == NULL && (!initR(tic, code))) { + core = (((tic_core *) tic))->currentVM; } -/* if (exists("x") && is.function(x)) x() */ -defineCallRFn_("MENU") -defineCallRFn_("BDR") -defineCallRFn_("BOOT") -defineCallRFn_("SCN") -#undef defineCallRFn_ + return core; +} static const char* const RKeywords [] = { @@ -99,6 +91,17 @@ static const char* const RKeywords [] = "...", "..1", "..2", "..3", "..4", "..5", "..6", "..7", "..8", "..9", }; +/* A naive edit of the Python function to check if a character is a valid + * character within an identifier. */ +static bool r_isalnum(char c) { + return ( + (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || (c == '_') || (c == '.') + ); +} + static const tic_outline_item* getROutline(const char* code, s32* size) { enum{Size = sizeof(tic_outline_item)}; diff --git a/src/api/r.org b/src/api/r.org index c654965b1..ac9ab9ee5 100644 --- a/src/api/r.org +++ b/src/api/r.org @@ -22,7 +22,7 @@ can define it right now. For now the =memory= parameter can be ignored. #+name: define evalR #+begin_src C void evalR(tic_mem *memory, char *code) { - Rf_eval(Rf_mkString(code)); + SEXP result = Rf_eval(Rf_mkString(code), R_GlobalEnv); } #+end_src @@ -249,7 +249,7 @@ definition of the function in either soure tree. tic_core *getTICCore(tic_mem* tic, const char* code) { tic_core *core; while (core == NULL && (!initR(tic, code))) { - core = (castTicMemory)->currentVM; + core = (((tic_core *) tic))->currentVM; } return core; } @@ -448,12 +448,12 @@ changed so that no references to "Scheme" occur in the code. <> - <> - <> <> + <> + <> <> @@ -498,24 +498,25 @@ restarting R as necessary and tracking the current interpreter (there can be only one). #+begin_src C :noweb-ref cartridge commands - #define killer(x) \ - if ((tic_core *core = (castTicMemory)->currentVM) != NULL) { \ - Rf_endEmbeddedR(x); \ - core->currentVM = NULL; \ + #define killer \ + tic_core *core; \ + if ((core = (((tic_core *) tic))->currentVM) != NULL) { \ + Rf_endEmbeddedR(0); \ + core->currentVM = NULL; \ } + tic_core *getTICCore(tic_mem* tic, const char* code); + static bool initR(tic_mem *tic, const char *code) { - killer(0); + killer; int tries = 1; - tic_core *core = getTicCore(tic, code); + core = getTICCore(tic, code); tryOnceMoreOnly: /* embdRAV: embedded R argument vector. */ char *embdRAV[]= { "REmbeddedInTIC80", "--silent" }; - core = (tic_core *) Rf_initEmbeddedR(sizeof(embdRAV)/sizeof(embdRAV[0]), embdRAV); - - bool rc = (bool) *core; + bool rc = (bool) Rf_initEmbeddedR(sizeof(embdRAV)/sizeof(embdRAV[0]), embdRAV); if (rc) return rc; else if (tries--) goto tryOnceMoreOnly; @@ -523,7 +524,7 @@ only one). } static void closeR(tic_mem *tic) { - killer(0); + killer; } #+end_src @@ -535,26 +536,26 @@ at least. The =exists= function doesn't use symbols, it uses strings to lookup symbols so that is why that part differs in the chunk below. #+begin_src C :noweb no-export :noweb-ref cartridge commands - static void callRfn_TIC80() { - /* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */ - Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\ - "`TIC-80`()")); - } + static void callRfn_TIC80() { + /* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */ + Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\ + "`TIC-80`()"), + R_GlobalEnv); + } #+end_src *** The menu, border, scanline, and system boot callback functions These four TIC-80 API functions or commands are defined using a macro. - -#+begin_src C :noweb no-export :noweb-ref cartridge commands #define defineCallRFn_(x) \ static void callRfn_##x(tic_mem *tic) { \ - Rf_eval(Rf_mkString("if (exists(\""#x"\") && is.function("#x")) "#x"()")); \ + Rf_eval(Rf_mkString("if (exists(\""#x"\") && is.function("#x")) "#x"()"), \ + R_GlobalEnv); \ } /* if (exists("x") && is.function(x)) x() */ - defineCallRFn_("MENU") - defineCallRFn_("BDR") - defineCallRFn_("BOOT") - defineCallRFn_("SCN") + defineCallRFn_(MENU) + defineCallRFn_(BDR) + defineCallRFn_(BOOT) + defineCallRFn_(SCN) #undef defineCallRFn_ #+end_src @@ -667,6 +668,17 @@ different areas of the script being written. #+name: OUTLINE GENERATION #+begin_src C + /* A naive edit of the Python function to check if a character is a valid + ,* character within an identifier. */ + static bool r_isalnum(char c) { + return ( + (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || (c == '_') || (c == '.') + ); + } + static const tic_outline_item* getROutline(const char* code, s32* size) { enum{Size = sizeof(tic_outline_item)}; @@ -933,8 +945,8 @@ the R langauge embedding into TIC-80's build process using CMake now. *** Tasks to complete related to this heading: - [X] Patch =CMakeLists.txt=, if necessary. -- [ ] Write a CMake file for R, specifically. -- [ ] Test the build succeeds. +- [X] Write a CMake file for R, specifically. +- [X] Test the build succeeds: build fails with the message "R.h not found." - [ ] Test ~eval~ on the TIC-80 command-line. *** Patching =CMakeLists.txt= @@ -962,7 +974,7 @@ chain (is there Fortran?) to ever work there. Baremetal? I won't bother. endif() if(NOT DEFINED ENV{LD_LIBRARY_PATH} AND NOT LD_LIBRARY_PATH) - set(LD_LIBRARY_PATH "${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server") + set(LD_LIBRARY_PATH "/usr/include/R:${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server") else() # Add these entries to the this *PATH variable set(LD_LIBRARY_PATH "${LD_LIBRARY_PATH}:${PREFIX}/lib64/R/lib:${PREFIX}/lib/jvm/jre/lib/server") @@ -985,7 +997,18 @@ chain (is there Fortran?) to ever work there. Baremetal? I won't bother. list(APPEND R_SRC "${PREFIX}/lib/jvm/jre/lib/server" R_SHARE_DIR R_DOC_DIR) endif() - list(APPEND R_SRC ${CMAKE_SOURCE_DIR}/src/api/r.c) + set(R_LIB_DIR "/usr/include/R") + include_directories(SYSTEM "/usr/include/R") + list( + APPEND R_SRC + ${CMAKE_SOURCE_DIR}/src/api/r.c + ${R_LIB_DIR}/R.h + ${R_LIB_DIR}/Rembedded.h + ${R_LIB_DIR}/Rinterface.h + ${R_LIB_DIR}/Rinternals.h + ${R_LIB_DIR}/Rconfig.h + ${R_LIB_DIR}/Rdefines.h + ) add_library(r ${TIC_RUNTIME} ${R_SRC}) set_target_properties(r PROPERTIES PREFIX "")