Squashed 'lispBM/lispBM/' changes from a5683c69..075fd296

075fd296 fix bug related to pattern matching guards
02b38d42 update lbmref
fb312618 update lbmref
884a0973 update lbmref
32af7455 update lbmref

git-subtree-dir: lispBM/lispBM
git-subtree-split: 075fd296a8c6061ae8c5a45b017392700675b1d8
This commit is contained in:
Benjamin Vedder 2022-11-10 12:08:00 +01:00
parent 54b662c15b
commit 807ff48e8d
5 changed files with 129 additions and 89 deletions

View File

@ -12,6 +12,7 @@ used as a value in and of itself a value (or data). So it can be used
to name data and functions and is itself also data.
---
**NOTE**
Symbols are expressed as strings in your program such as `a`, `let`,
@ -1238,6 +1239,15 @@ For example the match expression below evaluates to 2.
---
### no_match
The `no_match` symbol is returned from pattern matching if
no case matches the expression.
- Add a catch-all case to your pattern-matching. `_`.
---
### _
The underscore pattern matches anything.
@ -1506,21 +1516,30 @@ Below is an example that conditionally returns.
---
## Unparsable symbols
## Error handling
Unparsable symbols cannot be written into a program. The unparsable symbols
signals different kinds of error conditions that may point at something
being wrong in the code (or that it is exhausting all resources).
If an error occurs while evaluating a program, the process that runs
that program is killed. The result of the killed process is set to an
error symbol indicating what went wrong.
### no_match
If the process was created using `spawn` (or equivalently, started by a
issuing a command in the repl), the process dies and an error message
is presented over the registered printing callback (dependent on how LispBM
is integrated into your system). The `ctx_done_callback` is also called
and performs other integration dependent tasks related to the shutting down
of a process.
The `no_match` symbol is returned from pattern matching if
no case matches the expression.
- Add a catch-all case to your pattern-matching. `_`.
If the process was created using `spawn-trap`, in addition to the
above, a message is sent to the parent process (the process that
executed the spawn-trap) containing information about the process that
struck an error. See <a href="#spawn-trap">spawn-trap</a>.
The parent process can now choose to restart the process that crashed
or to take some other action.
---
## Error Symbols
### read_error
The `read_error` symbol is returned if the reader cannot
@ -1669,7 +1688,7 @@ A value with type `type-u` occupy 28bits on the 32 bit version of LBM and
---
### type-stream
### type-channel
---

View File

@ -34,6 +34,7 @@ extern "C" {
/*! \page changelog Changelog
Nov 9: Version 0.7.1
- Bugfix: string literal lengths.
- not-eq and != added.
- Corrected behaviour for eval when applied to no argument.
- lbm_memory operations are protected by mutex.

View File

@ -2246,7 +2246,7 @@ static void cont_match(eval_context_t *ctx) {
if (match(pattern, e, &new_env, &do_gc)) {
if (check_guard) {
CHECK_STACK(lbm_push_3(&ctx->K, lbm_cdr(patterns), ctx->curr_env, MATCH));
CHECK_STACK(lbm_push_3(&ctx->K, body, e, MATCH_GUARD));
CHECK_STACK(lbm_push_4(&ctx->K, new_env, body, e, MATCH_GUARD));
ctx->curr_env = new_env;
ctx->curr_exp = n1; // The guard
} else {
@ -2267,7 +2267,7 @@ static void cont_match(eval_context_t *ctx) {
}
if (check_guard) {
CHECK_STACK(lbm_push_3(&ctx->K, lbm_cdr(patterns), ctx->curr_env, MATCH));
CHECK_STACK(lbm_push_3(&ctx->K, body, e, MATCH_GUARD));
CHECK_STACK(lbm_push_4(&ctx->K, new_env, body, e, MATCH_GUARD));
ctx->curr_env = new_env;
ctx->curr_exp = n1; // The guard
} else {
@ -2354,14 +2354,16 @@ static void cont_match_guard(eval_context_t *ctx) {
if (lbm_is_symbol_nil(ctx->r)) {
lbm_value e;
lbm_pop(&ctx->K, &e);
lbm_stack_drop(&ctx->K, 1);
lbm_stack_drop(&ctx->K, 2);
ctx->r = e;
ctx->app_cont = true;
} else {
lbm_value body;
lbm_value env;
lbm_stack_drop(&ctx->K, 1);
lbm_pop(&ctx->K, &body);
lbm_pop_2(&ctx->K, &body, &env);
lbm_stack_drop(&ctx->K, 3);
ctx->curr_env = env;
ctx->curr_exp = body;
}
}

View File

@ -0,0 +1,9 @@
(defun f (x)
(match x
( (? x) (< x 5) 'smaller)
( (? x) (> x 5) 'larger)
( _ 'whatever)))
(and (eq (f 23) 'larger)
(eq (f 0.3) 'smaller))

View File

@ -0,0 +1,9 @@
(defun f (x)
(match x
( (? x) (< x 5) (list x 'smaller))
( (? x) (> x 5) (list x 'larger))
( _ 'whatever)))
(and (eq (f 23) '(23 larger))
(eq (f 0.3) '(0.3 smaller)))