Merge commit 'ba71a9d0596cabedb68bde81a882f9505f401900'

This commit is contained in:
Benjamin Vedder 2022-11-03 15:44:43 +01:00
commit ad9775f6e4
7 changed files with 42 additions and 16 deletions

View File

@ -458,13 +458,18 @@ Example that evaluates to 3.
Evaluate a list of data where each element represents an expression.
This function interacts with the continuation passing style
of the evaluator in a slightly non-intuitive way and should
be avoided in programs. It is used internally by the c-interoperation
code to start evaluation of newly loaded program.
Example that results in the value 15:
```
(define prg '( (+ 1 2) (+ 3 4) (+ 10 5)))
(eval-program prg)
```
Example that prints the strings "apa", "bepa" and "cepa":
```
(define prg '( (print "apa") (print "bepa") (print "cepa")))
(eval-program prg)
```
If you want to evaluate a program you can always use `eval` and
put the program you wish to evaluate in a `progn` form.
---
@ -547,7 +552,7 @@ The example below evaluates to 0 if a is less than or equal to 4. Otherwise it e
### cond
`cond` is a generalization of `if` to discern between n different cases
based on boolean expression. The form of a `cond` expression is:
based on boolean expressions. The form of a `cond` expression is:
`(cond ( cond-expr1 expr1) (cond-expr2 expr2) ... (cond-exprN exprN))`.
The conditions are checked from first to last and for the first `cond-exprN`
that evaluates to true, the corresponding `exprN` is evaluated.

View File

@ -1746,17 +1746,20 @@ static void apply_eval_program(lbm_value *args, lbm_uint nargs, eval_context_t *
lbm_value app_cont;
lbm_value app_cont_prg;
lbm_value new_prg;
if (ctx->K.sp > nargs+2) { // if there is a continuation
WITH_GC(app_cont, lbm_cons(ENC_SYM_APP_CONT, ENC_SYM_NIL));
WITH_GC_1(app_cont_prg, lbm_cons(app_cont, ENC_SYM_NIL), app_cont);
new_prg = lbm_list_append(app_cont_prg, ctx->program);
new_prg = lbm_list_append(prg, new_prg);
} else {
new_prg = lbm_list_append(prg, ctx->program);
}
lbm_value prg_copy;
WITH_GC(prg_copy, lbm_list_copy(prg));
lbm_stack_drop(&ctx->K, nargs+1);
if (ctx->K.sp > nargs+2) { // if there is a continuation
WITH_GC_1(app_cont, lbm_cons(ENC_SYM_APP_CONT, ENC_SYM_NIL), prg_copy);
WITH_GC_2(app_cont_prg, lbm_cons(app_cont, ENC_SYM_NIL), app_cont, prg_copy);
new_prg = lbm_list_append(app_cont_prg, ctx->program);
new_prg = lbm_list_append(prg_copy, new_prg);
} else {
new_prg = lbm_list_append(prg_copy, ctx->program);
}
if (lbm_type_of(new_prg) != LBM_TYPE_CONS) {
error_ctx(ENC_SYM_EERROR);
return;

View File

@ -862,7 +862,7 @@ lbm_value lbm_list_copy(lbm_value list) {
curr = lbm_cdr(curr);
}
return lbm_list_reverse(res);
return lbm_list_destructive_reverse(res);
}
// Append for proper lists only

View File

@ -0,0 +1,5 @@
(define prg '( (+ 1 2) (+ 3 4) (+ 10 5)))
(= (eval-program prg) 15)

View File

@ -0,0 +1,4 @@
(define prg '( (eval-program '( (+ 1 2) (+ 2 3) (+ 10 5)))))
(= (eval-program prg) 15)

View File

@ -0,0 +1,4 @@
(define prg (list (eval-program (list (+ 1 2) (+ 2 3) (+ 10 5)))))
(= (eval-program prg) 15)

View File

@ -0,0 +1,5 @@
(define prg (list (eval-program (list (+ 1 2) (+ 2 3) (+ 10 5)))))
(define r (+ 100 (eval-program prg)))
(= (eval-program prg) 15)