Skip to content

Commit

Permalink
s/lk_/lake_/g because the great vowel shortage of 1973 is over
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Mar 7, 2014
1 parent 6047bba commit 7ac3811
Show file tree
Hide file tree
Showing 29 changed files with 361 additions and 325 deletions.
28 changes: 14 additions & 14 deletions src/bool.c
Expand Up @@ -12,47 +12,47 @@
#include "common.h"
#include "lake.h"

bool lk_bool_val(LakeBool *b)
bool lake_bool_val(LakeBool *b)
{
return b->val;
}

bool lk_is_true(LakeCtx *ctx, LakeVal *x)
bool lake_is_true(LakeCtx *ctx, LakeVal *x)
{
return VAL(x) == VAL(ctx->T);
}

bool lk_is_false(LakeCtx *ctx, LakeVal *x)
bool lake_is_false(LakeCtx *ctx, LakeVal *x)
{
return VAL(x) == VAL(ctx->F);
}

bool lk_is_truthy(LakeCtx *ctx, LakeVal *x)
bool lake_is_truthy(LakeCtx *ctx, LakeVal *x)
{
return !lk_is_false(ctx, x);
return !lake_is_false(ctx, x);
}

bool lk_is_falsy(LakeCtx *ctx, LakeVal *x)
bool lake_is_falsy(LakeCtx *ctx, LakeVal *x)
{
return lk_is_false(ctx, x);
return lake_is_false(ctx, x);
}

LakeBool *lk_bool_from_int(LakeCtx *ctx, int n)
LakeBool *lake_bool_from_int(LakeCtx *ctx, int n)
{
return n ? ctx->T : ctx->F;
}

char *lk_bool_repr(LakeBool *b)
char *lake_bool_repr(LakeBool *b)
{
return strdup(lk_bool_val(b) ? "#t" : "#f");
return strdup(lake_bool_val(b) ? "#t" : "#f");
}

LakeVal *lk_bool_and(LakeCtx *ctx, LakeVal *x, LakeVal *y)
LakeVal *lake_bool_and(LakeCtx *ctx, LakeVal *x, LakeVal *y)
{
return lk_is_truthy(ctx, x) && lk_is_truthy(ctx, y) ? y : x;
return lake_is_truthy(ctx, x) && lake_is_truthy(ctx, y) ? y : x;
}

LakeVal *lk_bool_or(LakeCtx *ctx, LakeVal *x, LakeVal *y)
LakeVal *lake_bool_or(LakeCtx *ctx, LakeVal *x, LakeVal *y)
{
return lk_is_truthy(ctx, x) ? x : y;
return lake_is_truthy(ctx, x) ? x : y;
}
18 changes: 9 additions & 9 deletions src/bool.h
Expand Up @@ -13,14 +13,14 @@
#include "common.h"
#include "lake.h"

bool lk_bool_val(LakeBool *b);
bool lk_is_true(LakeCtx *ctx, LakeVal *x);
bool lk_is_false(LakeCtx *ctx, LakeVal *x);
bool lk_is_truthy(LakeCtx *ctx, LakeVal *x);
bool lk_is_falsy(LakeCtx *ctx, LakeVal *x);
LakeBool *lk_bool_from_int(LakeCtx *ctx, int n);
char *lk_bool_repr(LakeBool *b);
LakeVal *lk_bool_and(LakeCtx *ctx, LakeVal *x, LakeVal *y);
LakeVal *lk_bool_or(LakeCtx *ctx, LakeVal *x, LakeVal *y);
bool lake_bool_val(LakeBool *b);
bool lake_is_true(LakeCtx *ctx, LakeVal *x);
bool lake_is_false(LakeCtx *ctx, LakeVal *x);
bool lake_is_truthy(LakeCtx *ctx, LakeVal *x);
bool lake_is_falsy(LakeCtx *ctx, LakeVal *x);
LakeBool *lake_bool_from_int(LakeCtx *ctx, int n);
char *lake_bool_repr(LakeBool *b);
LakeVal *lake_bool_and(LakeCtx *ctx, LakeVal *x, LakeVal *y);
LakeVal *lake_bool_or(LakeCtx *ctx, LakeVal *x, LakeVal *y);

#endif
4 changes: 2 additions & 2 deletions src/comment.c
Expand Up @@ -30,7 +30,7 @@ LakeComment *comment_make(LakeStr *text)

