home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / ZEN1_10.ZIP / INTRPRET.SRC < prev    next >
Encoding:
Text File  |  1990-01-25  |  8.7 KB  |  339 lines

  1. \*
  2.  *   ZEN 1.10  Interpreter
  3.  *     C 1990  by Martin Tracy
  4.  *             Last modified  1.1.90
  5.  *\
  6.  
  7. \ Address space pointers.  Keep together.
  8.   VARIABLE DP     \ Dictionary pointer
  9. | VARIABLE TP     \ Table area pointer
  10. | VARIABLE VP     \ Array area pointer
  11.  
  12. \ Next available table location.
  13. : HERE ( - a) \ CORE
  14.    TP @ ;
  15.  
  16. \ Next available array location.
  17. : THERE ( - a)
  18.    VP @ ;
  19.  
  20. \ Add w to the dictionary
  21. CODE X, ( w)
  22.         mov   di,[_DPv]
  23.         mov   cs:[di],bx
  24.         add   [_DPv],2
  25.         pop   bx
  26.         NEXT
  27. END-CODE
  28.  
  29.  
  30. \ Keep together
  31. VARIABLE BLK    \ Input stream switch CORE
  32. VARIABLE >IN    \ Input stream offset CORE
  33.  
  34. \ Keep together
  35.   VARIABLE #TIB   \ Count of characters in TIB CORE
  36. | VARIABLE  TUB   \ TIB pointer
  37.   80 ALLOT        \ Default Terminal Input Buffer (TIB)
  38.  
  39. \ Terminal Input Buffer
  40. : TIB ( - a) \ CORE
  41.    TUB @ ;
  42.  
  43. \ Terminal input stream.
  44. | : SOURCE ( - a u)
  45.    TUB @ #TIB @ ;
  46.  
  47. \ Parse a character-delimited string.
  48. \ Leading delimiters are accepted and skipped;
  49. \ the string is counted and followed by an uncounted blank.
  50. : WORD ( c " ccc " - a) \ CORE
  51.    >R  SOURCE  >IN @ /STRING  OVER R> 2>R
  52.    R@ SKIP  OVER SWAP
  53.    R> SCAN  OVER R> -  SWAP  IF  1+  THEN  >IN +!
  54.    OVER -  31 MIN  THERE  DUP >R
  55.    PLACE   BL R@ COUNT + C!   R> ;
  56.  
  57. \ Parse ccc and return the integer value of its first character.
  58. : CHAR ( "ccc " ; - c) \ CORE
  59.    BL WORD  COUNT HUH?  C@ ;
  60.  
  61. \ Search vocabulary for a match with the packed name at  a .
  62. CODE THREAD ( a w - a 0 , cfa -1 , cfa 1)
  63.         pop   dx
  64.         push  si
  65.         mov   ax,cs
  66.         mov   es,ax
  67.         jmp   Thr2
  68. Thr1:   mov   bx,cs:[bx] ; chain thru dictionary
  69. Thr2:   or    bx,bx
  70.         jz    Thr5       ; jump if end of thread
  71.         mov   si,dx      ; 'string
  72.         mov   di,bx
  73.         add   di,2       ; si -> nfa
  74.         mov   cl,cs:[di]
  75.         and   cx,01Fh
  76.         cmp   cl,[si]    ; counts equal?
  77.         jnz   Thr1
  78.         inc   si         ; -> body of 'string
  79.         inc   di
  80.         repe cmpsb       ; names equal?
  81.         jnz   Thr1
  82.         pop   si
  83.         push  di         ; cfa
  84.         test  BYTE PTR cs:[bx+2],020h ; immediate bit
  85.         mov   bx,TRUTH
  86.         jz    Thr4
  87.         neg   bx
  88. Thr4:   NEXT
  89. Thr5:   pop   si
  90.         push  dx         ; bx = 0
  91.         NEXT
  92. END-CODE
  93.  
  94. \ Convert string to upper case in place.
  95. CODE UPCASE ( a u)
  96.         xchg  cx,bx
  97.         pop   bx
  98.         jcxz  UpC3
  99. UpC1:   mov   al,[bx]
  100.         cmp   al,'a'
  101.         jb    UpC2
  102.         cmp   al,'z'
  103.         ja    UpC2
  104.         xor   al,32
  105.         mov   [bx],al
  106. UpC2:   inc   bx
  107.         loop  UpC1
  108. UpC3:   pop   bx
  109.         NEXT
  110. END-CODE
  111.  
  112. \*
  113. \ Convert string to upper case in place.
  114. : UPCASE ( a u)
  115.    0 ?DO  DUP C@  DUP [CHAR] a [CHAR] z 1+ WITHIN
  116.           IF  BL XOR  THEN  OVER C!  1+
  117.       LOOP  DROP ;
  118. *\
  119.  
  120. \ STATE variables.  Keep together.
  121.   VARIABLE STATE   \ True if compiling CORE
  122. | VARIABLE SBCTR   \ SB pointer in STRINGS2.SRC
  123. | VARIABLE EVCTR   \ EVAL index in FILES.SRC
  124.   CELL ALLOT       \ Spare
  125.  
  126. \ Stop interpreting; start compiling.
  127. : ] ( ) \ CORE
  128.    STATE ON ;
  129.  
  130. \ Stop compiling; start interpreting.
  131. : [ ( ) \ CORE
  132.    STATE OFF ;
  133. IMMEDIATE
  134.  
  135. LOX? \*IF
  136. | VARIABLE LOCS   \ Locals index counter.
  137. | VARIABLE LOC$   \ Locals name array.  Max 8 names.
  138. 32 8 * ALLOT      \ Names go here.
  139.  
  140. \ Find local in local name array.
  141. | : LOCAL? ( a - a 0 | w -1)
  142.    LOCS  @ 0= IF  0  EXIT THEN
  143.    STATE @ DUP
  144.    IF  DROP  DUP C@ 1+  LOC$ CELL+  LOCS @  0
  145.       DO  ( a n+1 a2) >R  2DUP R@  -TEXT 0=
  146.          IF  R> DROP 2DROP  I 6 * ['] LOCT +  TRUE  UNLOOP EXIT  THEN
  147.          R> COUNT +
  148.       LOOP   2DROP 0
  149.    THEN ;
  150. *\
  151.  
  152.  
  153. VOX? \*IF
  154. 8 EQU #VOCS   \ Maximum 8 vocabularies in the search order
  155.  
  156. VARIABLE CURRENT   \ Pointer to definition  VOCABULARY
  157. VARIABLE CONTEXT   \ Pointer to search link VOCABULARY
  158. #VOCS CELLS ALLOT
  159.  
  160. : DEFINITIONS   \ VOCABULARY
  161.    CONTEXT @ CURRENT ! ;
  162.  
  163. VOCABULARY ROOT    \ Put search order words here
  164. ROOT DEFINITIONS
  165.  
  166. VOCABULARY FORTH   \ VOCABULARY
  167.  
  168. \ Adds CONTEXT to the fixed search order.
  169. : ALSO   \ VOC EXT
  170.    CONTEXT  DUP CELL+ [ #VOCS 1- CELLS ] LITERAL CMOVE> ;
  171.  
  172. \ Sets vocabulary stack to ROOT only.
  173. : ONLY   \ VOC EXT
  174.    CONTEXT CELL+ [ #VOCS CELLS ] LITERAL ERASE  ROOT ALSO ;
  175.  
  176. \ Remove the transient first vocabulary from the search order.
  177. \ Must be the last definition in the ROOT vocabulary.
  178. : PREVIOUS   \ VOC EXT
  179.    CONTEXT  DUP CELL+ SWAP [ #VOCS CELLS ] LITERAL CMOVE ;
  180.  
  181. FORTH DEFINITIONS
  182.  
  183. \ Search dictionary for a match with the given counted name.
  184. \ Return execution address and -1 or 1 ( IMMEDIATE) if found;
  185. \ ['] EXIT 1 if  a  has zero length;  a 0  if not found.
  186. : FIND ( a - a 0 | a - w -1 | a - w 1) \ CORE
  187.    DUP C@ IF  DUP COUNT UPCASE
  188. [ LOX? ] \IF  LOCAL?              ?DUP IF  EXIT  THEN
  189.               CONTEXT @ @ THREAD  ?DUP IF  EXIT  THEN  CONTEXT
  190.    BEGIN  DUP CELL+  DUP @ 0= IF  2DROP  0 EXIT  THEN  SWAP 2@ -
  191.       IF  ( 'string 'thread)  TUCK @ @ THREAD
  192.           ?DUP IF  ROT DROP  EXIT  THEN  SWAP
  193.       THEN
  194.    AGAIN ( stops at sentinel)
  195.    ELSE  DROP  ['] EXIT 1  THEN ;
  196. *\
  197.  
  198. VOX? NOT \*IF
  199. \ Keep together
  200.   VARIABLE CONTEXT   \ Pointer to search link VOCABULARY
  201. | VARIABLE CURRENT   \ Pointer to definition  VOCABULARY
  202. | VARIABLE 'FORTH    \ Forth VOCABULARY body
  203.  
  204. \ Search dictionary for a match with the given counted name.
  205. \ Return execution address and -1 or 1 ( IMMEDIATE) if found;
  206. \ ['] EXIT 1 if  a  has zero length;  a 0  if not found.
  207. : FIND ( a - a 0 | a - w -1 | a - w 1) \ CORE
  208.    DUP C@ IF  DUP COUNT UPCASE
  209. [ LOX? ] \IF  LOCAL?             ?DUP IF  EXIT  THEN
  210.               CONTEXT @ @ THREAD  DUP IF  EXIT  THEN
  211.               CONTEXT @ 'FORTH -
  212.               IF  DROP  'FORTH  @ THREAD  THEN  EXIT
  213.           ELSE  DROP  ['] EXIT 1  THEN ;
  214. *\
  215.  
  216.  
  217. \ # items on stack before DEPTH is executed.
  218. CODE DEPTH ( - n) \ CORE
  219.         push bx
  220.         mov  bx,(OFFSET DGROUP: SP0)-2
  221.         sub  bx,sp
  222.         sar  bx,1
  223.         NEXT
  224. END-CODE
  225.  
  226. \ Empty data stack.
  227. CODE BARE ( )
  228.         sti
  229.         mov   sp,OFFSET DGROUP: SP0
  230.         cli
  231.         NEXT
  232. END-CODE
  233.  
  234. \ Reset return stack and STATE variables.
  235. \ Used for error recovery.
  236. : RESET ( )
  237.    STATE 4 CELLS 0 FILL [
  238. ASSEMBLER
  239.         dw    Res1
  240. Res1:   mov   si,[bp]
  241.         mov   bp,OFFSET DGROUP: RP0
  242.         NEXT
  243. END-CODE
  244.  
  245.  
  246. \ Keep together
  247.   VARIABLE 'MAIN   \ Main entry point.  Default error handler comes here.
  248. | VARIABLE 'ERR    \ ERR transfer vector
  249.  
  250. \ Default error recovery used by ABORT"
  251. | : ERR ( )
  252.    'ERR PERFORM ;
  253.  
  254. \ Error action of several words.
  255. | : HUH? ( w)
  256.    0= ABORT" ?" ;
  257.  
  258. \ Compile w as a literal.
  259. : LITERAL ( - w) \ CORE
  260.    POSTPONE Lit  ( w) X, ;
  261. IMMEDIATE
  262.  
  263. \ Compile the top w stack items as numeric literals.
  264. | : VAL, ( ... w)
  265.    DUP BEGIN  ROT >R                1- ?DUP 0= UNTIL
  266.        BEGIN  R>  POSTPONE LITERAL  1- ?DUP 0= UNTIL ;
  267.  
  268. \ Return the execution token for the following word.
  269. : ' ( " name " ; - w) \ CORE
  270.    BL WORD  DUP C@ HUH?  FIND HUH? ;
  271.  
  272. \ The interpreter proper.
  273. | : INTERPRET ( )
  274.    BEGIN  BL WORD  FIND  ?DUP ( found?)
  275.      IF    STATE @ = ( Imm?)
  276.            IF  X,  ELSE  EXECUTE  THEN
  277.      ELSE  COUNT VAL?  DUP HUH?
  278.            STATE @ IF  VAL,  ELSE  DROP  THEN
  279.      THEN
  280.    AGAIN ;
  281.  
  282. \ Interpret a string.
  283. : EVALUATE ( a u) \ CORE
  284.    #TIB 2@ 2>R  #TIB 2!  BLK 2@ 2>R  0 0 BLK 2!  INTERPRET
  285.    2R> BLK 2!  2R> #TIB 2! ;
  286.  
  287.  
  288. \ Fill TIB from next line of input stream.
  289. : QUERY \ EXT CORE
  290.    0 0 BLK 2!  TUB CELL+ TUB !  TIB 80 EXPECT  SPAN @ #TIB ! ;
  291.  
  292. \ Status check.
  293. | : OK? ( )
  294.    DEPTH 0< ABORT" Stack?"
  295.    STATE @ 0= IF  ." Ok"  THEN ;
  296.  
  297. \ Default main program.
  298. : QUIT ( ) \ CORE
  299.    RESET  'MAIN PERFORM
  300.    BEGIN  CR QUERY  SPACE INTERPRET  OK?  AGAIN ;
  301.  
  302. \ Default main program,
  303. : ABORT ( ?) \ CORE
  304.    BARE  QUIT ;
  305.  
  306. \ Default error handler.
  307. | : GRIPE ( a u)
  308.    THERE COUNT 1+ TYPE ( msg ) TYPE  ABORT ;
  309.  
  310.  
  311. \ Print signed if decimal, unsigned otherwise.
  312. | : X. ( n)
  313.    BASE @ 10 = IF  .  EXIT  THEN  U. ;
  314.  
  315. \ Print contents at address.
  316. : ? ( a) \ EXT CORE
  317.    @ X. ;
  318.  
  319. \ Print stack contents.
  320. : .S ( ?) \ RESERVED
  321.    CR  DEPTH 0 MAX  ?DUP 0= IF  ." Empty "  EXIT THEN
  322.    ." --> "  DUP 0 DO  DUP I - PICK X.  LOOP  DROP ;
  323.  
  324. \ Display n bytes from address a.
  325. : DUMP ( a n) \ RESERVED
  326.    0 ?DO  I 7 AND 0= IF  CR  THEN  DUP C@ U.  1+ LOOP  DROP ;
  327.  
  328. \ For Your Information.
  329. : FYI ( )
  330.      CR ." Bytes remaining" [
  331. ASSEMBLER
  332.         dw    Lit, Table_Area_Size, Lit, OFFSET TP0
  333.         dw    Lit, Array_Area_Size, Lit, OFFSET VP0
  334.         dw    Lit, Dictionary_Size, Lit, OFFSET DP0
  335. END-CODE
  336.    ] CR ." Dict : " - U.
  337.      CR ." Array: " - U.
  338.      CR ." Table: " - U.  SPACE ;
  339.