From 1607012cdd1095997fe7c6d6d11a6d04e259c5c4 Mon Sep 17 00:00:00 2001 From: Jeremy Green Date: Tue, 2 Sep 2025 20:09:51 +0100 Subject: [PATCH] Enable uBasic to run from the command line Replace use-basic with run-basic --- .gitattributes | 2 ++ .gitignore | 52 ++++++++++++++++++++++++++++++++ makefile.bat | 1 + use-ubasic.c => run-ubasic.c | 57 ++++++++++++++++++++++++++---------- test.bas | 9 ++++++ tokenizer.c | 11 +++++-- 6 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 makefile.bat rename use-ubasic.c => run-ubasic.c (64%) create mode 100644 test.bas diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/makefile.bat b/makefile.bat new file mode 100644 index 0000000..bc78ac4 --- /dev/null +++ b/makefile.bat @@ -0,0 +1 @@ +cl run-ubasic.c ubasic.c tokenizer.c \ No newline at end of file diff --git a/use-ubasic.c b/run-ubasic.c similarity index 64% rename from use-ubasic.c rename to run-ubasic.c index 6b79ee1..e19d3e9 100644 --- a/use-ubasic.c +++ b/run-ubasic.c @@ -27,29 +27,54 @@ * SUCH DAMAGE. * */ + +/* + * Modified to support simple string variables and functions by David Mitchell + * November 2008. + * Changes and additions are marked 'string additions' throughout + */ #include "ubasic.h" - -static const char program[] = -"10 gosub 100\n\ -20 for i = 1 to 10\n\ -30 print i\n\ -40 next i\n\ -50 print \"end\"\n\ -60 end\n\ -100 print \"subroutine\"\n\ -110 return\n"; +#include +#include /*---------------------------------------------------------------------------*/ -int -main(void) -{ - ubasic_init(program); +// main routine modified to allow execution of BASIC script files +int +main(int argc, char **argv, char **envp) +{ + char *q; + char **p; + char *prog; + char buffer[15000]; + int infile; + + if (argc > 1) { + p = argv + 1; + q = *p; + while(*q == ' ') ++q; + + if ((infile = open(q,0)) == -1) { + printf("File \"%s\" not found in current directory - terminating\n",q); + return (-1); + } + + if (read(infile,buffer,15000) == -1) { + printf("Error reading file \"%s\" - terminating\n",q); + printf("Error was \"%d\" \n",errno); + return (-1); + } + prog = buffer; + } else { + printf("Usage: ubasic fname\n where fname is a file containing basic statements\n"); + return (0); + } + + ubasic_init(prog); do { ubasic_run(); } while(!ubasic_finished()); return 0; -} -/*---------------------------------------------------------------------------*/ +} \ No newline at end of file diff --git a/test.bas b/test.bas new file mode 100644 index 0000000..bf46204 --- /dev/null +++ b/test.bas @@ -0,0 +1,9 @@ +1 print "start of test" +10 gosub 100 +20 for i = 1 to 10 +30 print i +40 next i +50 print "end" +60 end +100 print "subroutine" +110 return diff --git a/tokenizer.c b/tokenizer.c index 3321079..cb971a8 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -43,6 +43,8 @@ static char const *ptr, *nextptr; +static char const *prog; + #define MAX_NUMLEN 6 struct keyword_token { @@ -119,6 +121,9 @@ get_next_token(void) int i; DEBUG_PRINTF("get_next_token(): '%s'\n", ptr); + + // eat all whitespace + while(*ptr == ' ' || *ptr == '\t' || *ptr == '\r') ptr++; if(*ptr == 0) { return TOKENIZER_ENDOFINPUT; @@ -170,8 +175,7 @@ get_next_token(void) return TOKENIZER_ERROR; } /*---------------------------------------------------------------------------*/ -void -tokenizer_goto(const char *program) +void tokenizer_goto(const char *program) { ptr = program; current_token = get_next_token(); @@ -180,7 +184,8 @@ tokenizer_goto(const char *program) void tokenizer_init(const char *program) { - tokenizer_goto(program); + ptr = program; + prog = program; current_token = get_next_token(); } /*---------------------------------------------------------------------------*/