# stdc.goku # # some useful Goku procedures console version # # copyright (c) Lutz Mueller 1998 # ####################################################################### # debug # displays the last executed source code line no in the top right # of the console. NOTE! that there is an overhead of about 20 bytes # of code per Goku word, when compiling in debug mode. If compilation # or loading a file crashes while in debug code, than probably # the code pad size in sysvar[12] is too small! # define debug local dummy; sysvar[104] getl 1 + disp " " type dummy 1 accept drop; ####################################################################### # # trace # ( onOff -- ) # setting the debug flag in sysvar[96] enables debug mode # during compile this causes Goku to emit debug code # in between the normal code. The debug code calles a routine # the address of which is found at the location sysvar[100]. The # debug code also updates a source line counter in sysvar[104]. define trace # (onOff -- ) if then @debug sysvar[100] putl 1 sysvar[96] putw else 0 sysvar[96] putw end; #--------------------------------------------------------------------- # calculate fibonacci numbers # this is the worse (time and stack consuming) way to caclulate # fibonacci numbers but makes for a good bench-mark function and # illustrates the usage of recursion in Goku. Try# # # 35 fibo disp # # which takes about 3.5 sec on a 200Mhz Pentium MMX machine # running NT 4.0 # define fibo # number -- result dup if 3 < then drop 1 else dup 1 - recurse swap 2 - recurse + end; ####################################################################### # # timeIt # calculates the execution time in ms for a procedure # # e.g. instead of executing# '35 fibo' execute# '35 @fibo timeIt' # define timeIt #(p1 .. pn procAddr -- ??) local start; #local variable for start time time !start #remember start time call #execute address on top of stack time @start - disp; #calculate elapsed time and display ####################################################################### # list all Goku words # # This function walkes first the core dictionary and then the user # dictionary to list all definitions. Note!, that this functions # walks forward, while the internal dictionary 'lookup' always starts # from the back. This way word definition can be overwritten (except # for core words, which are looke up always first). # # define next # address -- nextAddress dup 2 + getb + 8 + ; define words # -- sysvar[32] getl 8 + #Core dictionary past null entry while 1 do if dup getb -2 = then drop sysvar[36] getl 16 + #User dictionary end if dup getb -1 = then drop return end dup 8 + type " " type next #jump to next word end; ######################### Memory Usage ################################ # # displays the net memory allocated since startup # define memory sysvar[88] getl sysvar[92] getl - disp; ####################################################################### # # parse ( bufferPtr token separator -- newBufferPtr ) # # breaks up a string in to tokens at separator characters # removes leading spaces and CR LF's # define parse local buff tkn separator chr; #initialize token string !separator !tkn !buff #get parameters while @buff getc dup !chr do #skip white space if @chr ' ' > then break end buff inc end while @buff getc dup !chr do if @chr @separator = then buff inc break end @chr @tkn putb #copy chars tkn inc buff inc #increment pointers end 0 @tkn putb #terminating zero @buff; #######################EOF#############################################