Add system calls and some char processing.

Color REPL error output.
Add 'clear' shorthand to REPL.
Add basic switch to lib.
This commit is contained in:
Sage Vaillancourt 2021-12-10 16:33:59 -05:00
parent 08432275c8
commit 49bf4aa1a2
6 changed files with 62 additions and 16 deletions

View File

@ -217,13 +217,16 @@ struct Environment defaultEnv()
{"cat", &catObjects}, {"fil", &filter}, {"len", &len}, {"cat", &catObjects}, {"fil", &filter}, {"len", &len},
{"ap", &append}, {"pre", &prepend}, {"ap", &append}, {"pre", &prepend},
{"at", &at}, {"rest", &rest}, {"at", &at}, {"rest", &rest},
{"chat", &charAt},
#ifndef LOW_MEM #ifndef LOW_MEM
{"rev", &reverse}, {"rev", &reverse},
#endif #endif
{"isnum", &isNum}, {"isstr", &isString}, {"iserr", &isErr}, {"isnum", &isNum}, {"isstr", &isString}, {"iserr", &isErr},
{"char", &charVal},
{"eval", &parseEvalO}, {"eval", &parseEvalO},
#ifdef STANDALONE #ifdef STANDALONE
{"prn", &print}, {"pch", &pChar}, {"penv", &printEnvO}, {"prn", &print}, {"pch", &pChar}, {"penv", &printEnvO},
{"sys", &systemCall},
{"loadfile", &loadFile}, {"loadfile", &loadFile},
{"inp", &takeInput}, {"inp", &takeInput},
#endif #endif

View File

@ -25,14 +25,13 @@
; Return the smaller of the two ; Return the smaller of the two
(def min (fn (a b) (if (< a b) a b))) (def min (fn (a b) (if (< a b) a b)))
;(def switch (fn (pair_list) ; Switch expression
; (if (= 0 (len pair_list)) ; Doesn't yet work with lambdas
; "no match" (def switch (fn (val pair_list)
; ( (if (= 0 (len pair_list)) "no match" (
; (def _pair (at 0 pair_list)) (def current (at 0 pair_list))
; (if (at 0 _pair) (if (= val (at 0 current)) (at 1 current) (
; (at 1 _pair) (switch val (rest pair_list))
; (switch (rest pair_list)) ))
; ) ))
; )) ))
;))

View File

@ -3,8 +3,18 @@
(prn "pebblisp::> ") (prn "pebblisp::> ")
(def input (inp)) (def input (inp))
(if (= input "q") () ( (if (= input "q") () (
(prnl (eval input)) ;(switch input (
(repl))) ; ("clear" (fn () (sys "clear")))
;))
(if (= input "") () (
(if (= input "clear") (sys "clear") (
(def ret (eval input))
(if (iserr ret) (prn "X => ") (prn "Ok => "))
(prnl ret)
(prn "")))))
(repl)
))
))) )))
(repl) (repl)

View File

@ -771,14 +771,20 @@ inline enum errorCode getErrorCode(const Object obj)
#ifndef SIMPLE_ERRORS #ifndef SIMPLE_ERRORS
inline void errorAddContext(Object *o, const char* context) inline void errorAddContext(Object *o, const char* context)
{ {
printf("o: %p\n", o);
printf("o->error: %s\n", o->error);
printf("o->error->context: %s\n", o->error->context);
o->error->context = calloc(sizeof(char), RESULT_LENGTH); o->error->context = calloc(sizeof(char), RESULT_LENGTH);
printf("context: %p\n", context);
strncpy(o->error->context, context, RESULT_LENGTH); strncpy(o->error->context, context, RESULT_LENGTH);
} }
inline Object errorWithContext(enum errorCode err, const char* context) inline Object errorWithContext(enum errorCode err, const char* context)
{ {
Object o = errorObject(err); Object o = errorObject(err);
errorAddContext(&o, context); if (context) {
errorAddContext(&o, context);
}
return o; return o;
} }
#endif #endif

View File

@ -215,8 +215,8 @@ Object listEvalFunc(
Object listEvalLambda( Object listEvalLambda(
Object *lambda, Object *lambda,
const Object *remaining, const Object *remaining,
struct Environment *env) struct Environment *env
{ ) {
struct Environment newEnv = envForLambda( struct Environment newEnv = envForLambda(
&lambda->lambda->params, &lambda->lambda->params,
remaining, remaining,
@ -524,12 +524,29 @@ Object isString(Object test, Object ignore, struct Environment *ignore2)
boolObject(1) : boolObject(0); boolObject(1) : boolObject(0);
} }
// Get the int value of a string's first character
Object charVal(Object test, Object ignore, struct Environment *ignore2)
{
return numberObject(test.string[0]);
// return test.type == TYPE_STRING && test.string[0] == '\0' ?
// boolObject(1) : boolObject(0);
}
Object isErr(Object test, Object ignore, struct Environment *ignore2) Object isErr(Object test, Object ignore, struct Environment *ignore2)
{ {
return test.type == TYPE_ERROR ? return test.type == TYPE_ERROR ?
boolObject(1) : boolObject(0); boolObject(1) : boolObject(0);
} }
Object charAt(Object string, Object at, struct Environment *ignore)
{
char* c = malloc(sizeof(char) * 2);
c[0] = string.string[at.number];
c[1] = '\0';
string.string = c;
return string;
}
#ifdef STANDALONE #ifdef STANDALONE
Object print(Object p, Object ignore, struct Environment *env) Object print(Object p, Object ignore, struct Environment *env)
{ {
@ -828,6 +845,13 @@ Object loadFile(Object filename, Object _, struct Environment *env) {
return numberObject(1); return numberObject(1);
} }
Object systemCall(Object process, Object _, struct Environment *env) {
if (isStringy(process)) {
return numberObject(system(process.string));
}
return numberObject(255);
}
void repl(struct Environment *env) { void repl(struct Environment *env) {
if (readFile(SCRIPTDIR "/repl.pbl", env) == 1) { if (readFile(SCRIPTDIR "/repl.pbl", env) == 1) {
fprintf(stderr, "Could not read '%s'\n", SCRIPTDIR "/repl.pbl"); fprintf(stderr, "Could not read '%s'\n", SCRIPTDIR "/repl.pbl");

View File

@ -59,6 +59,9 @@ Object isNum(Object test, Object ignore, struct Environment *ignore2);
Object isString(Object test, Object ignore, struct Environment *ignore2); Object isString(Object test, Object ignore, struct Environment *ignore2);
Object isErr(Object test, Object ignore, struct Environment *ignore2); Object isErr(Object test, Object ignore, struct Environment *ignore2);
Object charAt(Object string, Object at, struct Environment *ignore);
Object charVal(Object test, Object ignore, struct Environment *ignore2);
Object print(Object p, Object ignore, struct Environment *ignore2); Object print(Object p, Object ignore, struct Environment *ignore2);
Object pChar(Object c, Object i1, struct Environment *i2); Object pChar(Object c, Object i1, struct Environment *i2);
Object printEnvO(Object i1, Object i2, struct Environment *env); Object printEnvO(Object i1, Object i2, struct Environment *env);
@ -66,6 +69,7 @@ Object parseEvalO(Object text, Object ignore, struct Environment *env);
#ifdef STANDALONE #ifdef STANDALONE
Object takeInput(Object i1, Object i2, struct Environment *i3); Object takeInput(Object i1, Object i2, struct Environment *i3);
Object systemCall(Object call, Object _, struct Environment *i3);
Object loadFile(Object filename, Object _, struct Environment *env); Object loadFile(Object filename, Object _, struct Environment *env);
#endif #endif