using IFAGAIN instead
[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_definitions,'DEFINITIONS',fasm
18         ;; ( wordlist -- )
19         ;; Change CURRENT-WORDLIST to use the given word list
20         pop qword [p_wordlist_DFA]
21         next
22
23         WORD p_use,'USE',fasm
24         ;; ( wordlist "name" -- cfa )
25         ;; Read next word using the given wordlist
26         pushr rsi
27         mov rax,qword [p_wordlist_DFA]
28         pushr rax
29         pop qword [p_wordlist_DFA]
30         DOFORTH p_input, p_get, p_read_word, p_find
31         popr rax
32         mov qword [p_wordlist_DFA],rax
33         cmp qword [rsp],0
34         jne p_use_done
35         add rsp,16
36         mov qword [rsp],0
37 p_use_done:
38         popr rsi
39         next
40
41         WORD p_words,'WORDS',fasm
42         ;; ( w -- )
43         ;; Dump all words of the word list w (the dfa of a word list)
44         pushr rsi
45 p_words_LOOP:
46         mov rax,qword [rsp]
47         mov rax,qword [rax]     ; Next word
48         mov qword [rsp],rax
49         cmp rax,0
50         je p_words_END
51         tfa2pfa rax
52         push 1                  ; stdout
53         pushpname rax           ; ( pfa* -- chars* length )
54         DOFORTH sys_write
55         pop rax                 ; ignore errors
56         push qword 10
57         DOFORTH p_emit          ; ( c -- )
58         jmp p_words_LOOP
59
60 p_words_END:
61         pop rax
62         popr rsi
63         next
64
65         WORD p_strlen,'STRLEN',fasm
66         ;; ( chars -- n )
67         ;; Determine length of NUL terminated byte sequence
68         pushr rsi
69         mov rsi,qword [rsp]
70         xor rcx,rcx
71         dec rcx
72         cld
73 p_strlen_LOOP:
74         inc rcx
75         lodsb
76         cmp al,0
77         jne p_strlen_LOOP
78         mov qword [rsp],rcx
79         popr rsi
80         next
81
82         WORD p_strncpy,'STRNCPY',fasm
83         ;; ( chars1 chars2 n -- )
84         ;; Copy n bytes from chars1 to chars2.
85         pushr rsi
86         pop rcx
87         pop rdi
88         pop rsi
89         cmp rcx,0
90         jle p_strncpy_END
91         cld
92 p_strncpy_LOOP:
93         movsb
94         dec rcx
95         jg p_strncpy_LOOP
96 p_strncpy_END:
97         popr rsi
98         next
99         
100         WORD p_strncmp,'STRNCMP',fasm
101         ;; ( chars1 chars2 n -- flag )
102         ;; Compare bytes until one is NUL, return <0, =0 or >0 to
103         ;; indicate that chars1 is lesser, they are equal, or chars2
104         ;; is lesser in ascii ordering respectively.
105         pushr rsi
106         pop rcx                 ; count
107         pop rsi                 ; chars2
108         pop rdi                 ; chars1
109         xor rax,rax
110         cmp rcx,0
111         jle p_strncmp_end
112         ;; rax = chars1, rbx = chars2, cl = byte acc, rdx = length
113         cld
114 p_strncmp_loop:
115         cmpsb
116         jne p_strncmp_diff
117         dec rcx
118         jg p_strncmp_loop
119 p_strncmp_diff:
120         xor rax,rax
121         mov al,[rsi-1]
122         sub al,[rdi-1]
123 p_strncmp_end:
124         push rax
125         popr rsi
126         next
127
128         WORD p_find,'FIND',fasm
129         ;; ( chars* length -- [ chars* length 0 ]|[ tfa ] )
130         ;; Search the current wordlists for the given pname
131         pushr rsi
132         mov rcx,[p_wordlist_DFA] ; the current top word list
133         mov rdx,qword [rcx+8]    ; successor word list
134         pushr rdx
135         mov rcx,qword [rcx]     ; use rcx for word list traversing
136         mov rbx,qword [rsp]     ; rbx is input length
137         mov rsi,qword [rsp+8]   ; rsi is input chars*
138 p_find_loop:
139         cmp rcx,0
140         je p_find_notfound      ; jump at end of word list
141         cmp rbx,qword [rcx+24]  ; compare lengths
142         jne p_find_nextword     ; jump on length mismatch
143         push rcx                ; save tfa for later
144         ;; check word
145         push rsi                ; input chars
146         tfa2pname rcx
147         push rcx                ; word pname
148         push rbx                ; length
149         DOFORTH p_strncmp       ; ( s1* s2 n -- v )
150         pop rax                 ; return value v
151         pop rcx                 ; restore tfa
152         cmp rax,0
153         je p_find_found
154         mov rbx,qword [rsp]
155         mov rsi,qword [rsp+8]
156 p_find_nextword:
157         mov rcx,qword [rcx]
158         jmp p_find_loop
159 p_find_notfound:
160         mov rcx,qword [rbp]     ; successor word list
161         cmp rcx,0
162         je p_find_nomore
163         mov rdx,qword [rcx]
164         mov qword [rbp],rdx
165         jmp p_find_loop
166 p_find_nomore:
167         push 0
168         add rbp,8               ; discard word list link
169         popr rsi
170         next
171 p_find_found:
172         add rsp,8               ; drop one stack entry
173         mov qword [rsp],rcx     ; replace with tfa / 0
174         add rbp,8               ; discard word list link
175         popr rsi
176         next