Squashed 'lispBM/lispBM/' changes from 1c1bd9e6..fb7d3503

fb7d3503 added a llama
744ca079 Fixed an environment handling bug in conditionals
b448596c small tweaks (warnings)

git-subtree-dir: lispBM/lispBM
git-subtree-split: fb7d35031649049a618cb1fc0c690edbfe45d7f6
This commit is contained in:
Benjamin Vedder 2022-03-03 10:24:42 +01:00
parent 8e3a8a909d
commit 16307e5e9d
6 changed files with 129 additions and 7 deletions

View File

@ -825,12 +825,12 @@ Example that combines to lists.
<a name="ix"> <h3>ix</h3> </a> <a name="ix"> <h3>ix</h3> </a>
Index into a list using the <code>ix</code>. the form of an <code>ix</code> expression Index into a list using the <code>ix</code>. the form of an <code>ix</code> expression
is <code>(ix index-expr list-expr)</code>. Indexing starts from 0 and if you index out of bounds the result is nil. is <code>(ix list-expr index-expr)</code>. Indexing starts from 0 and if you index out of bounds the result is nil.
\note \note
Example that evaluates to 2. Example that evaluates to 2.
\code \code
(ix 1 (list 1 2 3)) (ix (list 1 2 3) 1)
\endcode \endcode

110
include/lbm_llama_ascii.h Normal file
View File

@ -0,0 +1,110 @@
unsigned char llama_ascii[] = {
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x27, 0x2c, 0x2c, 0x5e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x5e, 0x5f, 0x5f, 0x5f, 0x5f, 0x2b, 0x6c, 0x60,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x49, 0x5f, 0x5f, 0x5f, 0x5f,
0x5f, 0x5f, 0x5f, 0x49, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60, 0x5f,
0x2d, 0x5f, 0x2b, 0x2b, 0x7e, 0x2d, 0x5f, 0x2c, 0x60, 0x60, 0x2e, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x60, 0x2c, 0x69, 0x5f, 0x5f, 0x2b, 0x2b, 0x7e, 0x7e, 0x3e, 0x5e,
0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x22, 0x21, 0x21, 0x3c, 0x2b, 0x69, 0x3c,
0x2b, 0x7e, 0x22, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2e, 0x5f, 0x2d, 0x2b, 0x2b,
0x5f, 0x5f, 0x2d, 0x5f, 0x7e, 0x2b, 0x3a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2e, 0x21,
0x2b, 0x5f, 0x2b, 0x2b, 0x7e, 0x7e, 0x2b, 0x7e, 0x5f, 0x5f, 0x49, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x21, 0x5f, 0x2b, 0x7e, 0x2b, 0x7e, 0x5f, 0x2b, 0x2b, 0x2b, 0x5f,
0x5f, 0x5f, 0x49,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e, 0x2b, 0x2b, 0x69, 0x2d, 0x5f, 0x5f,
0x5f, 0x6c, 0x3e, 0x2b, 0x5f, 0x2b, 0x5f, 0x5f, 0x5f, 0x3c, 0x49, 0x5e,
0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2e, 0x3a, 0x22, 0x2e, 0x2c,
0x6c, 0x49, 0x49, 0x22, 0x20, 0x20, 0x3a, 0x5e, 0x2c, 0x21, 0x3a, 0x60,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x27,
0x27, 0x20, 0x60, 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60, 0x20, 0x2e,
0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x0a
};
unsigned int llama_ascii_len = 1275;

View File

@ -1256,9 +1256,10 @@ static inline void eval_lambda(eval_context_t *ctx) {
static inline void eval_if(eval_context_t *ctx) { static inline void eval_if(eval_context_t *ctx) {
CHECK_STACK(lbm_push_u32_3(&ctx->K, CHECK_STACK(lbm_push_u32_4(&ctx->K,
lbm_car(lbm_cdr(lbm_cdr(lbm_cdr(ctx->curr_exp)))), // Else branch lbm_car(lbm_cdr(lbm_cdr(lbm_cdr(ctx->curr_exp)))), // Else branch
lbm_car(lbm_cdr(lbm_cdr(ctx->curr_exp))), // Then branch lbm_car(lbm_cdr(lbm_cdr(ctx->curr_exp))), // Then branch
ctx->curr_env,
lbm_enc_u(IF))); lbm_enc_u(IF)));
ctx->curr_exp = lbm_car(lbm_cdr(ctx->curr_exp)); ctx->curr_exp = lbm_car(lbm_cdr(ctx->curr_exp));
} }
@ -1859,15 +1860,19 @@ static inline void cont_bind_to_key_rest(eval_context_t *ctx) {
} }
static inline void cont_if(eval_context_t *ctx) { static inline void cont_if(eval_context_t *ctx) {
lbm_value then_branch; lbm_value then_branch;
lbm_value else_branch; lbm_value else_branch;
lbm_value env;
lbm_value arg = ctx->r; lbm_value arg = ctx->r;
lbm_pop_u32_2(&ctx->K, &then_branch, &else_branch); lbm_pop_u32_3(&ctx->K, &env, &then_branch, &else_branch);
if (lbm_type_of(arg) == LBM_VAL_TYPE_SYMBOL && lbm_dec_sym(arg) == SYM_TRUE) { if (lbm_type_of(arg) == LBM_VAL_TYPE_SYMBOL && lbm_dec_sym(arg) == SYM_TRUE) {
ctx->curr_env = env;
ctx->curr_exp = then_branch; ctx->curr_exp = then_branch;
} else { } else {
ctx->curr_env = env;
ctx->curr_exp = else_branch; ctx->curr_exp = else_branch;
} }
} }

View File

@ -774,14 +774,14 @@ lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn) {
lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]); lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
switch(array->elt_type) { switch(array->elt_type) {
case LBM_VAL_TYPE_CHAR: /* Same as byte */ case LBM_VAL_TYPE_CHAR: /* Same as byte */
res = lbm_enc_i(array->size); res = lbm_enc_i((lbm_int)array->size);
break; break;
case LBM_VAL_TYPE_I: /* fall through */ case LBM_VAL_TYPE_I: /* fall through */
case LBM_VAL_TYPE_U: case LBM_VAL_TYPE_U:
case LBM_PTR_TYPE_BOXED_I: case LBM_PTR_TYPE_BOXED_I:
case LBM_PTR_TYPE_BOXED_U: case LBM_PTR_TYPE_BOXED_U:
case LBM_PTR_TYPE_BOXED_F: case LBM_PTR_TYPE_BOXED_F:
res = lbm_enc_i(array->size * 4); res = lbm_enc_i((lbm_int)array->size * 4);
break; break;
} }
} }

View File

@ -40,7 +40,7 @@ int main(int argc, char **argv) {
printf("Initialized heap: OK\n"); printf("Initialized heap: OK\n");
for (int i = 0; i < heap_size; i ++) { for (unsigned int i = 0; i < heap_size; i ++) {
cell = lbm_heap_allocate_cell(LBM_PTR_TYPE_CONS); cell = lbm_heap_allocate_cell(LBM_PTR_TYPE_CONS);
if (!lbm_is_ptr(cell)) { if (!lbm_is_ptr(cell)) {
printf("Error allocating cell %d\n", i); printf("Error allocating cell %d\n", i);

7
tests/test_if_2.lisp Normal file
View File

@ -0,0 +1,7 @@
(define test (lambda (f x)
(if (f x)
x
(test f (- x 1)))))
(= (test (lambda (x) (< x 0)) 2) -1)