summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-03-14 14:06:07 -0700
committerKai Stevenson <kai@kaistevenson.com>2023-03-14 14:06:07 -0700
commitf1f7f0744a139ca006b51975b0b998c0fe46af9a (patch)
tree0fe9078045df549ef33949f3d59106d688804174
parentf12c6c6aef0f494068bb2b05ea35da1194ac7b0b (diff)
added input() function
-rw-r--r--doc/precedent.15
-rw-r--r--src/eval.c13
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;
}