Added user input support and many fixes. (0.16.5)
This commit is contained in:
parent
2b82a01fb5
commit
37abd5d6d3
11 changed files with 216 additions and 15 deletions
|
|
@ -114,6 +114,7 @@ typedef enum {
|
|||
|
||||
// OS
|
||||
OP_ENV, // pops name string; pushes value string (or "")
|
||||
OP_INPUT_LINE, // operand: 0=no prompt; 1=has prompt. Pops [prompt?]; pushes input string (no trailing newline)
|
||||
|
||||
// Threads
|
||||
OP_THREAD_SPAWN, // operand: 0=no args, 1=has args; pops [args?], fn; pushes thread id (int>0)
|
||||
|
|
|
|||
13
src/parser.c
13
src/parser.c
|
|
@ -641,6 +641,19 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "input") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
int hasPrompt = 0;
|
||||
skip_spaces(src, len, pos);
|
||||
if (*pos < len && src[*pos] != ')') {
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "input expects 0 or 1 argument"); free(name); return 0; }
|
||||
hasPrompt = 1;
|
||||
}
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after input arg(s)"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_INPUT_LINE, hasPrompt ? 1 : 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "env") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "env expects 1 argument"); free(name); return 0; }
|
||||
|
|
|
|||
1
src/vm.c
1
src/vm.c
|
|
@ -289,6 +289,7 @@ void vm_run(VM *vm, Bytecode *entry) {
|
|||
|
||||
#include "vm/io/read_file.c"
|
||||
#include "vm/io/write_file.c"
|
||||
#include "vm/io/input_line.c"
|
||||
|
||||
#include "vm/logic/and.c"
|
||||
#include "vm/logic/eq.c"
|
||||
|
|
|
|||
2
src/vm.h
2
src/vm.h
|
|
@ -33,7 +33,7 @@ static const char *opcode_names[] = {
|
|||
"ENUMERATE","ZIP",
|
||||
"MIN","MAX","CLAMP","ABS","POW","RANDOM_SEED","RANDOM_INT",
|
||||
"MAKE_MAP","KEYS","VALUES","HAS_KEY",
|
||||
"READ_FILE","WRITE_FILE","ENV",
|
||||
"READ_FILE","WRITE_FILE","ENV","INPUT_LINE",
|
||||
"THREAD_SPAWN","THREAD_JOIN","SLEEP_MS",
|
||||
"BAND","BOR","BXOR","BNOT","SHL","SHR","ROTL","ROTR"
|
||||
};
|
||||
|
|
|
|||
72
src/vm/io/input_line.c
Normal file
72
src/vm/io/input_line.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
case OP_INPUT_LINE: {
|
||||
/* operand: 0 = no prompt; 1 = has prompt (string or any value convertible to string) */
|
||||
int has_prompt = inst.operand ? 1 : 0;
|
||||
if (has_prompt) {
|
||||
/* pop prompt value and print without newline */
|
||||
Value pv = pop_value(vm);
|
||||
char *pstr = value_to_string_alloc(&pv);
|
||||
if (pstr) {
|
||||
fputs(pstr, stdout);
|
||||
fflush(stdout);
|
||||
free(pstr);
|
||||
}
|
||||
free_value(pv);
|
||||
}
|
||||
|
||||
/* read a line from stdin, dynamically grow buffer */
|
||||
size_t cap = 128;
|
||||
size_t len = 0;
|
||||
char *buf = (char*)malloc(cap);
|
||||
if (!buf) {
|
||||
fprintf(stderr, "Runtime error: out of memory reading input");
|
||||
push_value(vm, make_string(""));
|
||||
break;
|
||||
}
|
||||
|
||||
int ch;
|
||||
while ((ch = fgetc(stdin)) != EOF) {
|
||||
if (ch == '\r') {
|
||||
/* Handle CRLF by consuming optional following '\n' */
|
||||
int next = fgetc(stdin);
|
||||
if (next != EOF && next != '\n') {
|
||||
ungetc(next, stdin);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (ch == '\n') {
|
||||
break;
|
||||
}
|
||||
if (len + 1 >= cap) {
|
||||
cap *= 2;
|
||||
char *nb = (char*)realloc(buf, cap);
|
||||
if (!nb) {
|
||||
free(buf);
|
||||
fprintf(stderr, "Runtime error: out of memory reading input");
|
||||
push_value(vm, make_string(""));
|
||||
goto push_done;
|
||||
}
|
||||
buf = nb;
|
||||
}
|
||||
buf[len++] = (char)ch;
|
||||
}
|
||||
|
||||
/* null-terminate */
|
||||
if (len + 1 >= cap) {
|
||||
char *nb = (char*)realloc(buf, len + 1);
|
||||
if (!nb) {
|
||||
free(buf);
|
||||
fprintf(stderr, "Runtime error: out of memory finalizing input");
|
||||
push_value(vm, make_string(""));
|
||||
goto push_done;
|
||||
}
|
||||
buf = nb;
|
||||
}
|
||||
buf[len] = '\0';
|
||||
|
||||
/* push as Fun string */
|
||||
push_value(vm, make_string(buf));
|
||||
free(buf);
|
||||
|
||||
push_done:
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue