home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Mops 2.7 / Mops source / Module source / tool.txt < prev    next >
Encoding:
Text File  |  1995-10-13  |  8.1 KB  |  389 lines  |  [TEXT/MSET]

  1.  
  2. \ Construct table of names & traps for toolbox calls
  3.  
  4. false    value        DOING_GLOBALS?
  5.     9    constant    TAB
  6.  
  7. :class    CSARRAY  super{ object }  16 indexed
  8.  
  9. :m AT:        \ ( index -- addr len )
  10.     ^elem  count  ;m
  11.  
  12. :m TO:        \ ( addr len index -- )
  13.     ^elem  place  ;m
  14.  
  15. ;class
  16.  
  17.  
  18. :class    WARRAY  super{ object }  2 indexed
  19.  
  20. :m  AT:        \ ( index -- n )
  21.     inline{ ix w@}
  22.     ^elem  w@  ;m
  23.  
  24. :m  TO:        \ ( n index -- )
  25.     inline{ ix w!}
  26.     ^elem  w!  ;m
  27.  
  28. :m  +TO:    \ ( n index -- )
  29.     inline{ ix w+!}
  30.     ^elem  w+!  ;m
  31.  
  32. :m -TO:        \ ( n index -- )
  33.     inline{ ix w-!}
  34.     ^elem  w-!  ;m
  35.  
  36. :m ^ELEM:    \ ( index -- addr )
  37.     inline{ ix}
  38.     ^elem4  ;m
  39.  
  40. :m FILL:    \ ( value -- )  Fills all elements with value.
  41.     idxbase  limit 2*  bounds
  42.     ?DO  dup  i w!  2 +LOOP  drop  ;m
  43.  
  44. ;class
  45.  
  46.  
  47. :class    COUNTED_STRINGS  super{ object }  1 indexed
  48.  
  49. record { int SIZE }
  50.  
  51. :m GETSIZE:    get: size  ;m
  52.  
  53. :m ADD:  { addr len -- }
  54.     addr len  get: size  ^elem1  place
  55.     get: size  len +  1+  put: size  ;m
  56.  
  57. :m AT:        \ ( idx -- addr len )
  58.     ^elem1  count  ;m
  59.  
  60. ;class
  61.  
  62.  
  63. :class    HASHTABLE  super{ array }
  64.  
  65. record { int MASK }
  66.  
  67. private
  68.  
  69. :m LOOKUP:  { val \ ixb strt end addr -- index b }
  70.     idxbase -> ixb
  71.     val  get: mask  and  ixb +  dup -> strt -> addr
  72.     ixb  get: mask +  1+  -> end
  73.     BEGIN
  74.         addr @  NIF  addr idxbase -  4/  false  EXIT  THEN
  75.         addr @  val =
  76.         IF  ( found )
  77.             addr ixb -  4/  true  EXIT
  78.         THEN
  79.         4 ++> addr  addr end >=
  80.         IF    ixb -> addr
  81.         ELSE    addr strt =  IF  50 die  THEN
  82.         THEN
  83.     AGAIN  ;m
  84.  
  85. public
  86.  
  87. :m INDEXOF:    \ ( val -- index T  |  -- F )
  88.     lookup: self  IF  true  EXIT  THEN
  89.     drop  false  ;m
  90.  
  91. :m ENTER:  { val \ idx found? -- idx b }
  92.     val  lookup: self  -> found?  -> idx
  93.     found? iF  idx  false  EXIT  THEN
  94.     val idx to: super  idx true  ;m
  95.     
  96.  
  97. :m CLASSINIT:
  98.     limit  1-  2 <<  put: mask  ;m
  99.  
  100. ;class
  101.  
  102.  
  103. :class  STRINGARRAY  super{ string array }
  104.  
  105. record { int CURRENT }
  106.  
  107. :m CURRENT:
  108.     get: current  ;m
  109.  
  110. :m (SEL):  { idx -- }
  111.     idx  put: current
  112.     idx at: self  ^base !
  113.     nil?: self  ?EXIT
  114.     ^base  size: handle  put: size  ;m
  115.  
  116. :m SELECT:  { idx -- }
  117.     idx (sel): self
  118.     nil?: self
  119.     IF        \ new: not done - do it now
  120.         new: super
  121.         handle: self  idx to: self
  122.     ELSE
  123.         reset: self
  124.     THEN  ;m
  125.  
  126. :m RELEASE:
  127.     limit 0 DO
  128.         i (sel): self  release: super    \ Harmless if not open
  129.         nilH  i to: self
  130.     LOOP  ;m
  131.  
  132. :m CLEARALL:
  133.     limit 0 DO
  134.         i (sel): self
  135.         handle: self  IF  clear: super  THEN
  136.     LOOP  ;m
  137.  
  138. :m DUMP:
  139.     ." Current:"  get: current  .  cr
  140.     dump: super  ;m
  141.  
  142. :m CLASSINIT:
  143.     idxbase  limit 4*  bounds
  144.     DO  nilH  i !  4 +LOOP  ;m
  145.  
  146. ;class
  147.  
  148.  
  149. string TEMP
  150.  
  151. 2048    hashtable        TRAPNAMES
  152. 2048    Warray            TRAP_INDEXES
  153. 10000    counted_strings    TRAPS
  154. 2048    stringarray        $TNAMES
  155.  
  156. 512        hashtable        GNAMES
  157. 512        array            GLOBALS
  158.  
  159. 4096    hashtable    KNAMES
  160. \ 4096    array        KONSTANTS
  161.  
  162.     0    value    #DBL
  163.     0    value    #TRAPS
  164.     0    value    #GLOBALS
  165.     0    value    #KONSTANTS
  166.  
  167.  
  168. : CHAROF { addr chr -- offs T | -- F }
  169.         \ Addr is of a str255 string.  Offs refers to found char.
  170.     addr count  chr  scan
  171.     IF  addr - 1-  true  ELSE  drop false  THEN  ;
  172.  
  173.  
  174. : READ_INLINE  { \ loc -- }
  175.     clear: temp
  176.     BEGIN
  177.         >in @  src-len  >=  ?EXIT
  178.         hex intrp1  pad w!  pad 2 add: temp
  179.     AGAIN  ;
  180.  
  181.  
  182. true    value    DBLFAIL?
  183.  
  184. : TRAPNAME  { \ hashval s255 idx dbl? -- }
  185.  
  186.     source bl scan
  187.     ( addr len ) IF  1+ src-start -  >in !  ELSE  drop  THEN
  188.  
  189.     Mword  -> s255            \ Trap name
  190.     s255 hash  -> hashval
  191.     hashval  enter: trapnames  not -> dbl?  -> idx
  192.     dbl? IF
  193.         idx select: $tnames  get: $tnames  s255 count  s=
  194.         NIF  here count cr type ."  - hash collision!!" cr abort  THEN
  195.         1 ++> #dbl  EXIT
  196.     THEN
  197.     idx select: $tnames  s255 count put: $tnames
  198.     read_inline
  199.     getSize: traps  idx  to: trap_indexes
  200.     all: temp  add: traps
  201.     1 ++> #traps  ;
  202.  
  203.  
  204. : GLOBNAME  { \ hashval val s255 -- }
  205.     \ Gets next word, adds if tool name, records parm if applicable
  206.     Mword hex number  -> val    \ global value
  207.     Mword -> s255            \ name
  208.     s255 hash  -> hashval
  209.     hashval  enter: gnames
  210.     NIF  ( match - check for hash collision )
  211.         at: globals  val <>
  212.         IF  ( hash collision - FAIL )
  213.             here count cr type ."  - hash collision!!" cr abort
  214.         THEN
  215.         1 ++> #dbl   EXIT
  216.     THEN
  217.     val swap to: globals  1 ++> #globals  ;
  218.  
  219.  
  220. : HANDLE_LINE        \ ( glob? -- )
  221.     IF  globname  ELSE  trapname  THEN  ;
  222.  
  223.  
  224. : TOOLS" { glob? \ radix svecho -- }
  225.             \ Reads toolbox name/trap table and fills arrays.
  226.     base -> radix  echo? -> svecho
  227.     new: temp
  228.     pushNew: loadFile  setName: topfile
  229.     openReadOnly: topfile  ?error 149
  230.     false -> endload?
  231.     BEGIN  ( read until eof )
  232.         (Frefill)
  233.     WHILE
  234.         tib c@  & \  <>            \ skip comments
  235.         if  glob? handle_line  then
  236.     REPEAT
  237.     drop: loadFile
  238.     release: temp
  239.     radix -> base  svecho -> echo?  ;
  240.  
  241.  
  242. \ The "konstants" file can be interpreted as a source file, since
  243. \ it consists of lines of the form
  244. \
  245. \ 1234 konstant    Name
  246. \
  247. \ The following word KONSTANT does the hard work.
  248.  
  249. (*
  250. : KONSTANT    \ ( value --<name> )
  251.     dup   constant        \ Define the name as a constant so
  252.                         \  later konsts can refer to it
  253.     latest  hash        \ Get the name, hash it
  254.     enter: knames
  255.     NIF  ( match - check for hash collision )
  256.         at: konstants  <>
  257.         IF  ( hash collision - FAIL )
  258.             here count cr type ."  - hash collision!!" cr abort
  259.         THEN
  260.         1 ++> #dbl   EXIT
  261.     THEN
  262.     to: konstants  1 ++> #konstants  ;
  263.  
  264. *)
  265. (* buggy:
  266.     : 'TYPEX    \ ( --< 'xxxx' > )  Modified 'TYPE to use with KONSTANT
  267.     pad 4 bl fill
  268.     & '  scan-src  source drop  & '  scan-src
  269.     source drop  over -  4 min
  270.     pad swap cmove  pad @  postpone lit  ;        immediate
  271. *)
  272.  
  273. : 'TYPEX    \ ( --< 'xxxx' > )  Modified 'TYPE to use with KONSTANT
  274.     pad 4 bl fill & ' word count 4 min
  275.     pad swap cmove pad @ postpone lit ;            immediate
  276.  
  277.  
  278. \ load the calls etc.
  279.  
  280.     4    constant  midiToolNum
  281. $ A830    constant  _pack14
  282.  
  283.             false -> dblFail?
  284. cr  .( Loading trap names...)            false tools" calls"
  285. cr  #dbl .    .( double-ups - ignore them)   0 -> #dbl
  286. cr  #traps .    .( trap names stored.  )
  287. cr  getSize: traps .    .(  bytes used for traps storage)
  288.  
  289. release: $tnames
  290.  
  291. cr  .( Loading low memory global names...)    true  tools" globals"
  292. cr  #globals .    .( global names stored)
  293. cr  #dbl .    .( double-ups in globals)    0 -> #dbl
  294.  
  295. (*
  296. cr  .( Loading konsts...)
  297. // konstants
  298. cr  #konstants  .    .( konsts stored)
  299. cr  #dbl .    .( double-ups in konsts)    0 -> #dbl
  300. *)
  301.  
  302. forget read_inline        \ dump table generation code
  303.  
  304.  
  305. : @TRAP  { tStr \ mStr flg addr len -- addr len }
  306.         \ Gets inline call sequence for a trap name.  tStr is str255.
  307.  
  308. \ I don't think we need the following now - "PBxxx" calls are still in my current
  309. \ header files.  If they ever disappear, we can reinstate this code:
  310.  
  311. \    tStr count  2 min  " PB"  s=
  312. \    IF  ( PB file calls now have the PB omitted )
  313. \        tStr count 2 /string  str255  -> tStr
  314. \    THEN
  315.  
  316.     0 -> mStr
  317.     tStr  & ,  charOf            \ stop short of comma if any
  318.     IF  dup tStr c! tStr + 2+ -> mStr  THEN
  319.     tStr hash  indexOf: trapnames  not ?error 150
  320.     at: trap_indexes  at: traps  -> len  -> addr    \ That's the call sequence
  321.     mStr IF                        \ a modifier exists
  322.         true
  323.         CASE
  324.             mStr 4 " REGS"    s= OF $ 01  ENDOF \ GetTrapAddr
  325.             mStr 5 " ASYNC"    s= OF $ 04  ENDOF \ device drivers
  326.             mStr 5 " IMMED"    s= OF $ 02  ENDOF \ control calls
  327.             mStr 3 " SYS"    s= OF $ 04  ENDOF \ Memory Manager
  328.             mStr 5 " CLEAR"    s= OF $ 02  ENDOF
  329.             mStr 5 " MARKS"    s= OF $ 04  ENDOF \ String Compares
  330.             mStr 4 " CASE"    s= OF $ 02  ENDOF
  331.             164 die            \ Illegal modifier name
  332.         ENDCASE
  333.         addr c@  $ F0 and  $ A0 <>  ?error 151    \ call seq must start $Axxx
  334.         addr pad len cmove  pad -> addr
  335.         addr c@ or  addr c!    
  336.     THEN
  337.     addr len  ;
  338.  
  339. : @GLOB        \ ( str-addr -- glob# )
  340.     hash  indexOf: gnames  0= ?error 150
  341.     at: globals  ;
  342.  
  343. \ : @KONST    \ ( str-addr -- konst )
  344. \    hash  indexOf: knames  0= ?error 150
  345. \    at: konstants  ;
  346.  
  347. : (,TRAP)    \ ( addr len -- )
  348.     tuck  here swap cmove  align allot  ;
  349.  
  350.  
  351. : ,TRAP        \ ( addr len -- )  Compiles the given inline code sequence.
  352.     SavA5  (,trap)  RstA5  ;
  353.  
  354.  
  355. : ,FCALL        \ Trap dispatcher for low-level File Manager
  356.     $ 205E w,        \    move.l    (a6)+,a0    ; FCB pointer
  357.     ,trap
  358.     $ 48C0 w,        \    ext.l    d0            ; Result
  359.     $ 2D00 w,  ;    \    move.l    d0,-(a6)
  360.  
  361.  
  362. \ Now the exported words:
  363.  
  364. : ASMCALL    \ ( addr len -- )  Compiles the trap.
  365.     str255 count upper
  366.     buf255  @trap
  367.     tuck  here swap cmove  align allot  ;
  368.  
  369. : CALL
  370.     ?comp
  371.     Mword  @Trap  ,trap  ;        immediate
  372.  
  373. : FCALL
  374.     ?comp
  375.     Mword  @Trap  ,fcall  ;        immediate
  376.  
  377. : GLOBAL
  378.     Mword  @glob  postpone lit  ;    immediate
  379.  
  380. : $>GLOB    \ ( addr len -- glob )
  381.     str255 count upper  buf255  @glob  ;
  382.  
  383.  
  384. \ : KONST
  385. \    Mword  @konst  postpone lit  ;    immediate
  386.  
  387. \ : $>KONST    \ ( addr len -- konst )
  388. \    str255 count upper  buf255  @konst  ;
  389.