Add docs/tests to the old BASIC_OP functions.

Also test `poss`.
This commit is contained in:
Sage Vaillancourt 2022-03-29 20:00:57 -04:00
parent 5779ad5427
commit 85d8ee73e7
2 changed files with 70 additions and 30 deletions

View File

@ -215,8 +215,6 @@ struct helpText {
int currentHelp = 0; int currentHelp = 0;
struct helpText helpTexts[100]; struct helpText helpTexts[100];
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func), _func ## Doc, _func ## Tests, array_length(_func ## Tests))
/// For any instances (e.g. segfault recovery) where defaultEnv() may be called /// For any instances (e.g. segfault recovery) where defaultEnv() may be called
/// multiple times. /// multiple times.
int helpInitialized = 0; int helpInitialized = 0;
@ -376,6 +374,8 @@ int runTests(int detailed)
return failureCount; return failureCount;
} }
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func), _func ## Doc, _func ## Tests, array_length(_func ## Tests))
struct Environment defaultEnv() struct Environment defaultEnv()
{ {
if (!helpInitialized) { if (!helpInitialized) {
@ -395,16 +395,16 @@ struct Environment defaultEnv()
}; };
struct symFunc symFuncs[] = { struct symFunc symFuncs[] = {
{"+", &add}, pf(add),
{"-", &sub}, pf(sub),
{"*", &mul}, pf(mul),
{"/", &dvi}, pf(dvi),
{"%", &mod}, pf(mod),
{"=", &equ}, pf(equ),
{">", &greaterThan}, pf(greaterThan),
{"<", &lessThan}, pf(lessThan),
{"&", &and}, pf(and),
{"|", &or}, pf(or),
pf(catObjects), pf(catObjects),
pf(filter), pf(filter),
pf(len), pf(len),

View File

@ -3,30 +3,69 @@
#include "pebblisp.h" #include "pebblisp.h"
#define BASIC_OP(_name) \ fn(add, "+",
Object _name(Object* params, int length, struct Environment* env) "Add numbers",
"(+ 1 2)", "3",
"(+ 1 2 3 4)", "10",
);
BASIC_OP(add); fn(sub, "-",
"Subtract numbers",
"(- 3 2)", "1",
"(- 10 3 1)", "6",
);
BASIC_OP(sub); fn(mul, "*",
"Multiply numbers",
"(* 3 2)", "6",
"(* 1 2 3 4)", "24",
);
BASIC_OP(mul); fn(dvi, "/",
"Divide numbers",
"(/ 8 2)", "4",
"(/ 100 5 2)", "10",
);
BASIC_OP(dvi); fn(mod, "%",
"Get the modulus of two numbers",
"(% 10 3)", "1",
);
BASIC_OP(mod); fn(equ, "=",
"Check if two values are equal",
"(= (+ 2 2) 4)", "T",
"(= \"Hello\" (cat \"He\" \"llo\"))", "T",
"(= (1 2 3) (1 2 3))", "T",
"(= 10 \"10\")", "F",
"(= 2 5)", "F",
);
BASIC_OP(equ); fn(greaterThan, ">",
"Check if one number is greater than another",
"(> 10 3)", "T",
"(> 2 5)", "F",
);
BASIC_OP(greaterThan); fn(lessThan, "<",
"Check if one number is less than another",
"(< 2 5)", "T",
"(< 10 3)", "F",
);
BASIC_OP(lessThan); fn(and, "&",
"Check if all conditions are true",
"(& T T)", "T",
"(& F T)", "F",
"(& T T T T T F)", "F",
);
BASIC_OP(and); fn(or, "|",
"Check if any conditions are true",
BASIC_OP(or); "(| T F)", "T",
"(| F F)", "F",
#undef BASIC_OP "(| F F F F F T)", "T",
);
tfn(catObjects, "cat", tfn(catObjects, "cat",
({ NULL, isStringy }), ({ NULL, isStringy }),
@ -152,9 +191,10 @@ fn(parseEvalO, "eval",
/// STRUCT, STRING => ANY /// STRUCT, STRING => ANY
fn(possessive, "poss", fn(possessive, "poss",
"(struct Post (title body))\n" "Get the value of a struct's field",
"(def p (Post \"TI\" \"BO\"))\n" "(struct Post (title body))\n "
"p's title => TI" "(def p (Post \"TI\" \"BO\"))\n "
"p's title", "TI"
); );
#ifdef STANDALONE #ifdef STANDALONE