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;
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
/// multiple times.
int helpInitialized = 0;
@ -376,6 +374,8 @@ int runTests(int detailed)
return failureCount;
}
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func), _func ## Doc, _func ## Tests, array_length(_func ## Tests))
struct Environment defaultEnv()
{
if (!helpInitialized) {
@ -395,16 +395,16 @@ struct Environment defaultEnv()
};
struct symFunc symFuncs[] = {
{"+", &add},
{"-", &sub},
{"*", &mul},
{"/", &dvi},
{"%", &mod},
{"=", &equ},
{">", &greaterThan},
{"<", &lessThan},
{"&", &and},
{"|", &or},
pf(add),
pf(sub),
pf(mul),
pf(dvi),
pf(mod),
pf(equ),
pf(greaterThan),
pf(lessThan),
pf(and),
pf(or),
pf(catObjects),
pf(filter),
pf(len),

View File

@ -3,30 +3,69 @@
#include "pebblisp.h"
#define BASIC_OP(_name) \
Object _name(Object* params, int length, struct Environment* env)
fn(add, "+",
"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);
BASIC_OP(or);
#undef BASIC_OP
fn(or, "|",
"Check if any conditions are true",
"(| T F)", "T",
"(| F F)", "F",
"(| F F F F F T)", "T",
);
tfn(catObjects, "cat",
({ NULL, isStringy }),
@ -152,9 +191,10 @@ fn(parseEvalO, "eval",
/// STRUCT, STRING => ANY
fn(possessive, "poss",
"Get the value of a struct's field",
"(struct Post (title body))\n "
"(def p (Post \"TI\" \"BO\"))\n "
"p's title => TI"
"p's title", "TI"
);
#ifdef STANDALONE