mirror of https://github.com/rusefi/bldc.git
Merge commit 'ba71a9d0596cabedb68bde81a882f9505f401900'
This commit is contained in:
commit
ad9775f6e4
|
@ -458,13 +458,18 @@ Example that evaluates to 3.
|
||||||
|
|
||||||
Evaluate a list of data where each element represents an expression.
|
Evaluate a list of data where each element represents an expression.
|
||||||
|
|
||||||
This function interacts with the continuation passing style
|
Example that results in the value 15:
|
||||||
of the evaluator in a slightly non-intuitive way and should
|
```
|
||||||
be avoided in programs. It is used internally by the c-interoperation
|
(define prg '( (+ 1 2) (+ 3 4) (+ 10 5)))
|
||||||
code to start evaluation of newly loaded program.
|
(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
|
||||||
|
|
||||||
`cond` is a generalization of `if` to discern between n different cases
|
`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))`.
|
`(cond ( cond-expr1 expr1) (cond-expr2 expr2) ... (cond-exprN exprN))`.
|
||||||
The conditions are checked from first to last and for the first `cond-exprN`
|
The conditions are checked from first to last and for the first `cond-exprN`
|
||||||
that evaluates to true, the corresponding `exprN` is evaluated.
|
that evaluates to true, the corresponding `exprN` is evaluated.
|
||||||
|
|
|
@ -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;
|
||||||
lbm_value app_cont_prg;
|
lbm_value app_cont_prg;
|
||||||
lbm_value new_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);
|
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) {
|
if (lbm_type_of(new_prg) != LBM_TYPE_CONS) {
|
||||||
error_ctx(ENC_SYM_EERROR);
|
error_ctx(ENC_SYM_EERROR);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -862,7 +862,7 @@ lbm_value lbm_list_copy(lbm_value list) {
|
||||||
curr = lbm_cdr(curr);
|
curr = lbm_cdr(curr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return lbm_list_reverse(res);
|
return lbm_list_destructive_reverse(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append for proper lists only
|
// Append for proper lists only
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
(define prg '( (+ 1 2) (+ 3 4) (+ 10 5)))
|
||||||
|
|
||||||
|
|
||||||
|
(= (eval-program prg) 15)
|
|
@ -0,0 +1,4 @@
|
||||||
|
(define prg '( (eval-program '( (+ 1 2) (+ 2 3) (+ 10 5)))))
|
||||||
|
|
||||||
|
|
||||||
|
(= (eval-program prg) 15)
|
|
@ -0,0 +1,4 @@
|
||||||
|
(define prg (list (eval-program (list (+ 1 2) (+ 2 3) (+ 10 5)))))
|
||||||
|
|
||||||
|
|
||||||
|
(= (eval-program prg) 15)
|
|
@ -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)
|
Loading…
Reference in New Issue