ensuring full cell arithmetics
[rrq/rrqforth.git] / wordlists.asm
index ae3062bedc82e6a2240b2cda0d729bb9f467d40d..b2e8260e479d275c24a3162776cb5ab28fc82757 100644 (file)
@@ -1,34 +1,50 @@
 ;;; This file contains words dealing with word lists (vocabularies)
+;;;
+;;; CURRENT-WORDLIST (variable) points out "the current wordlist"
+;;; SYSCALLS is a wordl list
+;;; FORTH is a word list
+;;;
+;;; !! When a word list word is created, it gets added to the tail end
+;;; of the current word list as way of making all word list words be
+;;; present in all word lists. This is different to all other kinds of
+;;; words, which instead are added to the head end of the current word
+;;; list.
+;;;
+;;; !! A word list word is created, it is initialised to the head end
+;;; of the current word list. It will this be an extension to that
+;;; current word list.
+;;;
+;;; EMPTY-WORDLIST is a word list word for an empty word list. It thus
+;;; only contains word list words.
+;;; 
+;;; WORDLIST ( "name" -- ) = start
 
-       WORD p_wordlist,'WORDLIST',dovariable
-       ;; VARIABLE WORDLIST is the currently active wordlist.
-       dq last_word
-
+       WORD p_wordlist,'CURRENT-WORDLIST',dovariable
+       ;; CURRENT-WORDLIST points to cfa of the currently active wordlist.
+       dq p_forth_DFA
+       
        WORD p_words,'WORDS',fasm
        ;; ( -- )
        ;; Dump all words
        pushr rsi
-       push qword [p_wordlist_DFA]
+       mov rax,qword [p_wordlist_DFA] ; current wordlist word
+       mov rax,[rax]                  ; list start
+       sub rsp,8                      ; use stack to hold ptr
 p_words_LOOP:
-       mov rax,qword [rsp]
+       mov qword [rsp],rax
        cmp rax,0
        je p_words_END
        ;; tfa>pfa
-       add rax,9
-       ;; pfa@ => ( chars* length)
-       xor rbx,rbx
-       mov bl,[rax]
-       inc rax
+       tfa2pfa rax
        push 1
-       push rax
-       push rbx
+       ;; pfa@ => ( chars* length)
+       pushpname rax
        DOFORTH sys_write
        pop rax                 ; ignore errors
-       push 10
+       push qword 10
        DOFORTH p_emit
        mov rax,qword [rsp]
        mov rax,qword [rax]
-       mov qword [rsp],rax
        jmp p_words_LOOP
 
 p_words_END:
@@ -58,33 +74,40 @@ p_strncmp_end:
        push rcx
        next
 
-       WORD p_find,'FIND'
-       ;; ( chars length -- [ chars 0 | cfa 1 )
+       WORD p_find,'FIND',fasm
+       ;; ( chars* length -- [ chars* length 0 ]|[ tfa ] )
        ;; Search the current wordlists for the given pname
+       pushr rsi
        mov rcx,[p_wordlist_DFA]
+       mov rcx,qword [rcx]     ; use rcx for word list traversing
+       mov rbx,qword [rsp]     ; rbx is input length
+       mov rsi,qword [rsp+8]   ; rsi is input chars*
 p_find_loop:
        cmp rcx,0
-       je p_find_done
-       mov rbx,[rsp]
-       mov rax,[rsp+8]
-       cmp bl,byte [rcx+10]
-       jne p_find_nextword
-       push rcx
+       je p_find_notfound      ; jump at end of word list
+       cmp rbx,qword [rcx+16]  ; compare lengths
+       jne p_find_nextword     ; jump on length mismatch
+       push rcx                ; save tfa for later
        ;; check word
-       push rax
-       push rcx+11
-       push rbx
+       push rsi                ; input chars
+       tfa2pname rcx
+       push rcx                ; word chars
+       push rbx                ; length
        DOFORTH p_strncmp
        pop rax                 ; return value
-       pop rcx
+       pop rcx                 ; restore tfa
        cmp rax,0
-       je p_find_done
+       je p_find_found
+       mov rbx,qword [rsp]
+       mov rsi,qword [rsp+8]
 p_find_nextword:
-       mov rcx,[rcx]
+       mov rcx,qword [rcx]
        jmp p_find_loop
+p_find_notfound:
+       xor rcx,rcx
+       sub rsp,16
 p_find_found:
-       mov qword [rsp+8],rcx   ; replace chars with tfa
-       mov rcx,1
-p_find_done:
-       push rcx
+       add rsp,8
+       mov qword [rsp],rcx     ; replace with tfa / 0
+       popr rsi
        next