1 ;;; Words for adding words
3 WORD p_here,'HERE',dovariable
7 WORD p_create,'CREATE',fasm
8 ;; CREATE ( chars* n -- tfa )
9 ;; Add the pstring as a new word in the current wordlist.
11 mov rdx,qword [p_wordlist_DFA] ; Current word list
12 mov rbx,qword [p_here_DFA]
13 mov rax,qword [rdx] ; set up tfa linking to previous word
15 mov qword [rbx+16],0 ; flags field
18 mov qword [rbx+24],rcx ; PFA (length)
19 pop rsi ; chars* (source)
20 lea rdi,[rbx+32] ; (dest)
26 mov byte [rdi],0 ; extra NUL
28 mov qword [rdi],rbx ; pTFA
30 mov qword [rdi],0 ; OFF
32 mov qword [rbx+8],rdi ; pCFA
33 mov qword [rdi],dovariable ; CFA
35 mov qword [p_here_DFA],rdi ; allocate the space
36 mov qword [rdx],rbx ; Install new word (rdx still wordlist ptr)
43 ;; Allocate n bytes on the heap
44 dq p_here, p_put_plus, p_return
48 ;; Find the following word and push its cfa, or 0
49 dq p_input, p_get, p_read_word, p_find
55 WORD p_bracketed_quote,"[']",doforth,IMMEDIATE
56 ;; Compilation ( "word" -- cfa )
57 ;; Compile down " LIT value "
58 dq p_literal, p_literal, p_comma,p_quote, p_comma, p_return
62 ;; Put cell value onto the heap and advance "HERE"
63 dq p_here, p_literal, 8, p_get_n_increment, p_put, p_return
67 ;; Put byte value onto the heap and advance "HERE"
68 dq p_here, p_Cput, p_literal, 1, p_here, p_put_plus, p_return
70 WORD p_does,"DOES>",fasm,IMMEDIATE
72 ;; Change the "DOES offset" of most recent word and assign it
73 ;; the "dodoes" execution semantics that follows.
76 tfa2does rax ; *rax is the DOES offset field
78 mov rcx,qword [p_here_DFA]
80 mov qword [rax],rcx ; save offset from DFA to HERE
81 mov qword [rax+8],dodoes
84 WORD p_literal,'LIT',fasm
86 ;; Push the value of successor cell onto stack, and skip it.
87 ;; not for interactive use!!
92 WORD p_literal_string,'S"',fasm,IMMEDIATE ;; " (fool emacs)
94 ;; Save string on heap to make available at interpretation
95 ;; not for interactive use!!
96 cmp qword [p_state_DFA],0
97 je p_literal_string_executing
99 mov rdi,qword [p_here_DFA]
100 mov qword [rdi],p_literal_string
102 mov qword [p_here_DFA],rdi
103 DOFORTH p_double_quote
106 mov rdi,qword [p_here_DFA]
109 p_literal_string_copy:
111 jl p_literal_string_copied
113 jmp p_literal_string_copy
114 p_literal_string_copied:
115 mov qword [p_here_DFA],rdi
119 p_literal_string_executing:
127 WORD p_state,'STATE',dovariable
128 ;; Interpretation state (0=interpreting, 1=compiling)
131 WORD p_left_bracket,'[',fasm,IMMEDIATE
133 ;; Change state to interpreting state.
134 mov qword[p_state_DFA],0
137 WORD p_right_bracket,']',fasm
139 ;; Change state to compilation state.
140 mov qword[p_state_DFA],1
143 WORD p_base,'BASE',dovariable
146 WORD p_decimal,'DECIMAL',fasm
149 mov qword [p_base_DFA],10
152 WORD p_hex,'HEX',fasm
155 mov qword [p_base_DFA],16
158 WORD p_number,'NUMBER',fasm
159 ;; ( chars* n -- [ 0 ]/[ v 1 ] )
161 pop rcx ; ( -- chars* )
164 mov rbx,1 ; sign (byte 0=0 means negative)
165 cmp qword [p_base_DFA],10
176 xor rax,rax ; clearing
186 mul qword [p_base_DFA] ; uses rdx:rax
196 cmp rax,qword [p_base_DFA]
205 cmp rax,qword [p_base_DFA]
214 jne p_numper_POSITIVE
222 WORD p_input,'INPUT',dovariable
223 ;; The current input stream for evaluate-stream
226 WORD p_this_word,'THIS-WORD',dovariable
227 dq 0,0 ; ( n chars* )
229 WORD p_evaluate_stream,'EVALUATE-STREAM'
230 ;; ( stream* -- *?* flag )
231 ;; Execute the words from the given stream
232 ;; returns 1 if stream ends and 0 if an unknown word is found
233 dq p_input, p_get, p_gtR ; save old stream on R-stack
235 p_evaluate_stream_PROMPT:
237 BRANCH 0,p_evaluate_stream_LOOP
244 p_evaluate_stream_LOOP:
248 BRANCH 0,p_evaluate_stream_END ; branch if 0 on TOP
249 dq p_2dup, p_this_word, p_2put
252 BRANCH 0,p_evaluate_stream_NOTWORD ; branch if 0 on TOP
255 BRANCH 0,p_evaluate_stream_INTERPRET
258 dq p_literal, 1 ; the immediate bit
260 BRANCH 0,p_evaluate_stream_COMPILE
261 p_evaluate_stream_INTERPRET:
264 BRANCH ,p_evaluate_stream_AFTER
265 p_evaluate_stream_COMPILE:
268 BRANCH ,p_evaluate_stream_AFTER
269 p_evaluate_stream_NOTWORD:
273 BRANCH 0,p_evaluate_stream_BAD ; branch if 0
276 BRANCH 0,p_evaluate_stream_AFTER ; branch if 0
277 dq p_literal, p_literal
279 p_evaluate_stream_AFTER:
282 BRANCH 0,p_evaluate_stream_PROMPT
283 BRANCH ,p_evaluate_stream_LOOP
284 p_evaluate_stream_END:
287 p_evaluate_stream_BAD:
288 dq p_Rgt, p_input, p_put ; restore previous stream
293 ;; Read next word as a new word into current wordlist, set it
294 ;; to be a doforth word, and set compiling mode.
295 dq p_literal, doforth
304 WORD p_semicolon,';',,IMMEDIATE
306 ;; Lay out p_return, and set interpreting mode
307 dq p_literal, p_return, p_comma, p_left_bracket, p_return
309 WORD p_immediate,'IMMEDIATE',fasm,IMMEDIATE
311 ;; Set "immediate flag" of the word being defined
312 mov rax,qword [p_wordlist_DFA]
313 mov rax,qword [rax] ; tfa of most recent word
314 mov qword [rax+16],1 ; set the flags field to 1
317 WORD p_load_buffer_size,'LOAD-BUFFER-SIZE',dovariable
319 ;; The buffer size (in bytes) used by LOAD-FILE
322 WORD p_open_file,'OPEN-FILE',fasm
324 ;; Open the nominated file
326 add rsp,8 ; drop n ... assuming NUL-ended string
333 WORD p_load_file,'LOAD-FILE'
337 BRANCH 1,p_load_file_badfile
338 dq p_load_buffer_size, p_get
339 dq p_stream, p_dup, p_gtR
342 BRANCH ,p_load_file_exit
345 STRING '** open file error: '
346 dq p_tell, p_dot, p_nl, p_emit