Add a simple (doc-test) function.

Switch from single docString to docStrings array.
Fix (split) substringing.
This commit is contained in:
Sage Vaillancourt 2022-04-25 14:40:27 -04:00 committed by Sage Vaillancourt
parent bac1790b6c
commit 08bf35940d
3 changed files with 33 additions and 13 deletions

View File

@ -57,11 +57,28 @@
(def split (fn (txt splitter) (def split (fn (txt splitter)
"Split a string at each instance of splitter" ( "Split a string at each instance of splitter" (
(def i (indexOf txt splitter)) (def i (indexOf txt splitter))
(def split-len (slen splitter))
(if (< i 0) (txt) (if (< i 0) (txt)
(pre (split (substr (+ 1 i) 9999999 txt) splitter) (substr 0 i txt)) (pre (split (substr (+ split-len i) 9999999 txt) splitter) (substr 0 i txt))
) )
))) )))
(def not (fn (bool) (if bool F T)))
(def _doc-test (fn (docstring) (
(def halves (split docstring " => "))
(if (not (= 2 (len halves))) () (
(def '(test expected) halves)
(def actual (string (eval test)))
(if (= actual expected) (prnl "Test passed!") (prnl "Test failed! Expected " expected " but received " actual)))
)
""
)))
(def doc-test (fn (docstring) (
(for-each _doc-test (split docstring nl))
)))
(def loadfile (fn (file-name) (def loadfile (fn (file-name)
"Read and immediately evaluate the file with the given path." ( "Read and immediately evaluate the file with the given path." (
(eval (rf file-name)) (eval (rf file-name))
@ -95,7 +112,7 @@
(def max (fn (...values) (def max (fn (...values)
"Return the largest of the given values" "Return the largest of the given values"
"(max 1 5 3) => 3" "(max 1 5 3) => 5"
(reduce values _max (first values)) (reduce values _max (first values))
)) ))

View File

@ -282,10 +282,12 @@ int stringNObj(struct string* s, const Object* obj)
break; break;
case TYPE_LAMBDA: { case TYPE_LAMBDA: {
#if defined(STANDALONE) && !defined(DEBUG) #if defined(STANDALONE) && !defined(DEBUG)
const char* docString = lambdaDocs(obj); char** docStrings = lambdaDocs(obj);
if (docString) { if (docStrings) {
inflate(s, strlen(docString)); for (; *docStrings; docStrings++) {
appendf(s, "%s\n", docString); inflate(s, strlen(*docStrings));
appendf(s, "%s\n", *docStrings);
}
} }
stringNObj(s, &obj->lambda->params); stringNObj(s, &obj->lambda->params);
appendf(s, " -> "); appendf(s, " -> ");
@ -732,7 +734,7 @@ inline Object constructLambda(const Object* params, const Object* body, struct E
const Object* docs = body; const Object* docs = body;
size_t docLength = 0; size_t docLength = 0;
while (body->type == TYPE_STRING) { while (body->type == TYPE_STRING) {
docLength += sizeof(char) * (strlen(body->string) + 1); docLength++;
body = body->forward; body = body->forward;
} }
@ -749,13 +751,14 @@ inline Object constructLambda(const Object* params, const Object* body, struct E
if (docLength == 0) { if (docLength == 0) {
lambdaDocs(&o) = NULL; lambdaDocs(&o) = NULL;
} else { } else {
lambdaDocs(&o) = malloc(docLength + 1); lambdaDocs(&o) = malloc(sizeof(char*) * (docLength + 1));
char* docText = lambdaDocs(&o); char** docTexts = lambdaDocs(&o);
while (docs->type == TYPE_STRING) { while (docs->type == TYPE_STRING) {
const char* data = docs->string; *docTexts = strdup(docs->string);
docText += sprintf(docText, "%s\n", data);
docs = docs->forward; docs = docs->forward;
docTexts++;
} }
*docTexts = NULL;
} }
Object* dest = &o.lambda->body; Object* dest = &o.lambda->body;

View File

@ -158,7 +158,7 @@ struct Object {
union { union {
Object* forward; Object* forward;
char* docString; char** docStrings;
int refs; int refs;
}; };
}; };
@ -180,7 +180,7 @@ struct StructObject {
struct Object* fields; // Order should match that in the definition. struct Object* fields; // Order should match that in the definition.
}; };
#define lambdaDocs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->params.docString #define lambdaDocs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->params.docStrings
#define lambdaRefs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->body.refs #define lambdaRefs(LAMBDA_OBJECT) (LAMBDA_OBJECT)->lambda->body.refs
struct Lambda { struct Lambda {
/// params.docstring can contain details about the constructed lambda. /// params.docstring can contain details about the constructed lambda.