Merge commit '8701f9ad0695f86b3e7bf514d829119cb72d4427'

This commit is contained in:
Benjamin Vedder 2023-08-03 16:59:46 +02:00
commit 0d60c633c9
5 changed files with 134 additions and 16 deletions

View File

@ -773,7 +773,7 @@ Example:
```clj
(progn (var a 10) (set 'a 20) a)
```
```
The expression above evaluates to 20.
@ -796,7 +796,7 @@ And now `a` has been associated with the value 20 in the global env.
Just like `set` and `setvar`, `setq` can be used on variables that
are bound locally such as in the body of a `let` or a `progn`-local variable
created using `var`.
created using `var`.
### progn
@ -832,7 +832,7 @@ This program evaluates 30 but also extends the global environment with the
The curlybrace `{` syntax is a short-form (syntactic sugar) for `(progn`.
The parser replaces occurrences of `{` with `(progn`. The `{` should be
closed with an `}`.
closed with an `}`.
These two programs are thus equivalent:
@ -842,7 +842,7 @@ These two programs are thus equivalent:
(define b 20)
(+ a b))
```
```
And
@ -852,7 +852,7 @@ And
(define b 20)
(+ a b)
}
```
```
---
@ -862,7 +862,7 @@ The closing curlybrace `}` should be used to close an opening `{` but purely
for esthetical reasons. The `}` is treated identically to a regular closing parenthesis `)`.
The opening `{` and closing `}` curlybraces are used as a short-form for `progn`-blocks
of sequences expressions.
of sequences expressions.
### var
@ -935,7 +935,7 @@ has been extended with the binding `(apa 1)`.
### read-eval-program
Parses and evaluates a program incrementally. `read-eval-program` reads a top-level expression
then evaluates it before reading the next.
then evaluates it before reading the next.
Example that evaluates to 20:
@ -950,7 +950,7 @@ Example that evaluates to 20:
```clj
(read-eval-program "@const-start (define a 10) (+ a 10) @const-end")
```
```
---
## Lists and cons cells
@ -1290,7 +1290,7 @@ alist. The form of a `setassoc` expression is `(setassoc alist-expr key-expr val
## Arrays (byte-buffers)
### bufcreate
Create an array of bytes. The
@ -1485,7 +1485,7 @@ Example:
( (? y) (< y 0) 'less-than-zero)
( (? y) (> y 0) 'greater-than-zero)
( (? y) (= y 0) 'equal-to-zero))
```
```
---
@ -1651,7 +1651,7 @@ Example
(recv-to 0.5
( timeout (handle-timeout))
( _ (do-something-else)))
```
```
---
@ -1667,6 +1667,97 @@ Example that changes mailbox size to 100 elements.
---
## Flat values
Lisp values can be "flattened" into an array representation. The flat
representation of a value contains all information needed so that the
value can be recreated, "unflattened", in another instance of the
runtime system (for example running on another microcontroller).
Not all values can be flattened, custom types for example cannot.
### flatten
The `flatten` function takes a value as single argument and returns the flat representation if
successful. A flatten expression has the form `(flatten expr)`. Note that `expr` is evaluated
before the flattening.
Example:
```
# (define a (flatten (+ 1 2)))
> ~FLATVAL~
```
The example above creates a `~FLATVAL~` representation of the the value `3`.
```
# (define a (flatten '(+ 1 2)))
> ~FLATVAL~
```
Now the `~FLATVAL~` contains a representation of the list `(+ 1 2)`
---
### unflatten
`unflatten` converts a flat value back into a lisp value. Te form of an
`unflatten` expression is `(unflatten flat-value)`
Example:
```
# (define a (flatten (+ 1 2)))
> ~FLATVAL~
# (unflatten a)
> 3
```
and:
```
# (define a (flatten '(+ 1 2)))
> ~FLATVAL~
# (unflatten a)
> (+ 1 2)
```
---
### fv-to-a
Convert a flat value to a byte-array by applying `fv-to-a`. The form of
an `fv-to-a` expression is `(fv-to-a flat-value)`.
Example:
```
# (define a (flatten '(1 2 3)))
> ~FLATVAL~
# (fv-to-a a)
> ""
```
---
### a-to-fv
Convert an array representing a valid flat value into a flat value. The form
of an `a-to-fv` expression is `(a-to-fv array)`.
Example:
```
# (define a (flatten '(1 2 3)))
> ~FLATVAL~
# (define b (fv-to-a a))
> ""
# (define c (a-to-fv b))
> ~FLATVAL~
# (unflatten c)
> (1 2 3)
```
---
## Macros
lispBM macros are created using the `macro` keyword. A macro

View File

@ -125,7 +125,7 @@
#define SYM_IND_F_TYPE 0x36
#define SYM_CHANNEL_TYPE 0x37
#define SYM_CUSTOM_TYPE 0x38
#define SYM_FLATVAL_TYPE 0x39
#define SYM_FLATVAL_TYPE SYM_ARRAY_TYPE
#define TYPE_CLASSIFIER_ENDS 0x39
#define SYM_NONSENSE 0x3A
@ -281,7 +281,9 @@
#define SYM_REG_EVENT_HANDLER 0x245
#define SYM_TAKE 0x246
#define SYM_DROP 0x247
#define FUNDAMENTALS_END 0x247
#define SYM_FLAT_TO_ARRAY 0x248
#define SYM_ARRAY_TO_FLAT 0x249
#define FUNDAMENTALS_END 0x249
#define SPECIAL_SYMBOLS_START 0
#define SPECIAL_SYMBOLS_END 0xFFFF
@ -462,7 +464,8 @@
#define ENC_SYM_REG_EVENT_HANDLER ENC_SYM(SYM_REG_EVENT_HANDLER)
#define ENC_SYM_TAKE ENC_SYM(SYM_TAKE)
#define ENC_SYM_DROP ENC_SYM(SYM_DROP)
#define ENC_SYM_FLAT_TO_ARRAY ENC_SYM(SYM_FLAT_TO_ARRAY)
#define ENC_SYM_ARRAY_TO_FLAT ENC_SYM(SYM_ARRAY_TO_FLAT)
#endif

View File

@ -1209,6 +1209,26 @@ static lbm_value fundamental_drop(lbm_value *args, lbm_uint nargs, eval_context_
return lbm_list_drop(lbm_dec_as_u32(args[1]), args[0]);
}
static lbm_value fundamental_fv_to_a(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
(void) ctx;
if (nargs == 1 && lbm_type_of(args[0]) == LBM_TYPE_FLATVAL) {
lbm_value res = args[0];
res = lbm_set_ptr_type(res, LBM_TYPE_ARRAY);
return res;
}
return ENC_SYM_TERROR;
}
static lbm_value fundamental_a_to_fv(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
(void) ctx;
if (nargs == 1 && lbm_type_of(args[0]) == LBM_TYPE_ARRAY) {
lbm_value res = args[0];
res = lbm_set_ptr_type(res, LBM_TYPE_FLATVAL);
return res;
}
return ENC_SYM_TERROR;
}
const fundamental_fun fundamental_table[] =
{fundamental_add,
fundamental_sub,
@ -1267,5 +1287,7 @@ const fundamental_fun fundamental_table[] =
fundamental_range,
fundamental_reg_event_handler,
fundamental_take,
fundamental_drop
fundamental_drop,
fundamental_fv_to_a,
fundamental_a_to_fv,
};

View File

@ -688,7 +688,7 @@ int lbm_gc_sweep_phase(void) {
case SYM_IND_F_TYPE:
lbm_memory_free((lbm_uint*)heap[i].car);
break;
case SYM_FLATVAL_TYPE:
//case SYM_FLATVAL_TYPE:
case SYM_ARRAY_TYPE:{
lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
if (lbm_memory_ptr_inside((lbm_uint*)arr->data)) {

View File

@ -223,6 +223,8 @@ special_sym const special_symbols[] = {
{"type-f32" , SYM_TYPE_FLOAT},
{"type-f64" , SYM_TYPE_DOUBLE},
{"array-create" , SYM_ARRAY_CREATE},
{"fv-to-a" , SYM_FLAT_TO_ARRAY},
{"a-to-fv" , SYM_ARRAY_TO_FLAT},
};
static lbm_uint *symlist = NULL;