code: 9ferno

Download patch

ref: 84b7df170ff07aa5b082570c996e37caedb5caa9
parent: a6fb15ce74d7623c1e3c01bdfeef16fd4279cdca
author: 9ferno <gophone2015@gmail.com>
date: Tue Feb 15 21:48:45 EST 2022

cleaned up bugs around restoring inputs

--- a/os/init/disinit.b
+++ b/os/init/disinit.b
@@ -36,7 +36,7 @@
 	sys->bind("#d", "/fd", Sys->MREPL);		# dup(3)
 	sys->bind("#e", "/env", sys->MREPL|sys->MCREATE);	# environment
 #	sys->bind("#i", "/dev", sys->MREPL);	# draw device
-	sys->bind("#l0", "/net", sys->MAFTER);	# Ehernet device
+	sys->bind("#l0", "/net", sys->MAFTER);	# Ethernet device
 	sys->bind("#m","/dev",sys->MAFTER);     # pointer/mouse
 	sys->bind("#p", "/prog", sys->MREPL);	# prog device
 	sys->bind("#r", "/dev", sys->MAFTER);	# rtc
--- a/os/pc64/trap.c
+++ b/os/pc64/trap.c
@@ -153,7 +153,7 @@
 		return;
 	print("	depth %zd\n", (h+PSTACK - psp)/sizeof(intptr));
 	for(i = h + PSTACK-8; i >= psp; i-=8){
-		print("	0x%zX: 0x%zX", i, *(intptr*)i);
+		print("	0x%zX: 0x%zX %d", i, *(intptr*)i, *(intptr*)i);
 		l++;
 		if(l == 3){
 			l = 0;
--- a/os/pc64/words-nasm.s
+++ b/os/pc64/words-nasm.s
@@ -950,7 +950,7 @@
 dd M_literal
 dd 0
 dd M_doinit
-L_C_stdinput:
+L_C_stdinput_loop:
 
 dd M_literal
 dd -1
@@ -963,7 +963,7 @@
 dd M_plus
 
 dd M_doloop
-dd L_C_stdinput
+dd L_C_stdinput_loop
 dd M_drop
 dd M_exitcolon
 
@@ -995,10 +995,11 @@
 dd M_literal
 dd 0
 dd M_doinit
-L_C_input_fetch:
+L_C_input_fetch_loop:
 
 dd M_dup	; ( 'Bufferfd 'Bufferfd )
 dd M_fetch
+
 dd M_xswap	; ( fd 'Bufferfd )
 
 dd M_literal
@@ -1012,7 +1013,7 @@
 dd M_plus
 
 dd M_doloop
-dd L_C_input_fetch
+dd L_C_input_fetch_loop
 dd M_drop	; ( fd0 fd1 .. fdn )
 
 dd MV_Infd
@@ -1025,7 +1026,6 @@
 
 dd M_exitcolon
 
-Buggy
 CENTRY "input!" C_input_store 6 ; ( fd0 fd1 .. fdn infd n+1 | empty -- ) restore input stream from the stack or stdinput
 dd M_dup		; check if there is #Buffers+1 on the top of stack
 
@@ -1035,7 +1035,7 @@
 dd M_plus
 dd M_equal	; is the top of stack == #Buffers+1
 dd M_cjump
-dd L_C_input_store_1	; top of stack <> #Buffers+1, there is no input stream on the stack, use the default input
+dd L_C_input_store_no_stream	; top of stack <> #Buffers+1, there is no input stream on the stack, use the default input
 
 dd M_drop	; drop the #Buffers+1 on the top of stack
 
@@ -1065,6 +1065,7 @@
 
 dd M_doloop
 dd L_C_input_store_loop
+dd M_drop	; remove the 'Bufferfds on top
 
 dd MV_Eof
 dd C_off	; reset Eof back to 0
@@ -1072,7 +1073,7 @@
 dd C_true	; ( true )
 dd M_exitcolon
 
-L_C_input_store_1:	; there is no input stream on the stack
+L_C_input_store_no_stream:	; there is no input stream on the stack
 dd C_stdinput	; no input stream on the stack, use default input from now
 dd C_false		; ( 0 )
 dd M_exitcolon
@@ -1136,12 +1137,12 @@
 dd C_input_store
 dd C_0eq
 dd M_cjump
-dd L_restore_input	; input stream restored
+dd L_C_restore_input_exit	; input stream restored
 
 ; no input stream on the stack to restore, show error and abort
 dd C_space
 dd M_literal
-dd L137
+dd L_C_restore_input_error_message
 dd C_count
 dd C_type
 dd C_space
@@ -1148,8 +1149,9 @@
 dd C_depth
 dd C_dot
 dd C_cr
+
 dd C_abort
-L_restore_input:	; input stream restored, get out
+L_C_restore_input_exit:	; input stream restored, get out
 dd M_exitcolon
 
 CENTRY "concat" C_concat 6 ; ( 'cs1 'cs2 -- 'cs1+'cs2 ) concatenate counted string2 to counted-string1
@@ -1201,6 +1203,8 @@
 ; max of a counted string is 256 bytes. Hence, cannot use it.
 ; reads into Tib and puts the read count on the stack. Could move the file reading into accept.
 CENTRY "query" C_query 5 ; ( index -- read_count ) read from the indexed Fd in Bufferfds into Tib
+
+L_C_query_again:
 dd MV_Eof
 dd C_off	; clear EOF flag
 
@@ -1254,15 +1258,16 @@
 dd M_cjump
 dd L_C_query_read_failed
 
-dd C_nip	; ( read_count )
-dd M_dup	; ( read_count read_count )
+dd M_dup	; ( index read_count read_count )
+
 dd M_cjump
 dd L_C_query_read_0
 
-dd M_dup	; read_count > 0 ( read_count read_count )
+dd M_dup	; read_count > 0 ( index read_count read_count )
 dd M_literal
 dd 4096
 dd M_equal
+
 dd M_cjump
 dd L_C_query_read_successful
 
@@ -1286,13 +1291,19 @@
 dd M_exitcolon
 
 L_C_query_read_0:
-dd M_drop	; ( ) read_count == 0
+
+dd M_drop	; ( index ) read_count == 0
+dd M_rpush	; ( ) (R index ) save index for use after restoring input
 dd MV_Eof
 dd C_on		; end of file, qrestore_input
 dd C_restore_input
-dd M_exitcolon
 
+dd M_rpop	; ( index ) (R )
+dd M_jump	; ( index )
+dd L_C_query_again
+
 L_C_query_read_successful:
+dd C_nip
 dd M_exitcolon	; ( read_count ) successful read, get out
 
 CENTRY "parse" C_parse 5 ; ( read_count -- 'Wordb ) Wordb has a counted string. read_count bytes read into Tib
@@ -1330,11 +1341,8 @@
 
 CENTRY "word" C_word 4 ; ( -- 'Wordb ) read from #n/Infd/word into Tib and then parse to a counted string in Wordb
 dd MC_WORDNUM
-
 dd C_query
-
 dd C_parse
-
 dd M_exitcolon
 
 CENTRY "line" C_line 4 ; ( -- count ) read from #n/Infd/line into Tib
@@ -1450,7 +1458,7 @@
 dd C_find
 dd C_0eq
 dd M_cjump
-dd L_C_single_quote
+dd L_C_single_quote_exit
 dd C_space
 dd C_count
 dd C_type
@@ -1460,7 +1468,7 @@
 dd C_type
 dd C_cr
 dd C_abort
-L_C_single_quote:
+L_C_single_quote_exit:
 dd M_exitcolon
 
 CENTRY "?stack" C_qstack 6
@@ -1468,14 +1476,14 @@
 dd MV_S0
 dd M_greater
 dd M_cjump
-dd L_C_qstack
+dd L_C_qstack_exit
 dd M_literal
-dd L173
+dd L_C_stack_underflow_message
 dd C_count
 dd C_type
 dd C_cr
 dd C_abort
-L_C_qstack:
+L_C_qstack_exit:
 dd M_exitcolon
 
 dd MC_STDOUT	; ( str -- str 1) ; debug code to show the word found
@@ -1491,9 +1499,7 @@
 dd L_C_interpret_not_found
 
 dd M_execute	; found in dictionary, execute
-
 dd C_qstack		; check stack status
-
 dd M_exitcolon
 
 L_C_interpret_not_found:	; ( 'Wordb ) not found in the dictionary, check for number?
@@ -1787,6 +1793,7 @@
 
 CIENTRY "\\" CI_backslash 1 ; if the line is longer than 4096, C_query throws an error
 dd C_line
+dd M_drop
 dd M_exitcolon
 
 CENTRY "(?abort)" C_qabort_parens 8
@@ -2044,7 +2051,7 @@
 CENTRY "?fcheck" C_qfcheck 7
 dd C_0eq
 dd M_cjump
-dd L246
+dd L_C_qfcheck_exit
 dd C_space
 dd M_literal
 dd L_io_error
@@ -2052,7 +2059,7 @@
 dd C_type
 dd C_cr
 dd C_abort
-L246:
+L_C_qfcheck_exit:
 dd M_exitcolon
 
 CENTRY "create-file" C_create_file 11 ; ( a n mode perm -- fd ioresult ) not part of the original ff. could move this to a forth file.
@@ -2096,18 +2103,20 @@
 
 CENTRY "include" C_include 7	; this does not work
 dd C_word
+
 dd M_rpush
 
-dd C_input_fetch	; save the old input onto the stack
+dd C_input_fetch	; save the old input onto the stack and clears the input bufferfds
 
 dd M_rpop
 dd C_count
 dd C_ro
 dd C_open_file
+
 dd C_qfcheck
+
 dd MV_Infd		; open the new file
 dd M_store
-
 dd M_exitcolon
 
 CENTRY "crash" C_crash 5
@@ -2123,22 +2132,11 @@
 dd M_reset ; initialize return stack
 dd M_clear	; SP = sstack_end initialize data stack
 
-L_C_quit:
+L_C_quit_interpreter_loop:
 dd C_word
-
-; dd MV_toLimit	; show the line read, for debugging
-; dd M_fetch
-; dd MV_Tib
-; dd MC_STDOUT
-; dd M_fswrite
-; dd M_drop		; drop the return value of write
-; dd C_cr
-; dd C_space
-
 dd C_interpret
-
 dd M_jump
-dd L_C_quit
+dd L_C_quit_interpreter_loop
 dd M_exitcolon	; why is this needed?
 
 CENTRY "(abort)" C_parenabort 7 ; TODO correct below stack notations
@@ -2183,7 +2181,7 @@
 dd M_literal
 dd 0
 dd M_doinit
-L_C_initialize:
+L_C_initialize_fd_loop:
 
 dd M_dup
 dd M_literal
@@ -2197,7 +2195,7 @@
 dd M_plus
 
 dd M_doloop
-dd L_C_initialize
+dd L_C_initialize_fd_loop
 dd M_drop
 
 dd MV_Bufferfilenames
@@ -2205,7 +2203,7 @@
 dd M_literal
 dd 0
 dd M_doinit
-L_C_initialize_1:
+L_C_initialize_filename_loop:
 
 dd M_dup
 dd M_literal
@@ -2219,7 +2217,7 @@
 dd M_plus
 
 dd M_doloop
-dd L_C_initialize_1
+dd L_C_initialize_filename_loop
 dd M_drop
 
 dd M_literal
@@ -2299,8 +2297,8 @@
 L_closeparen_filename:
 db "/closeparen"
 
-L137:
-db "unable to restore input" ; comments for testing the awk parser
+L_C_restore_input_error_message:
+db "unable to restore input from the stack, aborting.." ; comments for testing the awk parser
 L_open_failed:
 db "open file failed"
 L_read_failed:
@@ -2307,7 +2305,7 @@
 db "read file failed"
 L170:
 db " Q?"
-L173:
+L_C_stack_underflow_message:
 db " stack underflow"
 L_unknown_interpret_input:
 db " I?"