From f1f7f0744a139ca006b51975b0b998c0fe46af9a Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Tue, 14 Mar 2023 14:06:07 -0700 Subject: added input() function --- doc/precedent.1 | 5 ++++- src/eval.c | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/precedent.1 b/doc/precedent.1 index c1a5908..ea1a125 100644 --- a/doc/precedent.1 +++ b/doc/precedent.1 @@ -115,7 +115,10 @@ For example, if the first argument to all() is FALSE, it will not evaluate the r \fBI/O\fP \fBprint()\fP -\-\- Evaluates its arguments and writes to the terminal. Writes a newline after all its arguments. +\-\- Evaluates its arguments and writes each to STDOUT. Writes a newline after all its arguments. + +\fBinput()\fP +\-\- Reads from STDIN until the next newline character. Returns everything up to the newline. If it is passed a string, writes it to STDOUT as a prompt. .SH AUTHOR Developed and maintained by Kai Stevenson. diff --git a/src/eval.c b/src/eval.c index f140ed0..7dba60d 100644 --- a/src/eval.c +++ b/src/eval.c @@ -42,7 +42,7 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { } } else { - printf("no matching pair in hashtable\n"); + printf("key %s not found in any scope\n", (char*)symbol->name); } } /* clear new scope, then return */ @@ -334,6 +334,17 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { printf("\n"); return init_sym_int(1); } + else if (hash == get_hash("input")) { + if (symbol->argCount > 0) { + sym_t* v = eval(symbol->args[0], symbolScope); + printf("%s", symtos(v)); + destroy_sym(v); + } + char buffer[4096]; + fgets(buffer, sizeof(buffer), stdin); + buffer[strcspn(buffer, "\n")] = '\0'; + return init_sym_str(buffer); + } else { return NULL; } -- cgit v1.3.1