From 09a0d4f8d7da893d01fd5eda4becd90917f918f8 Mon Sep 17 00:00:00 2001 From: ketmar Date: Wed, 25 Oct 2023 15:13:05 +0000 Subject: [PATCH] fixed starter FossilOrigin-Name: fdc193ddd186c037ba413c425921ab187f25069ffd156201761b9207d23aea13 --- src/urasm-new.c | 156 +++++++++++++++-------------------------- urflibs/urasm/00-main-loader.f | 3 + urflibs/urasm/asm-test.f | 123 ++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 98 deletions(-) create mode 100644 urflibs/urasm/asm-test.f diff --git a/src/urasm-new.c b/src/urasm-new.c index 658f68f..5684d26 100644 --- a/src/urasm-new.c +++ b/src/urasm-new.c @@ -99,31 +99,8 @@ static void initUFEInclideDir (void) { } -/////////////////////////////////////////////////////////////////////////////// -// options -// -static struct option longOpts[] = { - {"help", 0, NULL, 'h'}, - {NULL, 0, NULL, 0} -}; - - -//========================================================================== -// -// usage -// -//========================================================================== -static void usage (const char *pname) { - printf( - "usage: %s [options] infile\n" - "\n" - "options:\n" - " -h --help this help\n" - , pname); -} - - static jmp_buf errJP; +static int testparser = 0; static int g_argc; static char **g_argv; @@ -184,76 +161,34 @@ static __attribute__((format(printf,1,2))) char *strprintf (const char *fmt, ... //========================================================================== // -// strHasPathDelim -// -//========================================================================== -static int strHasPathDelim (const char *s) { - if (!s || !s[0]) return 0; - #ifdef WIN32 - return (strchr(s, '/') || strchr(s, '\\') ? 1 : 0); - #else - return (strchr(s, '/') ? 1 : 0); - #endif -} - - -//========================================================================== -// // createIncludeName // // returns malloced string // //========================================================================== -static char *createIncludeName (const char *fname, int assystem, const char *defaultmain, - const char *lastIncPathUFO) -{ - if (!fname || !fname[0]) return NULL; +static char *createIncludeName (const char *fname, int assystem, const char *lastIncPathUFO) { + struct stat st; char *res; + + if (!fname || !fname[0]) return NULL; + if (fname[0] != '/') { - const char *incdir; - if (!assystem) { - incdir = NULL; - } else if (assystem == -669) { - incdir = lastIncPathUFO; - } else if (assystem == -666) { - incdir = lastIncPathUFO; - if (!incdir || !incdir[0]) incdir = ufoIncludeDir; - } else { - ur_assert(0); - } + const char *incdir = lastIncPathUFO; + if (assystem && (!incdir || !incdir[0])) incdir = ufoIncludeDir; if (incdir == NULL || incdir[0] == 0) incdir = "."; - res = strprintf("%s/%s", (incdir && incdir[0] ? incdir : "."), fname); + res = strprintf("%s/%s", incdir, fname); } else { res = strprintf("%s", fname); } - struct stat st; - if (defaultmain && defaultmain[0]) { - if (stat(res, &st) == 0) { - if (S_ISDIR(st.st_mode)) { - char *rs = strprintf("%s/%s", res, defaultmain); - free(res); - res = rs; - } - } - } - /* check if there is the disk file */ - if (strHasPathDelim(fname) && stat(res, &st) != 0) { - /* no file, try "root include" */ - const char *incdir = (!assystem ? NULL : ufoIncludeDir); - char *rs = strprintf("%s/%s", (incdir && incdir[0] ? incdir : "."), fname); - free(res); - res = rs; - /* check for dir again */ - if (defaultmain && defaultmain[0]) { - if (stat(res, &st) == 0) { - if (S_ISDIR(st.st_mode)) { - char *rs = strprintf("%s/%s", res, defaultmain); - free(res); - res = rs; - } - } + + if (stat(res, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + char *rs = strprintf("%s/%s", res, (assystem ? "00-main-loader.f" : "zzmain.f")); + free(res); + res = rs; } } + //fprintf(stderr, "inc: fname=<%s>; sys=%d; def=<%s>; res=<%s>\n", fname, assystem, defaultmain, res); return res; } @@ -269,18 +204,8 @@ char *ufoCreateIncludeName (const char *fname, int assystem, const char *lastInc fprintf(stderr, "ICIN: fname=<%s>; lastinc=<%s>; assystem=%d\n", fname, lastIncPath, assystem); #endif - assystem = (assystem ? -666 : -669); - if (lastIncPath == NULL) lastIncPath = ufoIncludeDir; - char *res = createIncludeName(fname, assystem, NULL, lastIncPath); - struct stat st; - char *tmp; - if (res == NULL || res[0] == 0 || stat(res, &st) != 0) return res; - if (S_ISDIR(st.st_mode)) { - tmp = strprintf("%s/%s", res, "00-main-loader.f"); - free(res); - res = tmp; - } - return res; + if (lastIncPath == NULL && assystem) lastIncPath = ufoIncludeDir; + return createIncludeName(fname, assystem, lastIncPath); } @@ -324,7 +249,7 @@ static void ufcWord_BYE (uint32_t pfa) { //========================================================================== static void ufcWord_ARGC (uint32_t pfa) { int rest = g_argc - optind; - if (rest <= 1) ufoPushData(0); else ufoPushData((uint32_t)(rest - 1)); + if (rest <= 0) ufoPushData(0); else ufoPushData((uint32_t)rest); } @@ -339,11 +264,11 @@ static void ufcWord_ARGC (uint32_t pfa) { static void ufcWord_ARGV (uint32_t pfa) { uint32_t idx = ufoPopData(); int rest = g_argc - optind; - if (rest <= 1 || idx >= rest - 1) { + if (rest <= 0 || idx >= rest) { ufoPushData(ufoGetPAD()); ufoPushData(0); } else { - const unsigned char *arg = (const unsigned char *)(g_argv[optind + 1 + (int)idx]); + const unsigned char *arg = (const unsigned char *)(g_argv[optind + (int)idx]); const uint32_t addr = ufoGetPAD(); uint32_t count = 0; while (*arg) { @@ -363,6 +288,7 @@ static void ufcWord_ARGV (uint32_t pfa) { //========================================================================== static void initUrForth (void) { ufoCondDefine("URASM-NEW"); + if (testparser) ufoCondDefine("TEST-PARSER"); ufoRegisterWord("N-BYE", &ufcWord_N_BYE, UFW_FLAG_PROTECTED); ufoRegisterWord("BYE", &ufcWord_BYE, UFW_FLAG_PROTECTED); ufoRegisterWord("ARGC", &ufcWord_ARGC, UFW_FLAG_PROTECTED); @@ -381,6 +307,32 @@ static void urcShutdown (void) { } +/////////////////////////////////////////////////////////////////////////////// +// options +// +static struct option longOpts[] = { + {"help", 0, NULL, 'h'}, + {"repl", 0, NULL, 'R'}, + {"test", 0, NULL, 'T'}, + {NULL, 0, NULL, 0} +}; + + +//========================================================================== +// +// usage +// +//========================================================================== +static void usage (const char *pname) { + printf( + "usage: %s [options] infile\n" + "\n" + "options:\n" + " -h --help this help\n" + , pname); +} + + //========================================================================== // // main @@ -389,6 +341,7 @@ static void urcShutdown (void) { int main (int argc, char *argv[]) { int res = 0, c; const char *pname = argv[0]; + int dorepl = 0; ufoSetUserPostInit(&initUrForth); @@ -400,6 +353,8 @@ int main (int argc, char *argv[]) { switch (c) { case '?': return 1; case 'h': usage(pname); res = 0; if (ufoIncludeDir) free(ufoIncludeDir); exit(0); + case 'R': dorepl = 1; break; + case 'T': testparser = 1; break; } } @@ -419,7 +374,7 @@ int main (int argc, char *argv[]) { exit(1); } - if (optind >= argc) { + if (dorepl) { for (;;) { if (setjmp(errJP) == 0) { uint32_t cfa; @@ -433,8 +388,13 @@ int main (int argc, char *argv[]) { } } } else { + uint32_t cfa = ufoFindWordInVocabulary("RUN-URASM", ufoGetForthVocId()); + if (cfa == 0) { + fprintf(stderr, "FATAL: cannot find main URASM loop word.\n"); + exit(1); + } if (setjmp(errJP) == 0) { - ufoRunFile(argv[optind]); + ufoRunWord(cfa); } else { res = 1; } diff --git a/urflibs/urasm/00-main-loader.f b/urflibs/urasm/00-main-loader.f index f974816..5a0ea61 100644 --- a/urflibs/urasm/00-main-loader.f +++ b/urflibs/urasm/00-main-loader.f @@ -35,3 +35,6 @@ prev-defs asm-emit:init-memory + + +$include-once diff --git a/urflibs/urasm/asm-test.f b/urflibs/urasm/asm-test.f new file mode 100644 index 0000000..eb1dfa3 --- /dev/null +++ b/urflibs/urasm/asm-test.f @@ -0,0 +1,123 @@ +;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; and now for something completely different... +;; UrAsm Forth Engine! +;; GPLv3 ONLY +;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Z80 Assembler: main module +;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +$INCLUDE-ONCE <../zxdisasm> + +asm-emit:init-memory + +:noname ( addr -- byte ) + 0xffff and asm-emit:zx-mem + c@ +; to zxdis:(zx-c@) + + +: disasm ( -- ) + 0 asm-emit:find-used " no code" ?not-error + to zxdis:pc + begin + zxdis:pc asm-emit:.hex4 ." : " + zxdis:disasm-one ( addr count ) + xtype cr + zxdis:pc asm-emit:used? + not-until +; + + +also asm-lexer + +: test ( -- ) + \ " AYMute.zas" false false (include) + \ " cls_ei.zas" false false (include) + argc " assemble what?" ?not-error + 0 argv false false (include) + true to line-start + begin + begin + next-token tok-type tk-eos = + while + ." *** NEWLINE ***\n" + refill-nocross ifnot ." EOF!\n" exit endif + true to line-start + ." <" + (tib-in) 0 begin dup tib-peekch-ofs while 1+ repeat xtype ." >" cr + repeat + ." TOKEN: type=" + tok-type case + tk-eos of ." \n" endof + tk-id of ." ; str=<" token xtype ." >\n" endof + tk-num of ." ; num=" tok-num . ." str=<" token xtype ." >\n" endof + tk-str of ." ; str=<" token xtype ." >\n" endof + tk-punct of ." ; num=" tok-num . ." str=<" token xtype ." >\n" endof + tk-label of ."