Allow for multiple strings of lambda documentation.
Still trying to see a good way to base testing off of this.
This commit is contained in:
parent
ce3472cb90
commit
bac1790b6c
|
@ -95,6 +95,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"
|
||||||
(reduce values _max (first values))
|
(reduce values _max (first values))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -102,6 +103,7 @@
|
||||||
|
|
||||||
(def min (fn (...values)
|
(def min (fn (...values)
|
||||||
"Return the smallest of the given values"
|
"Return the smallest of the given values"
|
||||||
|
"(min 2 1 6) => 1"
|
||||||
(reduce values _min (first values))
|
(reduce values _min (first values))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
26
src/object.c
26
src/object.c
|
@ -719,7 +719,7 @@ inline Object withLen(size_t len, enum Type type)
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Object constructLambda(const Object* params, const Object* docs, const Object* body, struct Environment* env)
|
inline Object constructLambda(const Object* params, const Object* body, struct Environment* env)
|
||||||
{
|
{
|
||||||
if (!params || !body) {
|
if (!params || !body) {
|
||||||
return errorObject(NULL_LAMBDA_LIST);
|
return errorObject(NULL_LAMBDA_LIST);
|
||||||
|
@ -728,12 +728,17 @@ inline Object constructLambda(const Object* params, const Object* docs, const Ob
|
||||||
if (params->type != TYPE_LIST) {
|
if (params->type != TYPE_LIST) {
|
||||||
throw(LAMBDA_ARGS_NOT_LIST, "fn params must be TYPE_LIST, but is %s", getTypeName(params));
|
throw(LAMBDA_ARGS_NOT_LIST, "fn params must be TYPE_LIST, but is %s", getTypeName(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Object* docs = body;
|
||||||
|
size_t docLength = 0;
|
||||||
|
while (body->type == TYPE_STRING) {
|
||||||
|
docLength += sizeof(char) * (strlen(body->string) + 1);
|
||||||
|
body = body->forward;
|
||||||
|
}
|
||||||
|
|
||||||
if (body->type != TYPE_LIST) {
|
if (body->type != TYPE_LIST) {
|
||||||
throw(LAMBDA_ARGS_NOT_LIST, "fn body must be TYPE_LIST, but is %s", getTypeName(body));
|
throw(LAMBDA_ARGS_NOT_LIST, "fn body must be TYPE_LIST, but is %s", getTypeName(body));
|
||||||
}
|
}
|
||||||
if (docs && docs->type != TYPE_STRING) {
|
|
||||||
throw(BAD_TYPE, "fn docstring must be TYPE_STRING, but is %s", getTypeName(docs));
|
|
||||||
}
|
|
||||||
|
|
||||||
Object o = newObject(TYPE_LAMBDA);
|
Object o = newObject(TYPE_LAMBDA);
|
||||||
o.lambda = malloc(sizeof(struct Lambda));
|
o.lambda = malloc(sizeof(struct Lambda));
|
||||||
|
@ -741,11 +746,16 @@ inline Object constructLambda(const Object* params, const Object* docs, const Ob
|
||||||
o.lambda->body = cloneList(body);
|
o.lambda->body = cloneList(body);
|
||||||
lambdaRefs(&o) = 1;
|
lambdaRefs(&o) = 1;
|
||||||
|
|
||||||
if (docs) {
|
if (docLength == 0) {
|
||||||
lambdaDocs(&o) = malloc(sizeof(char) * (strlen(docs->string) + 1));
|
|
||||||
strcpy(lambdaDocs(&o), docs->string);
|
|
||||||
} else {
|
|
||||||
lambdaDocs(&o) = NULL;
|
lambdaDocs(&o) = NULL;
|
||||||
|
} else {
|
||||||
|
lambdaDocs(&o) = malloc(docLength + 1);
|
||||||
|
char* docText = lambdaDocs(&o);
|
||||||
|
while (docs->type == TYPE_STRING) {
|
||||||
|
const char* data = docs->string;
|
||||||
|
docText += sprintf(docText, "%s\n", data);
|
||||||
|
docs = docs->forward;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object* dest = &o.lambda->body;
|
Object* dest = &o.lambda->body;
|
||||||
|
|
|
@ -317,7 +317,7 @@ Object errorWithAllocatedContextLineNo(enum errorCode code, char* context, int l
|
||||||
|
|
||||||
struct Error noError();
|
struct Error noError();
|
||||||
|
|
||||||
Object constructLambda(const Object* params, const Object* docs, const Object* body, struct Environment* env);
|
Object constructLambda(const Object* params, const Object* body, struct Environment* env);
|
||||||
|
|
||||||
#ifdef STANDALONE
|
#ifdef STANDALONE
|
||||||
|
|
||||||
|
|
|
@ -268,14 +268,8 @@ Object evalList(const Object* obj, struct Environment* env)
|
||||||
|
|
||||||
if (strcmp(first_form->string, "fn") == 0) {
|
if (strcmp(first_form->string, "fn") == 0) {
|
||||||
Object* params = first_form->forward;
|
Object* params = first_form->forward;
|
||||||
Object* doc = NULL;
|
Object* body = params->forward;
|
||||||
Object* body = NULL;
|
return constructLambda(params, body, env);
|
||||||
if (params) {
|
|
||||||
int twoParams = params->forward && params->forward->forward;
|
|
||||||
doc = twoParams ? params->forward : NULL;
|
|
||||||
body = twoParams ? params->forward->forward : params->forward;
|
|
||||||
}
|
|
||||||
return constructLambda(params, doc, body, env);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(first_form->string, "struct") == 0) {
|
if (strcmp(first_form->string, "struct") == 0) {
|
||||||
|
|
Loading…
Reference in New Issue