Add (eval) function, for evaluating strings

Added a few tests for this new function
This commit is contained in:
Sage Vaillancourt 2020-11-02 15:46:17 -05:00
parent 875228a2b6
commit 963996f3a0
6 changed files with 20 additions and 3 deletions

View File

@ -230,7 +230,7 @@ struct Environment defaultEnv()
{"ap", &append}, {"pre", &prepend}, {"ap", &append}, {"pre", &prepend},
{"at", &at}, {"rest", &rest}, {"rev", &reverse}, {"at", &at}, {"rest", &rest}, {"rev", &reverse},
{"isnum", &isNum}, {"isstr", &isString}, {"isnum", &isNum}, {"isstr", &isString},
{"prn", &print}, {"penv", &printEnvO}, {"prn", &print}, {"penv", &printEnvO}, {"eval", &parseEvalO},
}; };
for(unsigned i = 0; i < sizeof(symFuncs)/sizeof(symFuncs[0]); i++) { for(unsigned i = 0; i < sizeof(symFuncs)/sizeof(symFuncs[0]); i++) {

View File

@ -219,7 +219,8 @@ static const char *errorText[] = {
"ONLY_ONE_ARGUMENT", "ONLY_ONE_ARGUMENT",
"NOT_A_LIST", "NOT_A_LIST",
"SCRIPT_NOT_FOUND", "SCRIPT_NOT_FOUND",
"NO_CLONE_SPECIFIED" "NO_CLONE_SPECIFIED",
"CAN_ONLY_EVAL_STRINGS"
}; };
/** /**

View File

@ -39,7 +39,8 @@ enum errorCode {
ONLY_ONE_ARGUMENT, ONLY_ONE_ARGUMENT,
NOT_A_LIST, NOT_A_LIST,
SCRIPT_NOT_FOUND, SCRIPT_NOT_FOUND,
NO_CLONE_SPECIFIED NO_CLONE_SPECIFIED,
CAN_ONLY_EVAL_STRINGS
}; };
#define MALLOC_FLAG 64 #define MALLOC_FLAG 64

View File

@ -415,6 +415,14 @@ Object printEnvO(Object i1, Object i2, struct Environment *env) {
return numberObject(0); return numberObject(0);
} }
Object parseEvalO(Object text, Object ignore, struct Environment *env)
{
if(text.type != TYPE_STRING) {
return errorObject(CAN_ONLY_EVAL_STRINGS);
}
return parseEval(text.string, env);
}
void copySlice(char * dest, struct Slice *src) void copySlice(char * dest, struct Slice *src)
{ {
if(!dest || !src) if(!dest || !src)

View File

@ -60,5 +60,6 @@ Object isString(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 printEnvO(Object i1, Object i2, struct Environment *env); Object printEnvO(Object i1, Object i2, struct Environment *env);
Object parseEvalO(Object text, Object ignore, struct Environment *env);
#endif #endif

View File

@ -177,6 +177,12 @@ check "BadParens4" ")))hey" "MISMATCHED_PARENS"
check "BadParens5" "hey))(" "MISMATCHED_PARENS" check "BadParens5" "hey))(" "MISMATCHED_PARENS"
endBlock endBlock
title "Eval"
check "BasicNumberEval" "(eval \"5\")" "5"
check "BasicOpEval" "(eval \"(+ 5 10)\")" "15"
check "MapFilter" "(eval \"(fil (< 50) (map sq (1 2 3 4 5 6 7 8 9 10 11 12)))\")" "( 64 81 100 121 144 )"
endBlock
# title "Environment" # title "Environment"
# check "EnvStressTestEarly" "(def a 1)(def b 20)(def c 'yee')(def d 'yeehunnid')(def e 3) (a)" "1" # check "EnvStressTestEarly" "(def a 1)(def b 20)(def c 'yee')(def d 'yeehunnid')(def e 3) (a)" "1"
# check "EnvStressTestLate" "(def a 1)(def b 2)(def c 3)(def d 4)(def e 5)(def f 6)(def n 40) (n)" "40" # check "EnvStressTestLate" "(def a 1)(def b 2)(def c 3)(def d 4)(def e 5)(def f 6)(def n 40) (n)" "40"