|
From: Paul P. <ppr...@us...> - 2004-07-24 07:21:28
|
Update of /cvsroot/forthy/forthy In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12676 Modified Files: forthy.dsp fparse.c fstack.h fsystem.c fsystem.h ftype.h fvalue.c main.c Log Message: - fixed swap fs_store() parameters - fixed memory leak when trying to fs_store() on a bad reference (non-reference) - changed input parsing behaviour, fs_load_input() replaces fs_load_string(). This now makes an internal copy of the source text, and puts it on an input stack. Should allow "included" files (untested as of yet) Index: fvalue.c =================================================================== RCS file: /cvsroot/forthy/forthy/fvalue.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** fvalue.c 17 Nov 2003 06:18:00 -0000 1.5 --- fvalue.c 24 Jul 2004 07:21:17 -0000 1.6 *************** *** 631,635 **** --- 631,638 ---- // Clean the destination value if(!(dest=value_deref(destref))) + { + value_clean(&temp, FALSE); VOID_THROW(sys, FS_BAD_REF); + } value_clean(dest, FALSE); Index: fparse.c =================================================================== RCS file: /cvsroot/forthy/forthy/fparse.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** fparse.c 7 Aug 2003 01:22:55 -0000 1.1 --- fparse.c 24 Jul 2004 07:21:17 -0000 1.2 *************** *** 31,34 **** --- 31,42 ---- + /* + ** parse_general: + ** - str, input to parse + ** - pre, post, delimiters + ** - start, where parsed token starts + ** - end, where parsed token ends + ** - next, where input will be parsed next time (past end of last token) + */ int parse_general(char *str, char *pre, char *post, char **start, char **end, char **next) { Index: ftype.h =================================================================== RCS file: /cvsroot/forthy/forthy/ftype.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ftype.h 17 Nov 2003 06:18:00 -0000 1.3 --- ftype.h 24 Jul 2004 07:21:17 -0000 1.4 *************** *** 108,116 **** double f; char *str; ! // TODO: Userdata (void*)? Copy/delete functions, etc.? void *v; FCFUNC func; - // FSYMREC *word; - // FDICT *table; FSTACK *stack; FXTOKEN tok; --- 108,114 ---- double f; char *str; ! // TODO: Userdata (void*)? Copy/clone/convert/delete interface, etc. void *v; FCFUNC func; FSTACK *stack; FXTOKEN tok; Index: fstack.h =================================================================== RCS file: /cvsroot/forthy/forthy/fstack.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** fstack.h 13 Nov 2003 07:18:04 -0000 1.2 --- fstack.h 24 Jul 2004 07:21:17 -0000 1.3 *************** *** 18,21 **** --- 18,22 ---- void stack_remove(FSYSTEM *sys, FSTACK *stack, int count); + char *stack_pop_detach_string(FSYSTEM *sys, FSTACK *stack); void stack_pop_to(FSYSTEM *sys, FSTACK *dest, FSTACK *src); void stack_swap(FSYSTEM *sys, FSTACK *stack, int off1, int off2); Index: fsystem.c =================================================================== RCS file: /cvsroot/forthy/forthy/fsystem.c,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** fsystem.c 23 Jul 2004 06:11:08 -0000 1.8 --- fsystem.c 24 Jul 2004 07:21:17 -0000 1.9 *************** *** 114,117 **** --- 114,119 ---- #define WHITESPACES " \r\n\t" + #define DEFAULT_STACK_SIZE 8 + // ** Info strings ** static char *_type_name[]= *************** *** 170,173 **** --- 172,176 ---- //static char _other_error_name[33]; + // TODO: Make the data/return stacks resizable int fs_sys_init(FSYSTEM *sys, int stack_size, int return_stack_size) { *************** *** 178,185 **** if(!sys->table) return FS_NULL; // TODO: Error handling ! if(!((sys->stack)=stack_new(stack_size))) return FS_OUT_OF_MEMORY; if(!((sys->rstack)=stack_new(return_stack_size))) return FS_OUT_OF_MEMORY; --- 181,194 ---- if(!sys->table) return FS_NULL; // TODO: Error handling ! ! if(!stack_size) ! stack_size=DEFAULT_STACK_SIZE; ! if(!((sys->stack)=stack_new(stack_size))) return FS_OUT_OF_MEMORY; + if(!return_stack_size) + stack_size=DEFAULT_STACK_SIZE; + if(!((sys->rstack)=stack_new(return_stack_size))) return FS_OUT_OF_MEMORY; *************** *** 189,192 **** --- 198,204 ---- sys->result=FS_OK; + if(!((sys->istack)=stack_new(DEFAULT_STACK_SIZE))) + return FS_OUT_OF_MEMORY; + sys->in_source=NULL; sys->in_load=NULL; *************** *** 275,278 **** --- 287,293 ---- char *start, *end; + if(!sys->in_token) + return "<no token>"; + if(parse_general(sys->in_token, WHITESPACES, WHITESPACES, &start, &end, NULL)) *************** *** 1315,1318 **** --- 1330,1334 ---- // Continue while there are words in the buffer + // TODO: Configurable delimiters? if(parse_general(sys->in_source, WHITESPACES, WHITESPACES, &start, &(sys->in_parse), &(sys->in_source))) *************** *** 1397,1402 **** } ! sys->in_source=NULL; ! sys->in_parse=NULL; VOID_THROW(sys, FS_READY); } --- 1413,1449 ---- } ! ! ! // pop top string value from the stack (bye bye!) ! stack_pop(sys, sys->istack); ! ! if(stack_depth(sys->istack)) ! { ! int offset; ! FVALUE *value; ! ! // get and pop the offset from stack, no longer need to store ! value=stack_get_value(sys, sys->stack, -1); ! offset=value_get_int(value); ! stack_pop(sys, sys->istack); ! ! // get input source start as void ! value=stack_get_value(sys, sys->stack, -1); ! ! // want direct access to avoid copying big strings ! sys->in_load=(char*)value_get_void(value); ! ! sys->in_source=sys->in_load+offset; ! sys->in_parse=NULL; ! sys->in_token=NULL; ! } ! else ! { ! sys->in_load=NULL; ! sys->in_source=NULL; ! sys->in_parse=NULL; ! sys->in_token=NULL; ! } ! VOID_THROW(sys, FS_READY); } *************** *** 1416,1419 **** --- 1463,1467 ---- if(!ret) { + // TODO: Use istack depth? if((sys->in_source)&&(!sys->running)) { *************** *** 1528,1546 **** // Attach a string to the string buffer ! int fs_load_string(FSYSTEM *sys, char *str) { sys->in_source=str; sys->in_load=str; ! sys->in_parse=str; sys->in_token=NULL; - // Clear the return stack? Behave like QUIT? - // vm_abort(sys); - return FS_OK; } /* One step of the VM (not a debugging step/trace) TODO: --- 1576,1638 ---- + int fs_clear_input(FSYSTEM *sys) + { + stack_remove(sys, sys->istack, stack_depth(sys->istack)); + sys->in_load=NULL; + sys->in_source=NULL; + sys->in_parse=NULL; + sys->in_token=NULL; + + return FS_OK; + } + + // Attach a string to the string buffer ! int fs_load_input(FSYSTEM *sys, char *str) { + FVALUE *value; + + // If we're already interpreting input + if(sys->in_source) + { + value=stack_push(sys, sys->istack); + if(!value) + return FS_OVERFLOW; + + value_init_int(value, sys->in_source-sys->in_load); + } + + // Put input string on the istack + value=stack_push(sys, sys->istack); + if(!value) + return FS_OVERFLOW; + + value_init_string(value, str, strlen(str)); + + // Set input sources + sys->in_source=str; sys->in_load=str; ! sys->in_parse=NULL; sys->in_token=NULL; return FS_OK; } + int fs_input_new(FSYSTEM *sys, int size) + { + if(!size) + return FS_OK; + + sys->istack=stack_new((size*2)-1); + + if(sys->istack) + return FS_OK; + + INT_THROW(sys, FS_OUT_OF_MEMORY); + } + + /* One step of the VM (not a debugging step/trace) TODO: *************** *** 1727,1731 **** return FS_OK; ! return FS_OUT_OF_MEMORY; } --- 1819,1823 ---- return FS_OK; ! INT_THROW(sys, FS_OUT_OF_MEMORY); } *************** *** 1887,1890 **** --- 1979,1984 ---- if(sys->vstack) stack_delete(sys->vstack); + if(sys->istack) + stack_delete(sys->istack); if(sys->temp_str) *************** *** 1894,1897 **** --- 1988,1993 ---- free(sys->result_str); + memset(sys, 0, sizeof(FSYSTEM)); + return FS_OK; } Index: forthy.dsp =================================================================== RCS file: /cvsroot/forthy/forthy/forthy.dsp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** forthy.dsp 23 Jul 2004 15:48:08 -0000 1.4 --- forthy.dsp 24 Jul 2004 07:21:17 -0000 1.5 *************** *** 106,109 **** --- 106,113 ---- # Begin Source File + SOURCE=..\Fortify\FORTIFY.C + # End Source File + # Begin Source File + SOURCE=.\fparse.c # End Source File Index: main.c =================================================================== RCS file: /cvsroot/forthy/forthy/main.c,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** main.c 23 Jul 2004 15:48:08 -0000 1.13 --- main.c 24 Jul 2004 07:21:17 -0000 1.14 *************** *** 473,477 **** void word_store(FSYSTEM *sys) { ! fs_store(sys, -2, -1); fs_remove(sys, 2); } --- 473,477 ---- void word_store(FSYSTEM *sys) { ! fs_store(sys, -1, -2); fs_remove(sys, 2); } *************** *** 1703,1707 **** int i; ! fs_load_string(sys, ": test 1 2 3 ; : test2 test swap drop dup + - . ; "); fs_run(sys); --- 1703,1707 ---- int i; ! fs_load_input(sys, ": test 1 2 3 ; : test2 test swap drop dup + - . ; "); fs_run(sys); *************** *** 1742,1746 **** fs_pop(sys); ! fs_store(sys, -2, -1); show_stack(sys); fs_pop(sys); --- 1742,1746 ---- fs_pop(sys); ! fs_store(sys, -1, -2); show_stack(sys); fs_pop(sys); *************** *** 1803,1807 **** return FS_ERROR; ! fs_load_string(sys, script); ret=fs_run(sys); --- 1803,1807 ---- return FS_ERROR; ! fs_load_input(sys, script); ret=fs_run(sys); *************** *** 1894,1897 **** --- 1894,1899 ---- printf("ERROR at '%s': ", fs_get_last_token(&sys)); printf("%s\n", fs_get_result_string(&sys, ret)); + + fs_clear_input(&sys); } else *************** *** 1935,1940 **** if(pos) { ! fs_load_string(&sys, buffer); ! ret=fs_run(&sys); } --- 1937,1941 ---- if(pos) { ! fs_load_input(&sys, buffer); ret=fs_run(&sys); } Index: fsystem.h =================================================================== RCS file: /cvsroot/forthy/forthy/fsystem.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** fsystem.h 23 Jul 2004 06:11:08 -0000 1.7 --- fsystem.h 24 Jul 2004 07:21:17 -0000 1.8 *************** *** 33,36 **** --- 33,37 ---- FSTACK *rstack; // return stack FSTACK *vstack; // vocabulary stack + FSTACK *istack; // input stack FVALUE vcompile; // "current" compilation vocabulary *************** *** 81,88 **** int fs_dict_attach(FSYSTEM *sys, FSYSTEM *source); ! int fs_load_string(FSYSTEM *sys, char *str); int fs_load_word(FSYSTEM *sys, int handle); int fs_run(FSYSTEM *sys); int fs_step(FSYSTEM *sys); //int fs_trace(FSYSTEM *sys); --- 82,90 ---- int fs_dict_attach(FSYSTEM *sys, FSYSTEM *source); ! int fs_load_input(FSYSTEM *sys, char *str); int fs_load_word(FSYSTEM *sys, int handle); int fs_run(FSYSTEM *sys); int fs_step(FSYSTEM *sys); + int fs_clear_input(FSYSTEM *sys); //int fs_trace(FSYSTEM *sys); |