#### init.goku initialization file for Goku ### ### copyright (c) Lutz Mueller 1998 ### ### DO NOT EDIT THIS FILE, IF NOT COMPETELY FAMILIAR WITH GOKU ### ### this file defines standard Goku words using existing core words ### and is the same for all versions of Goku ### ### 'define' declares new Goku procedures ### 'variable' declares integer variables ### 'array' declares vector variables for holding strings and data ### ### 'showStack' shows the stack without changing it ### 'load' load a Goku source file and evaluates is ### ### ### During dtartup Goku configures various buffers used internally ### the user can tune the sysvar settings, these variables and then ### calling 'init'. This should be the very first thing done in the ### init.goku file! ### The following statments are out-commented and the numbers they ### carry, are the defaults Goku will take. ### ### All the following adddresses take 32bit values, except ### when stated otherwise. ### ### Version info read only # sysvar[0] #four digit version No. Digit 1-3 = version # #digit 4# 0 = Win32 Graphics version # #digit 4# 1 = Win32 Console version # ### any of the following lines may be uncommented and the parameters changed ### ### read write ###################################################### # 4096 sysvar[4] putl #size of user dictionary (about 200 words) # 512 sysvar[8] putl #size of Goku data stack (126 slots of 32bit) # 2048 sysvar[12] putl #max size for compiled code # 256 sysvar[16] putl #nax N locals are used in a function (64) # 1024 sysvar[20] putl #max size for string constants # 256 sysvar[24] putl #max size err msg buffer # 1024 sysvar[28] putl #max token buffer size # init #restart Goku with new parameters ### following addresses are read-only ############################### # sysvar[32] #address of core read only dictionary # sysvar[36] #address of user current read/write dictionary # sysvar[40] #address of user active read/write dictionary # sysvar[44] #ptr to last symbol in dictionary # sysvar[48] #ptr to end of dictionary # sysvar[52] #low mem of Goku stack # sysvar[56] #high mem of Goku stack # sysvar[60] #current Goku stack pointer (starts in high mem) # sysvar[64] #reserved # sysvar[68] #reserved # sysvar[72] #reserved # sysvar[76] #address of err msg buffer # sysvar[80] #address of token buffer in 'token' # sysvar[84] #ptr to main dictionary # sysvar[88] #accumulated memory malloced # sysvar[92] #accumulated memory freed ### read/write for debugging ######################################### # sysvar[96] #debug flag 0=no debug, else debug (16bit) # sysvar[100] #address of debugging procedure ### read for debugging ############################################### # sysvar[104] #current source code line no when executing code # #witch contains debug code ###################################################################### # The four words 'define' 'variable' 'array' and 'import' are # 'compiling' words and use the 'sourcePtr' directive to look forward # in the Goku input stream # # 'define' is used to declare Goku procedures # 'variable' is used to declare variables # 'array' is used to declare data buffers # 'import' is used to import functions from shared libraries # # # all declarations done must end with a semicolon to interrupt # compile. 'updatePtr. updates the source ptr at which 'eval' picks # up evaluation after the compile. # #--------------------------------------------------------------------- # create 'define', word for defining Goku procedures # # usage# define aNewWord .... procedure to compile .... # # e.g.# define double dup + disp; # # defines a word double, which takes a number from the stack and # doubles it # "sourcePtr token drop 2 create swap compile updatePtr swap putl" compile drop "define" 2 create putl #--------------------------------------------------------------------- # create 'variable', word for declaring variables # # usage# variable aVar otherVar ....; # # e.g.# variable x year speed; # # declares the variables# x year speed, which can each hold one # integer value # define variable sourcePtr while 1 do token # -- newSrc tokenAddr type if dup 0 = then #check if token type 0 drop drop drop drop drop "missing semicolon" type exit end if 1 = then if dup getb ';' = then drop updatePtr #update source ptr for eval return end 0 sysvar[48] getb 8 << | #lobyte=type, hibyte=context create drop # -- newSrc else "name expected" type exit end end; #--------------------------------------------------------------------- # create 'array', word for declaring variables, which can hold any # number of bytes # # usage: array varName nn; # # e.g.: array myVector 10000; # # create a variable# myVector which can hold 10,000 bytes of data # # 'array' uses 'malloc' to allocate storage # define array sourcePtr token # -- newSrc tknAddr type if 1 = then 1 create # -- newSrc contAddr swap token # -- contAddr newSrc tknAddr type if 4 = then atoi malloc swap dup # -- contAddr memAddr newSrc newSrc token drop swap drop # -- contAddr memAddr newSrc tknAddr if getb ';' = then updatePtr # update src ptr for eval swap putl return else "missing semicolon" type exit end end end; #--------------------------------------------------------------------- # procedure for importing functions from shared libraries # # # usage: import libName funcName nParams gokuName; # # example: import libc.so printf 2 format; # # define import # usage: import libName funcName N gokuName local libName funcName gokuName memPtr; sourcePtr token drop !libName @libName loadLib dup if 0 = then "Could not load library " type @libName type exit end # -- srcPtr libHandle swap # -- libHandle srcPtr token drop !funcName # -- libHandle srcPtr swap # -- srcPtr libHandle @funcName loadFunc dup if 0 = then "Could not import function " type @funcName type exit end # -- srcPtr funcAddr swap token drop # -- funcAddr srcPtr numTkn 256 malloc !memPtr @memPtr swap copy # -- funcAddr srcPtr memPtr " " concat drop swap # -- srcPtr funcAddr 16 malloc itoa dup # -- srcPtr funcAscii funcAscii @memPtr swap concat " callLib" concat drop free # -- srcPtr token drop 2 create # -- srcPtr contAddr @memPtr compile drop # -- srcPtr contAddr codeAddr swap putl # puts code address into new word @memPtr free updatePtr; #--------------------------------------------------------------------- # three other usefule words # # ds show the stack contents # fileSize returns size of a file # load loads and evaluates a Goku source file #--------------------------------------------------------------------- # ds displays the contents of the stack, useful when debugging # # the stack will not be modified # define ds # ??? -- ??? "\n" type depth while dup do dup 1 + stack disp 1 - " " type end "\n" type drop; #--------------------------------------------------------------------- # constants for mode in 'open' file # # (see /usr/include/fcntlbits.h) define RDONLY 0x0; define WRONLY 0x1101; # create if not exists, truncate to 0 length define RDWR 0x2; #--------------------------------------------------------------------- # # fileSize # fileName -- fileSize # # find out the size of a file. The file must be readable # define fileSize # fileName -- fileSize RDONLY open dup 0 2 seek swap close drop; #--------------------------------------------------------------------- # # load # fileName -- # # loads a Goku source file and evaluates it. This is en example of # how to write code in Goku in a 'Data Flow' manner explicitly # popping all results from the stack into variables with the '!' # prefix and pushing the contents of the variables back on to the # stack using the '@' prefix. This makes much more readable code # than dealing with parameters only on the stack as in traditional # FORTH. There is a small payoff though in speed and code size of # about 10 - 20% . # # NOTE! that @ and ! don't have effect on memory pointers declared # with 'array'. # define load # fileName -- local fileName size memPtr handle; !fileName #this and the following 'fileName @fileName fileSize !size #are really a redundant pop-push if @size 0 = then "file does not exist in 'load'" type exit end @size 1 + malloc !memPtr @fileName RDONLY open !handle if @handle 0 = then "cannot open file in 'load'" type exit end @handle @memPtr @size read !size @handle close !handle 0 @memPtr @size + putb #append trailing 0 to stop 'eval' @memPtr eval @memPtr free; #--------------------------------------------------------------------- #install some useful functions "/usr/lib/goku/std.goku" load ##########################EOF##########################################