From e0153528a1f434b789008c9e5a4e10a4c6f5cfb5 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 2 Nov 2020 16:30:21 -0500 Subject: [PATCH] Take input in standalone. Print numbers as chars --- src/env.c | 6 +++++- src/pebblisp.c | 18 ++++++++++++++++++ src/pebblisp.h | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/env.c b/src/env.c index 05c950a..38e8602 100644 --- a/src/env.c +++ b/src/env.c @@ -230,7 +230,11 @@ struct Environment defaultEnv() {"ap", &append}, {"pre", &prepend}, {"at", &at}, {"rest", &rest}, {"rev", &reverse}, {"isnum", &isNum}, {"isstr", &isString}, - {"prn", &print}, {"penv", &printEnvO}, {"eval", &parseEvalO}, + {"prn", &print}, {"pch", &pChar}, {"penv", &printEnvO}, + {"eval", &parseEvalO}, + #ifdef STANDALONE + {"inp", &takeInput}, + #endif }; for(unsigned i = 0; i < sizeof(symFuncs)/sizeof(symFuncs[0]); i++) { diff --git a/src/pebblisp.c b/src/pebblisp.c index 94643f8..498069b 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -406,6 +406,15 @@ Object print(Object p, Object ignore, struct Environment *env) return p; } +Object pChar(Object c, Object i1, struct Environment *i2) +{ + if(c.type != TYPE_NUMBER) { + return errorObject(BAD_NUMBER); + } + printf("%c", c.number % 256); + return numberObject(0); +} + Object printEnvO(Object i1, Object i2, struct Environment *env) { while(env->outer) { env = env-> outer; @@ -423,6 +432,15 @@ Object parseEvalO(Object text, Object ignore, struct Environment *env) return parseEval(text.string, env); } +#ifdef STANDALONE +Object takeInput(Object i1, Object i2, struct Environment *i3) +{ + char input[256] = ""; + fgets(input, 256, stdin); + return stringFromSlice(input, strlen(input) - 1); +} +#endif + void copySlice(char * dest, struct Slice *src) { if(!dest || !src) diff --git a/src/pebblisp.h b/src/pebblisp.h index 539a8e7..4e67867 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -59,7 +59,12 @@ Object isNum(Object test, Object ignore, struct Environment *ignore2); Object isString(Object test, Object ignore, struct Environment *ignore2); Object print(Object p, Object ignore, struct Environment *ignore2); +Object pChar(Object c, Object i1, struct Environment *i2); Object printEnvO(Object i1, Object i2, struct Environment *env); Object parseEvalO(Object text, Object ignore, struct Environment *env); +#ifdef STANDALONE +Object takeInput(Object i1, Object i2, struct Environment *i3); +#endif + #endif