LispBM
Loading...
Searching...
No Matches
extensions.h
Go to the documentation of this file.
1
2/*
3 Copyright 2019, 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
4 2022 Benjamin Vedder
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef EXTENSIONS_H_
21#define EXTENSIONS_H_
22
23#include "heap.h"
24#include "lbm_types.h"
25#include "lbm_constants.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
37
40typedef struct {
42 char *name;
44
45
47
48#define LBM_EXTENSION(name, argv, argn) \
49 __attribute__((aligned(LBM_STORABLE_ADDRESS_ALIGNMENT))) lbm_value name(lbm_value *(argv), lbm_uint (argn))
50
56int lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size);
74bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix);
88bool lbm_clr_extension(lbm_uint sym_id);
94bool lbm_add_extension(char *sym_str, extension_fptr ext);
95
100static inline bool lbm_is_extension(lbm_value exp) {
101 return ((lbm_type_of(exp) == LBM_TYPE_SYMBOL) &&
102 (lbm_get_extension(lbm_dec_sym(exp)) != NULL));
103}
104
105
116bool lbm_check_number_all(lbm_value *args, lbm_uint argn);
122bool lbm_check_argn(lbm_uint argn, lbm_uint n);
130
131#define LBM_CHECK_NUMBER_ALL() if (!lbm_check_number_all(args, argn)) {return ENC_SYM_EERROR;}
132#define LBM_CHECK_ARGN(n) if (!lbm_check_argn(argn, n)) {return ENC_SYM_EERROR;}
133#define LBM_CHECK_ARGN_NUMBER(n) if (!lbm_check_argn_number(args, argn, n)) {return ENC_SYM_EERROR;}
134
136
137// Extension writing helpers
138
139extern lbm_value make_list(int num, ...);
140extern bool strmatch(const char *str1, const char *str2);
141
142static inline lbm_value mk_lam(lbm_value args, lbm_value body) {
143 return make_list(3, ENC_SYM_LAMBDA, args, body);
144}
145
146static inline lbm_value mk_call_cc(lbm_value body) {
147 return make_list(2, ENC_SYM_CALL_CC_UNSAFE, body);
148}
149
150static inline lbm_value mk_let(lbm_value bindings, lbm_value body) {
151 return make_list(3, ENC_SYM_LET, bindings, body);
152}
153
154static inline lbm_value mk_if(lbm_value cond, lbm_value tb, lbm_value fb) {
155 return make_list(4, ENC_SYM_IF, cond, tb, fb);
156}
157
158static inline lbm_value mk_inc(lbm_value v) {
159 return make_list(3, ENC_SYM_ADD, v, lbm_enc_i(1));
160}
161
162static inline lbm_value mk_lt(lbm_value a, lbm_value b) {
163 return make_list(3, ENC_SYM_LT, a, b);
164}
165
166static inline lbm_value mk_eq(lbm_value a, lbm_value b) {
167 return make_list(3, ENC_SYM_EQ, a, b);
168}
169
170static inline lbm_value mk_car(lbm_value a) {
171 return make_list(2, ENC_SYM_CAR, a);
172}
173
174static inline lbm_value mk_cdr(lbm_value a) {
175 return make_list(2, ENC_SYM_CDR, a);
176}
177
178#ifdef __cplusplus
179}
180#endif
181#endif
bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix)
Definition extensions.c:86
bool lbm_check_argn(lbm_uint argn, lbm_uint n)
Definition extensions.c:150
static bool lbm_is_extension(lbm_value exp)
Definition extensions.h:100
static lbm_value mk_call_cc(lbm_value body)
Definition extensions.h:146
static lbm_value mk_if(lbm_value cond, lbm_value tb, lbm_value fb)
Definition extensions.h:154
lbm_uint lbm_get_max_extensions(void)
Definition extensions.c:60
lbm_value make_list(int num,...)
Definition extensions.c:171
void lbm_extensions_set_next(lbm_uint i)
Definition extensions.c:34
bool lbm_check_true_false(lbm_value v)
Definition extensions.c:136
static lbm_value mk_inc(lbm_value v)
Definition extensions.h:158
static lbm_value mk_car(lbm_value a)
Definition extensions.h:170
static lbm_value mk_lt(lbm_value a, lbm_value b)
Definition extensions.h:162
extension_fptr lbm_get_extension(lbm_uint sym)
Definition extensions.c:68
lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn)
Definition extensions.c:38
static lbm_value mk_lam(lbm_value args, lbm_value body)
Definition extensions.h:142
lbm_value(* extension_fptr)(lbm_value *, lbm_uint)
Definition extensions.h:36
bool strmatch(const char *str1, const char *str2)
Definition extensions.c:182
bool lbm_check_number_all(lbm_value *args, lbm_uint argn)
Definition extensions.c:142
bool lbm_check_argn_number(lbm_value *args, lbm_uint argn, lbm_uint n)
Definition extensions.c:159
lbm_extension_t * extension_table
Definition extensions.c:32
static lbm_value mk_eq(lbm_value a, lbm_value b)
Definition extensions.h:166
static lbm_value mk_let(lbm_value bindings, lbm_value body)
Definition extensions.h:150
bool lbm_add_extension(char *sym_str, extension_fptr ext)
Definition extensions.c:98
static lbm_value mk_cdr(lbm_value a)
Definition extensions.h:174
int lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size)
Definition extensions.c:44
lbm_uint lbm_get_num_extensions(void)
Definition extensions.c:64
bool lbm_clr_extension(lbm_uint sym_id)
Definition extensions.c:76
static lbm_uint lbm_dec_sym(lbm_value x)
Definition heap.h:781
static lbm_value lbm_enc_i(lbm_int x)
Definition heap.h:721
static lbm_type lbm_type_of(lbm_value x)
Definition heap.h:682
#define ENC_SYM_LT
Definition lbm_defines.h:534
#define ENC_SYM_CALL_CC_UNSAFE
Definition lbm_defines.h:522
#define ENC_SYM_LET
Definition lbm_defines.h:480
#define ENC_SYM_IF
Definition lbm_defines.h:479
#define ENC_SYM_CAR
Definition lbm_defines.h:543
#define ENC_SYM_LAMBDA
Definition lbm_defines.h:478
#define ENC_SYM_ADD
Definition lbm_defines.h:525
#define ENC_SYM_CDR
Definition lbm_defines.h:544
#define LBM_TYPE_SYMBOL
Definition lbm_defines.h:77
#define ENC_SYM_EQ
Definition lbm_defines.h:530
uint32_t lbm_uint
Definition lbm_types.h:48
uint32_t lbm_value
Definition lbm_types.h:44
Definition extensions.h:40
extension_fptr fptr
Definition extensions.h:41
char * name
Definition extensions.h:42