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
56bool 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) &&
103}
104
110bool lbm_check_number_all(lbm_value *args, lbm_uint argn);
116bool lbm_check_argn(lbm_uint argn, lbm_uint n);
124
125#define LBM_CHECK_NUMBER_ALL() if (!lbm_check_number_all(args, argn)) {return ENC_SYM_EERROR;}
126#define LBM_CHECK_ARGN(n) if (!lbm_check_argn(argn, n)) {return ENC_SYM_EERROR;}
127#define LBM_CHECK_ARGN_NUMBER(n) if (!lbm_check_argn_number(args, argn, n)) {return ENC_SYM_EERROR;}
128
130
131// Extension writing helpers
132
141extern lbm_value make_list(int num, ...);
142
150extern bool strmatch(const char *str1, const char *str2);
151
160static inline lbm_value mk_lam(lbm_value args, lbm_value body) {
161 return make_list(3, ENC_SYM_LAMBDA, args, body);
162}
163
172static inline lbm_value mk_call_cc(lbm_value body) {
173 return make_list(2, ENC_SYM_CALL_CC_UNSAFE, body);
174}
175
184static inline lbm_value mk_let(lbm_value bindings, lbm_value body) {
185 return make_list(3, ENC_SYM_LET, bindings, body);
186}
187
197static inline lbm_value mk_if(lbm_value cond, lbm_value tb, lbm_value fb) {
198 return make_list(4, ENC_SYM_IF, cond, tb, fb);
199}
200
208static inline lbm_value mk_inc(lbm_value v) {
209 return make_list(3, ENC_SYM_ADD, v, lbm_enc_i(1));
210}
211
220static inline lbm_value mk_lt(lbm_value a, lbm_value b) {
221 return make_list(3, ENC_SYM_LT, a, b);
222}
223
232static inline lbm_value mk_eq(lbm_value a, lbm_value b) {
233 return make_list(3, ENC_SYM_EQ, a, b);
234}
235
243static inline lbm_value mk_car(lbm_value a) {
244 return make_list(2, ENC_SYM_CAR, a);
245}
246
254static inline lbm_value mk_cdr(lbm_value a) {
255 return make_list(2, ENC_SYM_CDR, a);
256}
257
258#ifdef __cplusplus
259}
260#endif
261#endif
bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix)
Definition extensions.c:87
bool lbm_check_argn(lbm_uint argn, lbm_uint n)
Definition extensions.c:145
static bool lbm_is_extension(lbm_value exp)
Definition extensions.h:100
static lbm_value mk_call_cc(lbm_value body)
Definition extensions.h:172
static lbm_value mk_if(lbm_value cond, lbm_value tb, lbm_value fb)
Definition extensions.h:197
lbm_uint lbm_get_max_extensions(void)
Definition extensions.c:60
lbm_value make_list(int num,...)
Definition extensions.c:166
void lbm_extensions_set_next(lbm_uint i)
Definition extensions.c:34
static lbm_value mk_inc(lbm_value v)
Definition extensions.h:208
static lbm_value mk_car(lbm_value a)
Definition extensions.h:243
static lbm_value mk_lt(lbm_value a, lbm_value b)
Definition extensions.h:220
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:160
lbm_value(* extension_fptr)(lbm_value *, lbm_uint)
Definition extensions.h:36
bool strmatch(const char *str1, const char *str2)
Definition extensions.c:177
bool lbm_check_number_all(lbm_value *args, lbm_uint argn)
Definition extensions.c:137
bool lbm_check_argn_number(lbm_value *args, lbm_uint argn, lbm_uint n)
Definition extensions.c:154
bool lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size)
Definition extensions.c:44
lbm_extension_t * extension_table
Definition extensions.c:32
static lbm_value mk_eq(lbm_value a, lbm_value b)
Definition extensions.h:232
static lbm_value mk_let(lbm_value bindings, lbm_value body)
Definition extensions.h:184
bool lbm_add_extension(char *sym_str, extension_fptr ext)
Definition extensions.c:99
static lbm_value mk_cdr(lbm_value a)
Definition extensions.h:254
lbm_uint lbm_get_num_extensions(void)
Definition extensions.c:64
bool lbm_clr_extension(lbm_uint sym_id)
Definition extensions.c:77
static lbm_uint lbm_dec_sym(lbm_value x)
Definition heap.h:792
static lbm_value lbm_enc_i(lbm_int x)
Definition heap.h:710
static lbm_type lbm_type_of(lbm_value x)
Definition heap.h:666
#define ENC_SYM_LT
Definition lbm_defines.h:544
#define EXTENSION_SYMBOLS_START
Definition lbm_defines.h:396
#define ENC_SYM_CALL_CC_UNSAFE
Definition lbm_defines.h:531
#define ENC_SYM_LET
Definition lbm_defines.h:489
#define ENC_SYM_IF
Definition lbm_defines.h:488
#define ENC_SYM_CAR
Definition lbm_defines.h:553
#define ENC_SYM_LAMBDA
Definition lbm_defines.h:487
#define ENC_SYM_ADD
Definition lbm_defines.h:535
#define ENC_SYM_CDR
Definition lbm_defines.h:554
#define LBM_TYPE_SYMBOL
Definition lbm_defines.h:82
#define ENC_SYM_EQ
Definition lbm_defines.h:540
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