This commit is contained in:
Alvin-Zilverstand committed 2026-10-06 23:09:37 +02:00
1 parent 472df35261
commit 96cd1ac97d
21 files changed
+11805 -3954

No files matched your search

+34 -2
View File
@@ -23,8 +23,31 @@ the next read reports EOF. The host terminal supplies line editing and echo.
The low-level `bob_putc(int)` sends the low eight bits to stdout. `bob_getc()`
returns 0..255 or -1 at EOF. `bob_puts()` adds a newline; use print for prompts.
`bob_key()` (trap 8) reads a key: -1 EOF, byte character, or 256/257 up/down,
258/259 left/right, 260/261 home/end, 262 delete. Windows console input uses
unbuffered keys, converts Enter to newline, and treats Ctrl+D as EOF.
Ctrl+Z is an ordinary control character: shell EOF, full-screen editor undo.
Redirected input uses getchar. `bob_terminal()` (trap 9) returns 1 only when
both input/output are native Windows console handles; otherwise 0. Shell
history, completion and editing are guest code in `kernel/input.c`. ANSI key
sequences in redirected regression fixtures exercise the same guest actions.
`bob_columns()` (trap 10) reports the console buffer width, or 80 as fallback.
The shell scrolls a long input line horizontally to keep redraw within it.
## Strings, memory and allocation
The kernel-only `bob_snapshot(int *descriptor, int operation)` is trap 7.
Operation 0 saves; 1 restores. The four-word descriptor contains pointers to
the 192-word names table, then eight-word length, kind and used tables. File
contents occupy the fixed `0xF000..0xFFFF` region. Descriptors can be on the
kernel stack; tables must be disjoint and below `0x9000`. The host validates
metadata, payload checksum and complete file length before committing a restore.
Returns 0 on success, 1 when restored binaries were omitted because the kernel
changed, -1 for storage errors, -2 for invalid data and -3 for invalid arguments.
Invocation by a supervised program faults and restores the shell. The default
host path is `bob-files.b16`; `BOB16_STORAGE` overrides it. See `C_OS.md` for the
user-facing save/restore workflow.
| Function | Contract |
| --- | --- |
| `int strlen(char *text)` | Count words before the zero terminator |
@@ -50,10 +73,19 @@ are 0..7. These routines are internal kernel APIs; callers must check slots.
Failed size/name validation does not overwrite an existing file. Edits start
with the existing file contents in a scratch buffer and commit on :w, :wq or a
single dot line. :q or EOF discards changes since the most recent save. Oversize
single dot line. :q protects unsaved changes; :q! or EOF discards them. Oversize
changes are rejected without modifying the current buffer.
Resident compilation similarly commits output only after successful parsing and
code generation. Storage is volatile and disappears when bob.exe stops.
code generation. RAM contents disappear when bob.exe stops; save snapshots files for explicit restore in another session.
Interactive `edit` uses native `kernel/nano.c`: direct insertion, cursor motion,
Ctrl+O save, Ctrl+X safe exit with Y/N/Cancel, and Ctrl+Z one-change undo/redo.
The legacy colon-command interface is only a redirected/unsupported-terminal
fallback. Both share `0xE000..0xE1FF` text and `0xE200..0xE3FF` undo scratch.
Resident compilation reuses these regions for code/names/tokens/loop chains;
it must not run concurrently with editing. History uses `0xE400..0xE5FF`, with
kernel stack reserved at `0xE600..0xEFFF`. Input buffers are `0x0100..0x01FF`.
`bob_rows()` (trap 11) reports visible console height, with a 25-row fallback.
## Program execution
+5
View File
@@ -7,4 +7,9 @@ void bob_gets(char *buffer, int capacity);
void bob_halt(void);
int bob_getc(void);
int bob_run(int entry);
int bob_snapshot(int *descriptor, int operation);
int bob_key(void);
int bob_terminal(void);
int bob_columns(void);
int bob_rows(void);
#endif
+6 -5
View File
@@ -28,11 +28,12 @@
char *cc_source;
int cc_token;
int cc_value;
char cc_text[64];
#define cc_text ((char *)0xe380)
int cc_error;
int cc_position;
int cc_code[511];
char cc_names[384];
/* Editor and compiler run separately, so they share reserved scratch RAM. */
#define cc_code ((int *)0xe000)
#define cc_names ((char *)0xe200)
int cc_variables;
int cc_error_offset;
int cc_depth;
@@ -171,8 +172,8 @@ int cc_shift_left(int a, int b) { return a << b; }
int cc_shift_right(int a, int b) { return a >> b; }
int cc_truth(int a) { return !!a; }
int cc_loop_depth;
int cc_break_chain[16];
int cc_continue_chain[16];
#define cc_break_chain ((int *)0xe3c0)
#define cc_continue_chain ((int *)0xe3d0)
int cc_precedence(int op) {
if (op == TOKEN_OR) return 1;
+17 -1
View File
@@ -3,6 +3,20 @@ int file_lengths[8];
int file_kinds[8];
int file_used[8];
void file_snapshot(int operation) {
int descriptor[4]; int result;
descriptor[0] = (int)file_names; descriptor[1] = (int)file_lengths;
descriptor[2] = (int)file_kinds; descriptor[3] = (int)file_used;
result = bob_snapshot(descriptor, operation);
if (result == -1) println("Storage unavailable. Check bob-files.b16 location/permissions (or BOB16_STORAGE).");
else if (result < 0) println("Invalid saved files; RAM files unchanged.");
else if (operation == 0) println("Files saved to bob-files.b16 (or BOB16_STORAGE).");
else {
println("Files restored; previous RAM files replaced.");
if (result == 1) println("Kernel changed: old programs omitted. Compile restored C sources again.");
}
}
char *file_name(int slot) { return file_names + slot * NAME_WORDS; }
/* The upper 4096 words are reserved for file contents, above the kernel stack.
Supervised programs cannot write this region. */
@@ -14,9 +28,10 @@ int file_find(char *name) {
return -1;
}
int file_slot(char *name) {
int slot; int length;
int slot; int length; int i;
length = strlen(name);
if (length == 0 || length >= NAME_WORDS) return -1;
for (i = 0; i < length; i++) if (name[i] < 33 || name[i] > 126) return -1;
slot = file_find(name);
if (slot >= 0) return slot;
for (slot = 0; slot < FILE_COUNT; slot++) {
@@ -52,6 +67,7 @@ void file_list(void) {
}
}
if (!count) println("No files.");
print("Used "); print_dec(count); println("/8 slots; limit 511 words per file. delete frees a slot.");
}
void file_manage(char *operation, char *name, char *destination) {
int slot; int target;
+124
View File
@@ -0,0 +1,124 @@
/* Native shell editing; history is stored before command tokenization. */
#define shell_history ((char *)0xe400)
int history_count;
int input_length;
int input_cursor;
int input_echo;
void input_redraw(void) {
int i; int width; int start; int end;
if (!input_echo) return;
width = bob_columns() - 6; if (width < 1) width = 1;
start = 0; if (input_cursor >= width) start = input_cursor - width + 1;
end = start + width; if (end > input_length) end = input_length;
print("\r\033[2Kbob> ");
for (i = start; i < end; i++) bob_putc(command_line[i]);
for (i = input_cursor; i < end; i++) print("\033[D");
}
int input_key(void) {
int key; int next;
key = bob_key();
if (key != 27) return key;
if (input_echo) return 0;
if (bob_key() != '[') return 0;
next = bob_key();
if (next == 'A') return 256;
if (next == 'B') return 257;
if (next == 'D') return 258;
if (next == 'C') return 259;
if (next == 'H') return 260;
if (next == 'F') return 261;
if (next == '3' && bob_key() == '~') return 262;
return 0;
}
int input_matches(char *candidate, int start, int count) {
int i;
for (i = 0; i < count; i++)
if (!candidate[i] || candidate[i] != command_line[start + i]) return 0;
return 1;
}
void input_complete(void) {
int start; int count; int matches; int i; int length; char *words; char candidate[24]; char selected[24];
start = input_cursor;
while (start && command_line[start - 1] != ' ') start--;
count = input_cursor - start; matches = 0;
if (input_cursor != input_length) return;
if (!start) {
words = "help echo clear mem halt list ls dir read write edit load run cc go copy rename delete alloc peek poke save restore";
while (*words) {
length = 0;
while (*words && *words != ' ') candidate[length++] = *words++;
candidate[length] = 0; if (*words) words++;
if (input_matches(candidate, start, count)) {
matches++; memcpy(selected, candidate, length + 1);
if (input_echo) { if (matches == 1) bob_putc('\n'); print(candidate); print(" "); }
}
}
} else {
for (i = 0; i < FILE_COUNT; i++) {
if (file_used[i] && input_matches(file_name(i), start, count)) {
matches++; memcpy(selected, file_name(i), strlen(file_name(i)) + 1);
if (input_echo) { if (matches == 1) bob_putc('\n'); print(selected); print(" "); }
}
}
}
if (matches == 1) {
length = strlen(selected);
if (start + length < 128) {
memcpy(command_line + start, selected, length + 1);
input_length = start + length; input_cursor = input_length;
}
}
if (input_echo && matches) bob_putc('\n');
input_redraw();
}
int shell_read_line(void) {
int key; int i; int truncated; int browsing; char draft[128];
input_length = 0; input_cursor = 0; truncated = 0; browsing = history_count;
input_echo = bob_terminal(); command_line[0] = 0; draft[0] = 0;
while (1) {
key = input_key();
if (key == 26) key = -1;
if (key == -1 || key == '\n') break;
if (key == '\r' || key == 0) continue;
if (key == 256 || key == 257) {
if (browsing == history_count) memcpy(draft, command_line, input_length + 1);
if (key == 256 && browsing) browsing--;
if (key == 257 && browsing < history_count) browsing++;
if (browsing == history_count) memcpy(command_line, draft, strlen(draft) + 1);
else memcpy(command_line, shell_history + browsing * 128, strlen(shell_history + browsing * 128) + 1);
input_length = strlen(command_line); input_cursor = input_length;
} else if (key == 258) { if (input_cursor) input_cursor--; }
else if (key == 259) { if (input_cursor < input_length) input_cursor++; }
else if (key == 260 || key == 1) input_cursor = 0;
else if (key == 261 || key == 5) input_cursor = input_length;
else if (key == 21) { input_length = 0; input_cursor = 0; command_line[0] = 0; }
else if (key == 9) { input_complete(); continue; }
else if (key == 262 || key == '\b' || key == 127) {
if (key != 262 && input_cursor) input_cursor--;
else if (key != 262) continue;
if (input_cursor < input_length) {
for (i = input_cursor; i < input_length; i++) command_line[i] = command_line[i + 1];
input_length--;
}
} else if (key >= 32 && key <= 255) {
if (input_length == 127) truncated = 1;
else {
for (i = input_length; i >= input_cursor; i--) command_line[i + 1] = command_line[i];
command_line[input_cursor++] = key; input_length++;
}
}
input_redraw();
}
if (input_echo) bob_putc('\n');
if (truncated) return -1;
if (key == -1 && !input_length) return -2;
if (input_length && (history_count == 0 || strcmp(command_line, shell_history + (history_count - 1) * 128))) {
if (history_count == 4) {
for (i = 0; i < 384; i++) shell_history[i] = shell_history[i + 128];
history_count = 3;
}
memcpy(shell_history + history_count * 128, command_line, input_length + 1); history_count++;
}
return input_length;
}
+3 -1
View File
@@ -3,6 +3,8 @@
#include "files.c"
#include "compiler.c"
#include "shell.c"
#include "input.c"
#include "nano.c"
int main(void) {
int length; char *sample;
@@ -13,7 +15,7 @@ int main(void) {
file_write("bob.c", sample, strlen(sample), 0);
while (1) {
print("bob> ");
length = read_line(command_line, 128);
length = shell_read_line();
if (length == -2) { bob_putc('\n'); return 0; }
if (length < 0) println("Command too long.");
else shell_command(command_line);
+95
View File
@@ -0,0 +1,95 @@
/* Full-screen, non-modal editor for an interactive Windows console. */
int nano_cursor;
int nano_width;
void nano_text(char *text) {
int i;
for (i = 0; text[i] && i < nano_width - 1; i++) bob_putc(text[i]);
}
void nano_position(int row, int column) {
print("\033["); print_dec(row); bob_putc(';'); print_dec(column); bob_putc('H');
}
int nano_start(int position) {
while (position && edit_buffer[position - 1] != '\n') position--;
return position;
}
int nano_finish(int position) {
while (position < editor_length && edit_buffer[position] != '\n') position++;
return position;
}
void nano_draw(char *name, char *message) {
int rows; int width; int top; int position; int line; int row; int column; int left; int i;
rows = bob_rows(); width = bob_columns();
nano_width = width;
if (rows < 5) rows = 5;
column = nano_cursor - nano_start(nano_cursor); left = 0;
if (column >= width - 1) left = column - width + 2;
top = nano_start(nano_cursor);
for (i = 0; i < rows - 5 && top; i++) top = nano_start(top - 1);
print("\033[?25l\033[2J\033[H"); print("bob edit: "); print(name);
if (editor_changed(name)) print(" *");
position = top; row = 2; line = 2;
while (row < rows - 1) {
nano_position(row, 1); i = 0;
while (position < editor_length && edit_buffer[position] != '\n') {
if (i >= left && i - left < width - 1) {
if (edit_buffer[position] >= 32) bob_putc(edit_buffer[position]);
else bob_putc(' ');
}
i++; position++;
}
if (nano_cursor >= top && nano_cursor <= position) line = row;
if (position >= editor_length) break;
position++; top = position; row++;
}
nano_position(rows - 1, 1); nano_text(message);
nano_position(rows, 1); nano_text("^O Save ^X Exit ^Z Undo Arrows Move");
nano_position(line, column - left + 1); print("\033[?25h");
}
void nano_edit(char *name) {
int key; int start; int end; int column; int i; int temporary; char text[2]; char *message;
nano_cursor = 0; message = "Ctrl+O saves; Ctrl+X exits.";
while (1) {
nano_draw(name, message); key = input_key(); message = "";
if (key == -1) key = 24;
if (key == 24) {
if (!editor_changed(name)) break;
nano_draw(name, "Save? Y=yes N=discard ^C=cancel");
key = input_key();
if (key == 'n' || key == 'N') break;
if ((key == 'y' || key == 'Y') && editor_save(name)) break;
continue;
}
if (key == 15) {
if (editor_save(name)) message = "Saved to RAM; shell save for disk.";
else message = "Save failed; changes kept.";
} else if (key == 26 && editor_has_undo) {
end = editor_length; if (editor_undo_length > end) end = editor_undo_length;
for (i = 0; i <= end; i++) {
temporary = edit_buffer[i]; edit_buffer[i] = edit_undo[i]; edit_undo[i] = temporary;
}
temporary = editor_length; editor_length = editor_undo_length; editor_undo_length = temporary;
if (nano_cursor > editor_length) nano_cursor = editor_length;
message = "Undo/redo applied.";
} else if (key == 258) { if (nano_cursor) nano_cursor--; }
else if (key == 259) { if (nano_cursor < editor_length) nano_cursor++; }
else if (key == 260 || key == 1) nano_cursor = nano_start(nano_cursor);
else if (key == 261 || key == 5) nano_cursor = nano_finish(nano_cursor);
else if (key == 256 || key == 257) {
start = nano_start(nano_cursor); column = nano_cursor - start;
if (key == 256 && start) start = nano_start(start - 1);
if (key == 257) { end = nano_finish(start); if (end < editor_length) start = end + 1; }
end = nano_finish(start); nano_cursor = start + column;
if (nano_cursor > end) nano_cursor = end;
} else {
start = nano_cursor; end = start; text[0] = 0; text[1] = 0;
if (key == '\b' || key == 127) { if (!start) continue; start--; }
else if (key == 262) { if (end == editor_length) continue; end++; }
else if (key == '\n' || key == 9 || (key >= 32 && key < 256)) {
text[0] = key; if (key == 9) text[0] = ' ';
} else continue;
if (editor_replace(start, end, text, 0)) nano_cursor = start + strlen(text);
else message = "Full: 511 chars. Delete text first.";
}
}
print("\033[2J\033[H");
}
+30 -16
View File
@@ -1,12 +1,12 @@
char command_line[128];
char edit_line[128];
char edit_buffer[512];
char edit_undo[512];
#define command_line ((char *)0x100)
#define edit_line ((char *)0x180)
#define edit_buffer ((char *)0xe000)
#define edit_undo ((char *)0xe200)
int editor_length;
int editor_undo_length;
int editor_has_undo;
char *arguments;
int program_length;
void nano_edit(char *name);
char *next_arg(void) {
char *start;
@@ -33,6 +33,9 @@ int parse_number(char *text) {
if (base == 16) {
if (digits == 4) return 0;
number = number * 16 + digit;
} else if (!negative) {
if (number < 0 || number > 6553 || (number == 6553 && digit > 5)) return 0;
number = number * 10 + digit;
} else {
limit = 7; if (negative) limit = 8;
if (number < -3276 || (number == -3276 && digit > limit)) return 0;
@@ -44,7 +47,7 @@ int parse_number(char *text) {
number_ok = 1;
if (base == 16) { if (negative) return -number; return number; }
if (negative) return number;
return -number;
return number;
}
int editor_line(int line) {
int i; int current;
@@ -119,6 +122,7 @@ void edit_file(char *name) {
memcpy(edit_buffer, file_content(slot), editor_length);
}
edit_buffer[editor_length] = 0;
if (bob_terminal()) { nano_edit(name); return; }
println("Editor: text appends; :p lists; :i N TEXT inserts; :r N TEXT replaces.");
println(":d N deletes; :u undo/redo; :w saves; :q quits; :q! discards; . saves/quits.");
println(":p N shows one line. :q protects unsaved edits; :wq also saves/quits.");
@@ -198,7 +202,6 @@ void run_file(char *name) {
program = (int *)PROGRAM_BASE;
memset(program, 0, PROGRAM_WORDS);
memcpy(program, file_content(slot), file_lengths[slot]);
program_length = file_lengths[slot];
status = bob_run(PROGRAM_BASE);
if (status == -2) println("Program fault; shell restored.");
else if (status == -3) println("Program timed out; shell restored.");
@@ -208,16 +211,19 @@ void memory_status(void) {
print("Heap "); print_hex(HEAP_BASE); print(".."); print_hex(0xdfff);
print("; free "); print_dec(HEAP_WORDS - heap_used); println(" words");
print("Programs "); print_hex(PROGRAM_BASE); print(".."); print_hex(0xbfff); println("");
println("Files: 8 slots, 511 words each; stack above 0xE000.");
println("Files: 8 slots, 511 words each; stack above 0xE600.");
}
void command_help(char *name) {
if (strcmp(name, "edit") == 0) println("edit NAME: open text; ordinary lines append. :p or :p N views; :i N TEXT inserts; :r N TEXT replaces; :d N deletes; :u undo/redo; :w saves; :q quits safely; :q! discards; . saves/quits.");
if (strcmp(name, "save") == 0 || strcmp(name, "restore") == 0) {
println("save: snapshot all files to bob-files.b16 in the working directory (or BOB16_STORAGE). restore yes: replace RAM files with that snapshot. Save current work first. Changed kernels restore text only; recompile programs."); return;
}
if (strcmp(name, "edit") == 0) println("edit NAME: type/arrows; Ctrl+O saves, Ctrl+X exits, Ctrl+Z undo/redo. Unsaved exit asks Y/N. Redirected input uses the line editor; see C_OS.md.");
else if (strcmp(name, "cc") == 0 || strcmp(name, "go") == 0)
println("cc SOURCE [OUTPUT]: compile C; output defaults to app. go SOURCE [OUTPUT] also runs on success. Example: go bob.c. One int main(void); no includes. Use edit SOURCE to fix errors.");
else if (strcmp(name, "write") == 0) println("write NAME TEXT: create/replace a text file. Example: write note bob! Names: 1..23 characters without spaces; 8 file slots, 511 words each. Use edit for multiple lines.");
else if (strcmp(name, "read") == 0) println("read NAME: print text. Example: read bob.c. Use ls to see names; run executes program files.");
else if (strcmp(name, "ls") == 0 || strcmp(name, "dir") == 0 || strcmp(name, "list") == 0)
println("ls / dir / list: show names, lengths and text/program kinds. Files are in RAM and lost on exit.");
println("ls / dir / list: show names, lengths and text/program kinds. Files are in RAM; save keeps them between sessions.");
else if (strcmp(name, "copy") == 0 || strcmp(name, "rename") == 0 || strcmp(name, "delete") == 0)
println("copy OLD NEW | rename OLD NEW | delete NAME. Example: copy bob.c backup.c. Destinations must be new. Copy needs a free slot; rename does not. Delete is permanent.");
else if (strcmp(name, "run") == 0) println("run NAME: execute a compiled/loaded program. Example: cc bob.c bob, then run bob. Exit shows return value; faults/timeouts restore the shell.");
@@ -228,7 +234,7 @@ void command_help(char *name) {
println("alloc WORDS: allocate/zero 1..8192 heap words, print start address; no free. Example: alloc 16. mem shows remaining heap and memory regions.");
else if (strcmp(name, "echo") == 0) println("echo TEXT: print text. Example: echo bob!");
else if (strcmp(name, "clear") == 0) println("clear: clear an ANSI-capable terminal; does not remove files.");
else if (strcmp(name, "halt") == 0) println("halt: stop emulator. RAM files and allocations are lost; read important files before quitting.");
else if (strcmp(name, "halt") == 0) println("halt: stop emulator. Unsaved RAM files and allocations are lost; use save before quitting.");
else if (strcmp(name, "help") == 0) println("help: list commands. help COMMAND: show usage and an example. Try help edit or help go.");
else println("No help for that command. Type help for available names.");
}
@@ -247,6 +253,14 @@ void shell_command(char *line) {
println("cc/go default output: app. Try go bob.c to compile and print bob!");
println("Use hex addresses. poke is limited to heap RAM.");
println("Type help COMMAND for usage/examples. Try help edit or help go.");
println("save | restore yes (replaces RAM files); help save for storage details.");
println("Shell keys: Up/Down history; Tab completion; Ctrl+U clears (Windows console).");
} else if (strcmp(command, "save") == 0) {
if (*next_arg()) println("Usage: save (all RAM files)");
else file_snapshot(0);
} else if (strcmp(command, "restore") == 0) {
if (strcmp(next_arg(), "yes") || *next_arg()) println("restore yes replaces all RAM files. Use save first to keep current work.");
else file_snapshot(1);
} else if (strcmp(command, "echo") == 0) println(remaining_args());
else if (strcmp(command, "clear") == 0) print("\033[2J\033[H");
else if (strcmp(command, "mem") == 0) memory_status();
@@ -265,7 +279,7 @@ void shell_command(char *line) {
else println(file_content(value));
} else if (strcmp(command, "write") == 0) {
name = next_arg(); text = remaining_args();
if (file_write(name, text, strlen(text), 0) < 0) println("Cannot write file.");
if (file_write(name, text, strlen(text), 0) < 0) println("Cannot write file. Names: 1..23 printable characters; 8 slots. Use ls or help write.");
else println("Saved.");
} else if (strcmp(command, "edit") == 0) edit_file(next_arg());
else if (strcmp(command, "load") == 0) { name = next_arg(); load_words(name); }
@@ -278,17 +292,17 @@ void shell_command(char *line) {
if (value && strcmp(command, "go") == 0) run_file(text);
} else if (strcmp(command, "alloc") == 0) {
value = parse_number(next_arg());
if (!number_ok) { println("Invalid allocation size."); return; }
if (!number_ok) { println("Invalid allocation size. Use 1..8192 words; mem shows available space."); return; }
pointer = alloc(value);
if (!pointer) println("Allocation failed.");
if (!pointer) println("Allocation failed. Use a positive size within free space; see mem. No free command.");
else { print_hex((int)pointer); bob_putc('\n'); }
} else if (strcmp(command, "peek") == 0 || strcmp(command, "poke") == 0) {
address = parse_number(next_arg());
if (!number_ok) { println("Invalid address."); return; }
if (!number_ok) { println("Invalid address. Use hex 0x0000..0xFFFF or decimal 0..65535."); return; }
pointer = (int *)address;
if (strcmp(command, "poke") == 0) {
value = parse_number(next_arg());
if (!number_ok) { println("Invalid value."); return; }
if (!number_ok) { println("Invalid value. Use -32768..65535 or 0x0000..0xFFFF."); return; }
if (address >= 0 || address < -16384 || address >= -8192) {
println("poke is limited to 0xC000..0xDFFF."); return;
}