Make (def) a normal function.

Remove (defe) for now.
This commit is contained in:
Sage Vaillancourt 2022-03-30 16:32:31 -04:00 committed by Sage Vaillancourt
parent 342eba3a0d
commit d76e3f5341
4 changed files with 19 additions and 24 deletions

View File

@ -395,6 +395,7 @@ struct Environment defaultEnv()
}; };
struct symFunc symFuncs[] = { struct symFunc symFuncs[] = {
pf(def),
pf(add), pf(add),
pf(sub), pf(sub),
pf(mul), pf(mul),

View File

@ -15,11 +15,9 @@
(def clock (fn (ti) (cat (hour ti) ":" (zero ti's minute) ":" (zero ti's sec)))) (def clock (fn (ti) (cat (hour ti) ":" (zero ti's minute) ":" (zero ti's sec))))
(def prompt (fn (a) ( (def prompt (fn (a) (
(sys "echo")
(def ti (time)) (def ti (time))
(def m ti's minute) (cat nl
(def s ti's sec) bold red "[sage] " blue (clock) nl
green "pebblisp ~> " reset)
(cat red bold "[sage] " blue (clock) nl green bold "pebblisp ~> " reset)
))) )))

View File

@ -34,17 +34,16 @@
* @param env The environment to add the new definition to * @param env The environment to add the new definition to
* @return The symbol(s) defined * @return The symbol(s) defined
*/ */
Object evalDefArgs(const Object* symbol, const Object* value, Object def(Object* params, int length, struct Environment* env)
struct Environment* env)
{ {
const char* name = symbol->string; const char* name = params[0].string;
Object finalValue = eval(value, env); Object finalValue = eval(&params[1], env);
addToEnv(env, name, finalValue); addToEnv(env, name, finalValue);
cleanObject(&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) struct Environment* env, int* found)
{ {
*found = 1; *found = 1;
if (strcmp(first->string, "def") == 0) { if (strcmp(first->string, "if") == 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) {
return evalIfArgs(rest, env); return evalIfArgs(rest, env);
} else if (strcmp(first->string, "fn") == 0) { } else if (strcmp(first->string, "fn") == 0) {
return evalLambdaArgs(rest, env); return evalLambdaArgs(rest, env);
@ -381,7 +371,7 @@ Result parse(struct Slice* slices)
Result readSeq(struct Slice* tokens) Result readSeq(struct Slice* tokens)
{ {
Object res = listObject(); Object res = listObject();
int isHelp = 0; int forceString = 0;
for (;;) { for (;;) {
struct Slice* next = tokens; struct Slice* next = tokens;
struct Slice* rest = next->text ? &next[1] : NULL; struct Slice* rest = next->text ? &next[1] : NULL;
@ -389,8 +379,9 @@ Result readSeq(struct Slice* tokens)
return (Result) {res, rest}; return (Result) {res, rest};
} }
Result r = parse(tokens); Result r = parse(tokens);
sugar("(? fil) => (? 'fil')", sugar("(? fil) => (? 'fil')" // or,
if (isHelp && r.obj.type == TYPE_SYMBOL) { "(def yee 10) => (def 'yee' 10)",
if (forceString && r.obj.type == TYPE_SYMBOL) {
r.obj.type = TYPE_STRING; r.obj.type = TYPE_STRING;
} }
) )
@ -400,7 +391,7 @@ Result readSeq(struct Slice* tokens)
nf_addToList(&res, cloneObject(r.obj)); nf_addToList(&res, cloneObject(r.obj));
tokens = r.slices; tokens = r.slices;
cleanObject(&r.obj); cleanObject(&r.obj);
isHelp = next->text[0] == '?'; forceString = next->text[0] == '?' || (strncmp(next->text, "def", 3) == 0);
} }
} }

View File

@ -68,4 +68,9 @@ int readFile(const char* filename, struct Environment* env);
#endif /* STANDALONE */ #endif /* STANDALONE */
fn(def, "def",
"Define a variable in the current scope.",
"(def x 10) x", "10",
);
#endif #endif