From d76e3f534108558ac6f700450979ec8d4505816f Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 30 Mar 2022 16:32:31 -0400 Subject: [PATCH] Make (def) a normal function. Remove (defe) for now. --- src/env.c | 1 + src/examples/pebblisp.pbl | 8 +++----- src/pebblisp.c | 29 ++++++++++------------------- src/pebblisp.h | 5 +++++ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/env.c b/src/env.c index 1ad2e12..6522d9d 100644 --- a/src/env.c +++ b/src/env.c @@ -395,6 +395,7 @@ struct Environment defaultEnv() }; struct symFunc symFuncs[] = { + pf(def), pf(add), pf(sub), pf(mul), diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 9366aea..d7a3b3b 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -15,11 +15,9 @@ (def clock (fn (ti) (cat (hour ti) ":" (zero ti's minute) ":" (zero ti's sec)))) (def prompt (fn (a) ( - (sys "echo") (def ti (time)) - (def m ti's minute) - (def s ti's sec) - - (cat red bold "[sage] " blue (clock) nl green bold "pebblisp ~> " reset) + (cat nl + bold red "[sage] " blue (clock) nl + green "pebblisp ~> " reset) ))) diff --git a/src/pebblisp.c b/src/pebblisp.c index d02d759..9360e1e 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -34,17 +34,16 @@ * @param env The environment to add the new definition to * @return The symbol(s) defined */ -Object evalDefArgs(const Object* symbol, const Object* value, - struct Environment* env) +Object def(Object* params, int length, struct Environment* env) { - const char* name = symbol->string; + const char* name = params[0].string; - Object finalValue = eval(value, env); + Object finalValue = eval(¶ms[1], env); addToEnv(env, name, finalValue); cleanObject(&finalValue); - return cloneObject(*symbol); + return cloneObject(params[0]); } /** @@ -134,16 +133,7 @@ Object evalBuiltIns(const Object* first, const Object* rest, struct Environment* env, int* found) { *found = 1; - if (strcmp(first->string, "def") == 0) { - return evalDefArgs(rest, rest->forward, env); -#ifndef LOW_MEM - } else if (strcmp(first->string, "defe") == 0) { - Object symbol = eval(rest, env); - Object e = evalDefArgs(&symbol, rest->forward, env); - cleanObject(&symbol); - return e; -#endif - } else if (strcmp(first->string, "if") == 0) { + if (strcmp(first->string, "if") == 0) { return evalIfArgs(rest, env); } else if (strcmp(first->string, "fn") == 0) { return evalLambdaArgs(rest, env); @@ -381,7 +371,7 @@ Result parse(struct Slice* slices) Result readSeq(struct Slice* tokens) { Object res = listObject(); - int isHelp = 0; + int forceString = 0; for (;;) { struct Slice* next = tokens; struct Slice* rest = next->text ? &next[1] : NULL; @@ -389,8 +379,9 @@ Result readSeq(struct Slice* tokens) return (Result) {res, rest}; } Result r = parse(tokens); - sugar("(? fil) => (? 'fil')", - if (isHelp && r.obj.type == TYPE_SYMBOL) { + sugar("(? fil) => (? 'fil')" // or, + "(def yee 10) => (def 'yee' 10)", + if (forceString && r.obj.type == TYPE_SYMBOL) { r.obj.type = TYPE_STRING; } ) @@ -400,7 +391,7 @@ Result readSeq(struct Slice* tokens) nf_addToList(&res, cloneObject(r.obj)); tokens = r.slices; cleanObject(&r.obj); - isHelp = next->text[0] == '?'; + forceString = next->text[0] == '?' || (strncmp(next->text, "def", 3) == 0); } } diff --git a/src/pebblisp.h b/src/pebblisp.h index 4bb4ac7..29eb53f 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -68,4 +68,9 @@ int readFile(const char* filename, struct Environment* env); #endif /* STANDALONE */ +fn(def, "def", + "Define a variable in the current scope.", + "(def x 10) x", "10", +); + #endif \ No newline at end of file