Add a place to give the machine its input
INP read straight from stdin with scanf, which the live view cannot offer: the
view owns the terminal, and a tape that wants input either ate a number nobody
typed or blocked on a pipe.
INP now takes its value from, in order:
1. a queue filled by --input "7,5" (spaces or commas, scripts and tests)
2. a hook the live view installs: it prints 'input> ' under the frame and
reads a line, redraws on the next tick, and feeds 0 when stdin is done so
a tape behind a pipe still finishes
3. stdin, exactly as before
So nothing changes for a plain run, and --live can drive programs that need
input. tapes/echo.rot8 (just ',.') is the smallest tape that exercises it.
rot8_test --input 42 tapes/echo.rot8 -> 42
rot8_test --live tapes/echo.rot8 -> asks under the view
Tests: --input with one value, with a list, a live run fed through a pipe
(prints 5), and a live run with stdin already closed (feeds 0, terminates).
The --dump path and the untouched tapes (and, or, full_adder, wave) still work.
This commit is contained in:
1 parent
bda173b078
commit
6475938e6c
7 files changed
+129
-6
No files matched your search
@@ -92,6 +92,7 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
| `-t`, `--tty` / `--no-tty` | 强制当成/不当成终端(见下) |
|
||||
| `-k`, `--compact` / `--full` | 强制精简画面/完整画面 |
|
||||
| `-c`, `--color` / `--no-color` | 强制开/关 ANSI 颜色(默认跟随终端) |
|
||||
| `-i`, `--input <v...>` | 预先喂给 `INP` 的值(空格或逗号分隔) |
|
||||
| `-h`, `--help` | 帮助 |
|
||||
|
||||
不加 `--live` 与 `--dump` 时行为与以前完全一致:直接跑完,只留下 `OUT` 的输出.\
|
||||
@@ -102,6 +103,20 @@ ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各
|
||||
可以强制完整版. 用 `--tty` 之前那套原地重绘才生效,所以**如果你的终端其实是通过管道接进来的**
|
||||
(网页版终端、某些 IDE 的面板,程序 `_isatty` 看不出来),画面会变成一帧帧往下滚 —— 加个 `-t` 就好了.
|
||||
|
||||
## 输入
|
||||
|
||||
`INP` 现在按这个顺序取值:先吃 `--input` 排好的队列,再问现场(live 模式),最后才回落到
|
||||
stdin —— 所以不带 `--input`、也不开 `--live` 时行为与以前完全一样.
|
||||
|
||||
```sh
|
||||
rot8_test --input 7,5 tapes/full_adder.rot8 # 脚本/测试:一次喂完
|
||||
rot8_test --live tapes/echo.rot8 # 现场:画面下方问 input>
|
||||
```
|
||||
|
||||
live 模式下如果队列空了,画面底下会出现 `input> ` 提示,敲一个数回车就继续;stdin 已经关掉
|
||||
(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是拿来试这个的
|
||||
最小例子.
|
||||
|
||||
## 构建与测试
|
||||
|
||||
### 构建
|
||||
|
||||
@@ -10,6 +10,15 @@ void rot8_inp_io(rot8_iter *it);
|
||||
void rot8_io_set_quiet(bool quiet);
|
||||
bool rot8_io_quiet(void);
|
||||
|
||||
/* INP takes its value from the queue pushed here first, then from the hook,
|
||||
* then from stdin. The queue is for scripts and tests (--input 1 2 3); the
|
||||
* hook is for the live view, which has to stop and ask the person watching. */
|
||||
typedef bool (*rot8_input_fn)(void *ctx, rot8_data *value);
|
||||
|
||||
void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx);
|
||||
void rot8_io_push_input(rot8_data value);
|
||||
size_t rot8_io_pending_input(void);
|
||||
|
||||
/* Reads the instructions out of a tape into tape[0..size-1]. Returns false
|
||||
* when the file cannot be opened; the tape is always NUL terminated. */
|
||||
bool read_tape(const char *filename, char *tape, size_t size);
|
||||
|
||||
@@ -39,6 +39,10 @@ void rot8_viz_free(rot8_viz *viz);
|
||||
* Worth telling the user about when it is false but live was asked for. */
|
||||
bool rot8_viz_terminal(const rot8_viz *viz);
|
||||
|
||||
/* Asks for one value below the view: the INP hook while a tape is running live.
|
||||
* Pass the rot8_viz as ctx. Returns false when stdin is exhausted. */
|
||||
bool rot8_viz_ask(void *ctx, rot8_data *value);
|
||||
|
||||
/* rot8_hook that drives the view; pass the rot8_viz as ctx. */
|
||||
void rot8_viz_hook(rot8_iter *iter, void *ctx);
|
||||
|
||||
|
||||
+43
-4
@@ -1,16 +1,46 @@
|
||||
#include "rot8/rot8_io.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "rot8/rot8.h"
|
||||
|
||||
static bool io_quiet = false;
|
||||
|
||||
/* INP values handed over before the run (scripts, tests) or asked for on the
|
||||
* fly (the live view). Small ring buffer: a tape that needs more than this
|
||||
* still works, it just goes back to stdin. */
|
||||
#define INPUT_QUEUE_MAX 256
|
||||
|
||||
static rot8_data input_queue[INPUT_QUEUE_MAX];
|
||||
static size_t input_head = 0;
|
||||
static size_t input_tail = 0;
|
||||
static rot8_input_fn input_hook = NULL;
|
||||
static void *input_hook_ctx = NULL;
|
||||
|
||||
void rot8_io_set_quiet(bool quiet) { io_quiet = quiet; }
|
||||
|
||||
bool rot8_io_quiet(void) { return (io_quiet); }
|
||||
|
||||
void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx) {
|
||||
input_hook = fn;
|
||||
input_hook_ctx = ctx;
|
||||
}
|
||||
|
||||
void rot8_io_push_input(rot8_data value) {
|
||||
size_t next = (input_tail + 1) % INPUT_QUEUE_MAX;
|
||||
if (next == input_head) {
|
||||
return; /* full, drop it */
|
||||
}
|
||||
input_queue[input_tail] = value;
|
||||
input_tail = next;
|
||||
}
|
||||
|
||||
size_t rot8_io_pending_input(void) {
|
||||
return ((input_tail + INPUT_QUEUE_MAX - input_head) % INPUT_QUEUE_MAX);
|
||||
}
|
||||
|
||||
void rot8_out_io(rot8_iter *it) {
|
||||
// putchar(out(it));
|
||||
if (!io_quiet) {
|
||||
@@ -19,11 +49,20 @@ void rot8_out_io(rot8_iter *it) {
|
||||
}
|
||||
|
||||
void rot8_inp_io(rot8_iter *it) {
|
||||
int n;
|
||||
if (scanf("%d", &n) != 1) {
|
||||
n = 0;
|
||||
rot8_data value = 0;
|
||||
if (input_head != input_tail) {
|
||||
value = input_queue[input_head];
|
||||
input_head = (input_head + 1) % INPUT_QUEUE_MAX;
|
||||
} else if (input_hook != NULL && input_hook(input_hook_ctx, &value)) {
|
||||
/* the hook filled value in */
|
||||
} else {
|
||||
int n;
|
||||
if (scanf("%d", &n) != 1) {
|
||||
n = 0;
|
||||
}
|
||||
value = (rot8_data)n;
|
||||
}
|
||||
rot8_inp(it, (rot8_data)n);
|
||||
rot8_inp(it, value);
|
||||
}
|
||||
|
||||
bool read_tape(const char *filename, char *tape, size_t size) {
|
||||
|
||||
@@ -574,6 +574,35 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* input
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
/* Called from rot8_inp_io while a tape runs under the live view: the person
|
||||
* watching gets a prompt under the frame instead of the program swallowing a
|
||||
* number off the terminal (or blocking on a pipe). */
|
||||
bool rot8_viz_ask(void *ctx, rot8_data *value) {
|
||||
rot8_viz *viz = (rot8_viz *)ctx;
|
||||
char line[64];
|
||||
if (viz == NULL || value == NULL) {
|
||||
return (false);
|
||||
}
|
||||
fprintf(viz->out, "\ninput> ");
|
||||
fflush(viz->out);
|
||||
if (fgets(line, sizeof line, stdin) == NULL) {
|
||||
fputc('\n', viz->out);
|
||||
fflush(viz->out);
|
||||
return (false);
|
||||
}
|
||||
char *end = NULL;
|
||||
long parsed = strtol(line, &end, 0);
|
||||
if (end == line) { /* nothing usable typed: feed 0 and keep going */
|
||||
*value = 0;
|
||||
return (true);
|
||||
}
|
||||
*value = (rot8_data)parsed;
|
||||
return (true);
|
||||
}
|
||||
size_t rot8_viz_output_count(const rot8_viz *viz) {
|
||||
return (viz != NULL ? viz->output_count : 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
,.
|
||||
+28
-2
@@ -38,12 +38,15 @@ static void usage(const char *program) {
|
||||
" --no-tty never draw in place, just print frames\n"
|
||||
" -k, --compact short frame: the tape, without the cell inspector\n"
|
||||
" --full always draw the full frame\n"
|
||||
" -i, --input <v...> feed INP values up front (spaces or commas)\n"
|
||||
" anything left over is asked for on the terminal\n"
|
||||
" -h, --help show this text\n"
|
||||
"\n"
|
||||
"examples:\n"
|
||||
" %s --live tapes/full_adder.rot8\n"
|
||||
" %s --dump tapes/and.rot8\n",
|
||||
program, program, program);
|
||||
" %s --dump tapes/and.rot8\n"
|
||||
" %s --live --input 7,5 tapes/full_adder.rot8\n",
|
||||
program, program, program, program);
|
||||
}
|
||||
|
||||
static bool parse_uint(const char *text, unsigned int *out) {
|
||||
@@ -60,6 +63,20 @@ static bool parse_uint(const char *text, unsigned int *out) {
|
||||
return (true);
|
||||
}
|
||||
|
||||
/* "1 2 3" or "1,2,3" or "1, 2, 3": every number becomes one INP value. */
|
||||
static void push_input_list(const char *text) {
|
||||
while (*text != '\0') {
|
||||
char *end = NULL;
|
||||
long value = strtol(text, &end, 0);
|
||||
if (end == text) { /* not a number: step over the separator */
|
||||
text++;
|
||||
continue;
|
||||
}
|
||||
rot8_io_push_input((rot8_data)value);
|
||||
text = end;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *program = base_name(argv[0]);
|
||||
const char *path = NULL;
|
||||
@@ -80,6 +97,12 @@ int main(int argc, char **argv) {
|
||||
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
|
||||
usage(program);
|
||||
return (0);
|
||||
} else if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "%s: --input needs a value or a list\n", program);
|
||||
return (2);
|
||||
}
|
||||
push_input_list(argv[++i]);
|
||||
} else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) {
|
||||
opts.live = true;
|
||||
} else if (strcmp(arg, "-d") == 0 || strcmp(arg, "--dump") == 0) {
|
||||
@@ -150,8 +173,11 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (opts.live) {
|
||||
rot8_io_set_quiet(true);
|
||||
/* whatever --input did not cover gets asked for under the frame */
|
||||
rot8_io_set_input_hook(rot8_viz_ask, viz);
|
||||
}
|
||||
rot8_run_hooked(ins, (rot8_ins_size)length, rot8_viz_hook, viz);
|
||||
rot8_io_set_input_hook(NULL, NULL);
|
||||
rot8_io_set_quiet(false);
|
||||
|
||||
rot8_viz_free(viz);
|
||||
|
||||
Reference in new issue
Block a user