@@ -64,61 +64,88 @@ void vfs_print_current_tree(vfs *fs);
6464
6565static zf_ctx * g_ctx = 0 ;
6666
67+ const char * zf_result_strings [] = {"OK" ,
68+ "ABORT: internal error" ,
69+ "ABORT: outside memory bounds" ,
70+ "ABORT: data stack underrun" ,
71+ "ABORT: data stack overrun" ,
72+ "ABORT: return stack underrun" ,
73+ "ABORT: return stack overrun" ,
74+ "ABORT: not a word" ,
75+ "ABORT: compile-only word" ,
76+ "ABORT: invalid size" ,
77+ "ABORT: division by zero" ,
78+ "ABORT: invalid user variable" ,
79+ "ABORT: external error" };
80+
81+ const char * zf_result_to_string (zf_result result ) {
82+ if (result >= 0 &&
83+ result < sizeof (zf_result_strings ) / sizeof (zf_result_strings [0 ])) {
84+ return zf_result_strings [result ];
85+ }
86+ return "Unknown error" ;
87+ }
88+
6789static void enter (const char * str ) {
6890 if (g_ctx ) {
6991 zf_result result = zf_eval (g_ctx , str );
7092
7193 if (result == ZF_OK ) {
7294 kernel_printf (" ok\n" );
7395 } else {
74- kernel_printf (" error %d \n" , result );
96+ kernel_printf (" error %s \n" , zf_result_to_string ( result ) );
7597 }
7698
7799 kernel_printf ("> " );
78100 }
79101}
80102
81103static void bootstrap_zforth (zf_ctx * ctx , vfs * unified_vfs ) {
82- const char * forth_files [] = {"/fd/forth/dict.f" , "/fd/forth/core.f" , NULL };
83-
84- uint8_t buffer [1024 ] = {0 };
104+ const char * forth_files [] = {"/fd/forth/core.f" , "/fd/forth/dict.f" , NULL };
85105
106+ uint8_t buffer [RAMDISK_MAX_FILESIZE ] = {0 };
86107 for (int file_idx = 0 ; forth_files [file_idx ] != NULL ; file_idx ++ ) {
87108 const char * filepath = forth_files [file_idx ];
88-
109+ vfs_stat stat = {0 };
110+ if (VFS_SUCCESS != unified_vfs -> stat (unified_vfs , filepath , & stat )) {
111+ kernel_printf ("error: file not found: %s\n" , filepath );
112+ continue ;
113+ }
114+ if (RAMDISK_MAX_FILESIZE < stat .size ) {
115+ kernel_printf ("error: file is over 64k: %s\n" , filepath );
116+ }
89117 int fd = 0 ;
90118 if (VFS_SUCCESS !=
91119 unified_vfs -> open (unified_vfs , filepath , VFS_READ , & fd )) {
92- kernel_printf ("file not found : %s\n" , filepath );
93- continue ; // skip this file, try next one
120+ kernel_printf ("error: could not open : %s\n" , filepath );
121+ continue ;
94122 }
95123
96124 memset (buffer , 0 , sizeof (buffer ));
97-
98125 int bytes_read = 0 ;
99126 int ret = unified_vfs -> read (unified_vfs , fd , buffer , sizeof (buffer ) - 1 ,
100127 buffer , & bytes_read );
101128
102129 if (ret != VFS_SUCCESS ) {
103- kernel_printf ("error %d reading %s\n" , ret , filepath );
104130 unified_vfs -> close (unified_vfs , fd );
105131 continue ;
106132 }
107133
108- if (bytes_read > 0 ) {
109- buffer [bytes_read ] = '\0' ;
134+ if (bytes_read <= 0 ) {
135+ unified_vfs -> close (unified_vfs , fd );
136+ continue ;
110137 }
111138
139+ buffer [bytes_read ] = '\0' ;
112140 unified_vfs -> close (unified_vfs , fd );
113141
114142 zf_result result = zf_eval (ctx , (char * )buffer );
115143 if (result != ZF_OK ) {
116- kernel_printf ("zforth error %d in file: %s\n" , result , filepath );
117- } else {
144+ kernel_printf (" error %s\n" , zf_result_to_string (result ));
118145 }
119146 }
120147
121- kernel_printf ("- zforth initialization complete\n" );
148+ kernel_printf ("- zForth initialization complete\n" );
122149}
123150
124151void kernel_main (void ) {
@@ -151,10 +178,10 @@ void kernel_main(void) {
151178 vfs_print_current_tree (unified_vfs );
152179
153180 zf_ctx ctx ;
154- zf_init (& ctx , 1 );
181+ zf_init (& ctx , 0 );
155182 zf_bootstrap (& ctx );
156183 bootstrap_zforth (& ctx , unified_vfs );
157-
184+ serial_debug ( "zf inited." );
158185 keyboard_ctx_t * kb = get_kb_ctx ();
159186 kb -> enter_handler = enter ;
160187 g_ctx = & ctx ;
0 commit comments