LispBM
lbm_flat_value.h
1 /*
2  Copyright 2023 Joel Svensson svenssonjoel@yahoo.se
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef LBM_FLAT_VALUE_H_
19 #define LBM_FLAT_VALUE_H_
20 
21 #include <heap.h>
22 #include <symrepr.h>
23 #include <lbm_memory.h>
24 
25 typedef struct {
26  uint8_t *buf;
27  lbm_uint buf_size;
28  lbm_uint buf_pos;
30  // Arity
31 #define S_CONS 0x1 // 2 car, cdr
32 #define S_SYM_VALUE 0x2 // 1 value
33 #define S_SYM_STRING 0x3
34 #define S_BYTE_VALUE 0x4
35 #define S_I_VALUE 0x5
36 #define S_U_VALUE 0x6
37 #define S_I32_VALUE 0x7
38 #define S_U32_VALUE 0x8
39 #define S_FLOAT_VALUE 0x9
40 #define S_I64_VALUE 0xA
41 #define S_U64_VALUE 0xB
42 #define S_DOUBLE_VALUE 0xC
43 #define S_LBM_ARRAY 0xD
44 
45 // Maximum number of recursive calls
46 #define FLATTEN_VALUE_MAXIMUM_DEPTH 2000
47 
48 #define FLATTEN_VALUE_OK 0
49 #define FLATTEN_VALUE_ERROR_CANNOT_BE_FLATTENED -1
50 #define FLATTEN_VALUE_ERROR_BUFFER_TOO_SMALL -2
51 #define FLATTEN_VALUE_ERROR_ARRAY -3
52 #define FLATTEN_VALUE_ERROR_CIRCULAR -4
53 #define FLATTEN_VALUE_ERROR_MAXIMUM_DEPTH -5
54 #define FLATTEN_VALUE_ERROR_NOT_ENOUGH_MEMORY -6
55 #define FLATTEN_VALUE_ERROR_FATAL -7
56 
57 bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size);
58 bool lbm_finish_flatten(lbm_flat_value_t *v);
59 bool f_cons(lbm_flat_value_t *v);
60 bool f_sym(lbm_flat_value_t *v, lbm_uint sym);
61 bool f_sym_string(lbm_flat_value_t *v, lbm_uint sym);
62 bool f_i(lbm_flat_value_t *v, lbm_int i);
63 bool f_u(lbm_flat_value_t *v, lbm_uint u);
64 bool f_b(lbm_flat_value_t *v, uint8_t b);
65 bool f_i32(lbm_flat_value_t *v, int32_t w);
66 bool f_u32(lbm_flat_value_t *v, uint32_t w);
67 bool f_float(lbm_flat_value_t *v, float f);
68 bool f_i64(lbm_flat_value_t *v, int64_t w);
69 bool f_u64(lbm_flat_value_t *v, uint64_t w);
70 bool f_lbm_array(lbm_flat_value_t *v, uint32_t num_bytes, uint8_t *data);
71 lbm_value flatten_value(lbm_value v);
72 void lbm_set_max_flatten_depth(int depth);
73 
80 bool lbm_unflatten_value(lbm_flat_value_t *v, lbm_value *res);
81 #endif
uint32_t lbm_value
Definition: lbm_types.h:43
Definition: lbm_flat_value.h:25