Argc, Argv

This commit is contained in:
Brad Nelson
2021-01-03 22:10:09 -08:00
parent c7371911ed
commit 99eb96536f
4 changed files with 18 additions and 3 deletions

View File

@ -38,6 +38,8 @@
: 'heap ( -- a ) 'sys 5 cells + ; : 'heap ( -- a ) 'sys 5 cells + ;
: last ( -- a ) 'sys 6 cells + ; : last ( -- a ) 'sys 6 cells + ;
: 'notfound ( -- a ) 'sys 7 cells + ; : 'notfound ( -- a ) 'sys 7 cells + ;
: 'argc ( -- a ) 'sys 8 cells + ;
: 'argv ( -- a ) 'sys 9 cells + ;
( Dictionary ) ( Dictionary )
: here ( -- a ) 'heap @ ; : here ( -- a ) 'heap @ ;

View File

@ -13,6 +13,8 @@ static struct {
const char *tib; const char *tib;
cell_t ntib, tin, state, base; cell_t ntib, tin, state, base;
cell_t *heap, *last, notfound; cell_t *heap, *last, notfound;
int argc;
char **argv;
cell_t DOLIT_XT, DOEXIT_XT; cell_t DOLIT_XT, DOEXIT_XT;
} g_sys; } g_sys;
@ -114,7 +116,8 @@ static cell_t *evaluate1(cell_t *sp, cell_t *call) {
return sp; return sp;
} }
static void ueforth(void *heap, const char *src, cell_t src_len) { static void ueforth(int argc, char *argv[], void *heap,
const char *src, cell_t src_len) {
g_sys.heap = (cell_t *) heap; g_sys.heap = (cell_t *) heap;
register cell_t *sp = g_sys.heap; g_sys.heap += STACK_SIZE; register cell_t *sp = g_sys.heap; g_sys.heap += STACK_SIZE;
register cell_t *rp = g_sys.heap; g_sys.heap += STACK_SIZE; register cell_t *rp = g_sys.heap; g_sys.heap += STACK_SIZE;
@ -134,6 +137,8 @@ static void ueforth(void *heap, const char *src, cell_t src_len) {
*g_sys.heap++ = FIND("EVALUATE1"); *g_sys.heap++ = FIND("EVALUATE1");
*g_sys.heap++ = FIND("BRANCH"); *g_sys.heap++ = FIND("BRANCH");
*g_sys.heap++ = (cell_t) ip; *g_sys.heap++ = (cell_t) ip;
g_sys.argc = argc;
g_sys.argv = argv;
g_sys.base = 10; g_sys.base = 10;
g_sys.tib = src; g_sys.tib = src;
g_sys.ntib = src_len; g_sys.ntib = src_len;

View File

@ -27,6 +27,7 @@ z" mmap" 6 sysfunc mmap
z" munmap" 2 sysfunc munmap z" munmap" 2 sysfunc munmap
z" unlink" 1 sysfunc unlink z" unlink" 1 sysfunc unlink
z" rename" 2 sysfunc rename z" rename" 2 sysfunc rename
z" strlen" 1 sysfunc strlen
( Errno ) ( Errno )
z" __errno_location" 0 sysfunc __errno_location z" __errno_location" 0 sysfunc __errno_location
@ -94,12 +95,19 @@ z" realloc" 2 sysfunc realloc
: free ( a -- ior ) sysfree 0 ; : free ( a -- ior ) sysfree 0 ;
: resize ( a n -- a ior ) realloc 0ior ; : resize ( a n -- a ior ) realloc 0ior ;
( String Handling )
: s>z ( a n -- z ) here >r $place r> ;
: z>s ( z -- a n ) dup strlen ;
( Arguments )
: argc ( -- n ) 'argc @ ;
: argv ( n -- a n ) cells 'argv @ + @ z>s ;
( Generic Files ) ( Generic Files )
O_RDONLY constant r/o O_RDONLY constant r/o
O_WRONLY constant w/o O_WRONLY constant w/o
O_RDWR constant r/w O_RDWR constant r/w
octal 777 constant 0777 decimal octal 777 constant 0777 decimal
: s>z ( a n -- z ) here >r $place r> ;
: open-file ( a n fam -- fh ior ) >r s>z r> 0777 open 0<ior ; : open-file ( a n fam -- fh ior ) >r s>z r> 0777 open 0<ior ;
: create-file ( a n fam -- fh ior ) >r s>z r> O_CREAT or 0777 open 0<ior ; : create-file ( a n fam -- fh ior ) >r s>z r> O_CREAT or 0777 open 0<ior ;
: close-file ( fh -- ior ) close 0<ior ; : close-file ( fh -- ior ) close 0<ior ;

View File

@ -26,5 +26,5 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
void *heap = mmap(0, HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); void *heap = mmap(0, HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ueforth(heap, boot, sizeof(boot)); ueforth(argc, argv, heap, boot, sizeof(boot));
} }