adding execution control words
[rrq/rrqforth.git] / wordlists.asm
1 ;;; This file contains words dealing with word lists (aka vocabularies)
2 ;;;
3 ;;; CURRENT-WORDLIST (variable) points out "the current wordlist"
4 ;;; FORTH is a word list
5 ;;;
6
7         ;; FORTH is the last word of WORDLIST FORTH
8         WORD p_forth,'FORTH',dovariable
9         ;; The FORTH word list
10         dq last_forth_word      ; tfa of last FORTH word
11         dq 0                    ; successor word list dfa
12
13         WORD p_wordlist,'CURRENT-WORDLIST',dovariable
14         ;; CURRENT-WORDLIST points to dfa of the currently active wordlist.
15         dq p_forth_DFA          ; compilation word list
16
17         WORD p_words,'WORDS',fasm
18         ;; ( w -- )
19         ;; Dump all words of the word list w (the dfa of a word list)
20         pushr rsi
21 p_words_LOOP:
22         mov rax,qword [rsp]
23         mov rax,qword [rax]     ; Next word
24         mov qword [rsp],rax
25         cmp rax,0
26         je p_words_END
27         tfa2pfa rax
28         push 1                  ; stdout
29         pushpname rax           ; ( pfa* -- chars* length )
30         DOFORTH sys_write
31         pop rax                 ; ignore errors
32         push qword 10
33         DOFORTH p_emit          ; ( c -- )
34         jmp p_words_LOOP
35
36 p_words_END:
37         pop rax
38         popr rsi
39         next
40
41         WORD p_strncmp,'STRNCMP',fasm
42         ;; ( chars1 chars2 n -- flag )
43         ;; Compare bytes until one is NUL, return <0, =0 or >0 to
44         ;; indicate that chars1 is lesser, they are equal, or chars2
45         ;; is lesser in ascii ordering respectively.
46         pop rdx                 ; count
47         pop rbx                 ; chars2
48         pop rax                 ; chars1
49         xor rcx,rcx
50         ;; rax = chars1, rbx = chars2, cl = byte acc, rdx = length
51 p_strncmp_loop:
52         dec rdx
53         jl p_strncmp_end
54         mov cl,[rax]
55         inc rax
56         inc rbx
57         sub cl,[rbx-1]
58         je p_strncmp_loop
59         jmp p_strncmp_end
60 p_strncmp_end:
61         push rcx
62         next
63
64         WORD p_find,'FIND',fasm
65         ;; ( chars* length -- [ chars* length 0 ]|[ tfa ] )
66         ;; Search the current wordlists for the given pname
67         pushr rsi
68         mov rcx,[p_wordlist_DFA] ; the current top word list
69         mov rdx,qword [rcx+8]    ; successor word list
70         pushr rdx
71         mov rcx,qword [rcx]     ; use rcx for word list traversing
72         mov rbx,qword [rsp]     ; rbx is input length
73         mov rsi,qword [rsp+8]   ; rsi is input chars*
74 p_find_loop:
75         cmp rcx,0
76         je p_find_notfound      ; jump at end of word list
77         cmp rbx,qword [rcx+24]  ; compare lengths
78         jne p_find_nextword     ; jump on length mismatch
79         push rcx                ; save tfa for later
80         ;; check word
81         push rsi                ; input chars
82         tfa2pname rcx
83         push rcx                ; word pname
84         push rbx                ; length
85         DOFORTH p_strncmp       ; ( s1* s2 n -- v )
86         pop rax                 ; return value v
87         pop rcx                 ; restore tfa
88         cmp rax,0
89         je p_find_found
90         mov rbx,qword [rsp]
91         mov rsi,qword [rsp+8]
92 p_find_nextword:
93         mov rcx,qword [rcx]
94         jmp p_find_loop
95 p_find_notfound:
96         mov rcx,qword [rbp]     ; successor word list
97         cmp rcx,0
98         je p_find_nomore
99         mov rdx,qword [rcx]
100         mov qword [rbp],rdx
101         jmp p_find_loop
102 p_find_nomore:
103         push 0
104         add rbp,8               ; discard word list link
105         popr rsi
106         next
107 p_find_found:
108         add rsp,8               ; drop one stack entry
109         mov qword [rsp],rcx     ; replace with tfa / 0
110         add rbp,8               ; discard word list link
111         popr rsi
112         next