LakeComment *comment_from_c(char *text)
{
return comment_make(lk_str_from_c(text));
return comment_make(lake_str_from_c(text));
}

char *comment_repr(LakeComment *comment)
Expand All @@ -40,5 +40,5 @@ char *comment_repr(LakeComment *comment)

bool comment_equal(LakeComment *a, LakeComment *b)
{
return lk_str_equal(COMM_TEXT(a), COMM_TEXT(b));
return lake_str_equal(COMM_TEXT(a), COMM_TEXT(b));
}
2 changes: 1 addition & 1 deletion src/common.c
Expand Up @@ -12,7 +12,7 @@
#include <stdlib.h>
#include <stdio.h>

char *lk_str_append(char *s1, char *s2)
char *lake_str_append(char *s1, char *s2)
{
size_t n2 = strlen(s2);
s1 = realloc(s1, strlen(s1) + n2 + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Expand Up @@ -22,6 +22,6 @@ typedef int bool;
#define FALSE 0
#endif

char *lk_str_append(char *s1, char *s2);
char *lake_str_append(char *s1, char *s2);

#endif
12 changes: 6 additions & 6 deletions src/dlist.c
Expand Up @@ -47,21 +47,21 @@ char *dlist_repr(LakeDottedList *dlist)
if (dlist->head && LIST_N(dlist->head)) {
for (i = 0; i < LIST_N(dlist->head); ++i) {
s2 = lake_repr(LIST_VAL(dlist->head, i));
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
if (i != LIST_N(dlist->head) - 1) s = lk_str_append(s, " ");
if (i != LIST_N(dlist->head) - 1) s = lake_str_append(s, " ");
}
}
else if (dlist->head) {
s2 = lake_repr(dlist->head);
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
}
s = lk_str_append(s, " . ");
s = lake_str_append(s, " . ");
s2 = lake_repr(dlist->tail);
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
return lk_str_append(s, ")");
return lake_str_append(s, ")");
}

bool dlist_equal(LakeDottedList *a, LakeDottedList *b)
Expand Down
8 changes: 4 additions & 4 deletions src/env.c
Expand Up @@ -18,19 +18,19 @@ Env *env_make(Env *parent)
{
Env *env = malloc(sizeof(Env));
env->parent = parent;
env->bindings = lk_hash_make();
env->bindings = lake_hash_make();
return env;
}

Env *env_is_defined(Env *env, LakeSym *key)
{
if (lk_hash_get(env->bindings, key->s) != NULL) return env;
if (lake_hash_get(env->bindings, key->s) != NULL) return env;
return env->parent ? env_is_defined(env->parent, key) : NULL;
}

static void env_put(Env *env, LakeSym *key, LakeVal *val)
{
lk_hash_put(env->bindings, key->s, val);
lake_hash_put(env->bindings, key->s, val);
}

LakeVal *env_define(Env *env, LakeSym *key, LakeVal *val)
Expand All @@ -51,7 +51,7 @@ LakeVal *env_set(Env *env, LakeSym *key, LakeVal *val)

LakeVal *env_get(Env *env, LakeSym *key)
{
LakeVal *val = lk_hash_get(env->bindings, key->s);
LakeVal *val = lake_hash_get(env->bindings, key->s);
if (!val && env->parent) {
val = env_get(env->parent, key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/env.h
Expand Up @@ -15,7 +15,7 @@

struct env {
struct env *parent;
lk_hash_t *bindings;
lake_hash_t *bindings;
};
typedef struct env Env;

Expand Down
42 changes: 21 additions & 21 deletions src/eval.c
Expand Up @@ -42,8 +42,8 @@ static LakeVal *_and(LakeCtx *ctx, Env *env, LakeList *expr)

/* (and ...) */
LakeVal *result = LIST_N(expr) ? eval(ctx, env, list_shift(expr)) : VAL(ctx->T);
while (lk_is_truthy(ctx, result) && LIST_N(expr) > 0) {
result = lk_bool_and(ctx, result, eval(ctx, env, list_shift(expr)));
while (lake_is_truthy(ctx, result) && LIST_N(expr) > 0) {
result = lake_bool_and(ctx, result, eval(ctx, env, list_shift(expr)));
}
return result;
}
Expand All @@ -55,16 +55,16 @@ static LakeVal *_or(LakeCtx *ctx, Env *env, LakeList *expr)

/* (or ...) */
LakeVal *result = LIST_N(expr) ? eval(ctx, env, list_shift(expr)) : VAL(ctx->F);
while (lk_is_falsy(ctx, result) && LIST_N(expr) > 0) {
result = lk_bool_or(ctx, result, eval(ctx, env, list_shift(expr)));
while (lake_is_falsy(ctx, result) && LIST_N(expr) > 0) {
result = lake_bool_or(ctx, result, eval(ctx, env, list_shift(expr)));
}
return result;
}

static LakeVal *_setB(LakeCtx *ctx, Env *env, LakeList *expr)
{
/* (set! x 42) */
if (LIST_N(expr) == 3 && lk_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
if (LIST_N(expr) == 3 && lake_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "set!" symbol */
LakeSym *var = SYM(list_shift(expr));
LakeVal *form = list_shift(expr);
Expand All @@ -83,15 +83,15 @@ static LakeVal *_define(LakeCtx *ctx, Env *env, LakeList *expr)
/* TODO: make these more robust, check all expected params */

/* (define x 42) */
if (LIST_N(expr) == 3 && lk_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
if (LIST_N(expr) == 3 && lake_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "define" symbol */
LakeSym *var = SYM(list_shift(expr));
LakeVal *form = list_shift(expr);
env_define(env, var, eval(ctx, env, form));
}

/* (define (inc x) (+ 1 x)) */
else if (LIST_N(expr) >= 3 && lk_is_type(TYPE_LIST, LIST_VAL(expr, 1))) {
else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_LIST, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "define" symbol */
LakeList *params = LIST(list_shift(expr));
LakeSym *var = SYM(list_shift(params));
Expand All @@ -100,7 +100,7 @@ static LakeVal *_define(LakeCtx *ctx, Env *env, LakeList *expr)
}

/* (define (print format . args) (...)) */
else if (LIST_N(expr) >= 3 && lk_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) {
else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "define" symbol */
LakeDottedList *def = DLIST(list_shift(expr));
LakeList *params = dlist_head(def);
Expand All @@ -120,21 +120,21 @@ static LakeVal *_define(LakeCtx *ctx, Env *env, LakeList *expr)
static LakeVal *_lambda(LakeCtx *ctx, Env *env, LakeList *expr)
{
/* (lambda (a b c) ...) */
if (LIST_N(expr) >= 3 && lk_is_type(TYPE_LIST, LIST_VAL(expr, 1))) {
if (LIST_N(expr) >= 3 && lake_is_type(TYPE_LIST, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "lambda" symbol */
LakeList *params = LIST(list_shift(expr));
LakeList *body = expr;
return VAL(fn_make(params, NULL, body, env));
}
else if (LIST_N(expr) >= 3 && lk_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) {
else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_DLIST, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "lambda" symbol */
LakeDottedList *def = DLIST(list_shift(expr));
LakeList *params = dlist_head(def);
LakeSym *varargs = SYM(dlist_tail(def));
LakeList *body = expr;
return VAL(fn_make(params, varargs, body, env));
}
else if (LIST_N(expr) >= 3 && lk_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
else if (LIST_N(expr) >= 3 && lake_is_type(TYPE_SYM, LIST_VAL(expr, 1))) {
list_shift(expr); /* drop the "lambda" symbol */
LakeSym *varargs = SYM(list_shift(expr));
LakeList *body = expr;
Expand All @@ -154,7 +154,7 @@ static LakeVal *_if(LakeCtx *ctx, Env *env, LakeList *expr)
}
list_shift(expr); /* "if" token */
LakeVal *cond = eval(ctx, env, list_shift(expr));
if (lk_is_truthy(ctx, cond)) {
if (lake_is_truthy(ctx, cond)) {
return eval(ctx, env, list_shift(expr));
}
else {
Expand All @@ -171,13 +171,13 @@ static LakeVal *_cond(LakeCtx *ctx, Env *env, LakeList *expr)
LakeVal *pred;
LakeList *conseq;
while (LIST_N(expr)) {
if (!lk_is_type(TYPE_LIST, LIST_VAL(expr, 0))) {
if (!lake_is_type(TYPE_LIST, LIST_VAL(expr, 0))) {
invalid_special_form(expr, "expected a (predicate consequence) pair");
return NULL;
}
conseq = LIST(list_shift(expr));
pred = list_shift(conseq);
if (pred == ELSE || lk_is_truthy(ctx, eval(ctx, env, pred))) {
if (pred == ELSE || lake_is_truthy(ctx, eval(ctx, env, pred))) {
return eval_exprs1(ctx, env, conseq);
}
}
Expand All @@ -192,14 +192,14 @@ static LakeVal *_when(LakeCtx *ctx, Env *env, LakeList *expr)
}
list_shift(expr); /* "when" token */
LakeVal *cond = eval(ctx, env, list_shift(expr));
return lk_is_truthy(ctx, cond) ? eval_exprs1(ctx, env, expr) : NULL;
return lake_is_truthy(ctx, cond) ? eval_exprs1(ctx, env, expr) : NULL;
}

typedef LakeVal *(*handler)(LakeCtx *, Env *, LakeList *);

static void define_handler(LakeCtx *ctx, char *name, handler fn)
{
lk_hash_put(ctx->special_form_handlers, name, (void *)fn);
lake_hash_put(ctx->special_form_handlers, name, (void *)fn);
}

void init_special_form_handlers(LakeCtx *ctx)
Expand All @@ -222,13 +222,13 @@ void init_special_form_handlers(LakeCtx *ctx)
bool is_special_form(LakeCtx *ctx, LakeList *expr)
{
LakeVal *head = LIST_VAL(expr, 0);
if (!lk_is_type(TYPE_SYM, head)) return FALSE;
return lk_hash_has(ctx->special_form_handlers, SYM(head)->s);
if (!lake_is_type(TYPE_SYM, head)) return FALSE;
return lake_hash_has(ctx->special_form_handlers, SYM(head)->s);
}

static special_form_handler get_special_form_handler(LakeCtx *ctx, LakeSym *name)
{
return (special_form_handler)lk_hash_get(ctx->special_form_handlers, name->s);
return (special_form_handler)lake_hash_get(ctx->special_form_handlers, name->s);
}

static LakeVal *eval_special_form(LakeCtx *ctx, Env *env, LakeList *expr)
Expand Down Expand Up @@ -340,7 +340,7 @@ LakeVal *eval_exprs1(LakeCtx *ctx, Env *env, LakeList *exprs)
LakeVal *apply(LakeCtx *ctx, LakeVal *fnVal, LakeList *args)
{
LakeVal *result = NULL;
if (lk_is_type(TYPE_PRIM, fnVal)) {
if (lake_is_type(TYPE_PRIM, fnVal)) {
LakePrimitive *prim = PRIM(fnVal);
int arity = prim->arity;
if (arity == ARITY_VARARGS || LIST_N(args) == arity) {
Expand All @@ -351,7 +351,7 @@ LakeVal *apply(LakeCtx *ctx, LakeVal *fnVal, LakeList *args)
result = NULL;
}
}
else if (lk_is_type(TYPE_FN, fnVal)) {
else if (lake_is_type(TYPE_FN, fnVal)) {
LakeFn *fn = FN(fnVal);

/* Check # of params */
Expand Down
16 changes: 8 additions & 8 deletions src/fn.c
Expand Up @@ -35,31 +35,31 @@ char *fn_repr(LakeFn *fn)
{
char *s = malloc(8);
s[0] = '\0';
s = lk_str_append(s, "(lambda ");
s = lake_str_append(s, "(lambda ");
char *s2;
if (LIST_N(fn->params) && fn->varargs) {
LakeDottedList *params = dlist_make(fn->params, VAL(fn->varargs));
s2 = dlist_repr(params);
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
}
else if (fn->varargs) {
s2 = lake_repr(fn->varargs);
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
}
else {
s2 = lake_repr(fn->params);
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
}
s = lk_str_append(s, " ");
s = lake_str_append(s, " ");
int i;
for (i = 0; i < LIST_N(fn->body); ++i) {
s2 = lake_repr(LIST_VAL(fn->body, i));
s = lk_str_append(s, s2);
s = lake_str_append(s, s2);
free(s2);
if (i != LIST_N(fn->body) - 1) s = lk_str_append(s, " ");
if (i != LIST_N(fn->body) - 1) s = lake_str_append(s, " ");
}
return lk_str_append(s, ")");
return lake_str_append(s, ")");
}

0 comments on commit 7ac3811

Please sign in to comment.