bridge_adapters/
lisp_adapters.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//! Notes on how conversion works:
//!
//! # Accepting argumets
//! Check rust docs for [`SlFrom`], [`SlFromRef`], and [`SlFromRefMut`] to get a feel for
//! which rust types will work as parameters to the procedural macro.
//!
//! # Returning Arguments
//! [`SlInto`], [`SlIntoRef`], and [`SlIntoRefMut`] are all inverses of their respective from implementations
//! and are used to transform the rust types tha function return into slosh values from the procedural macro.
//!
//! # On accepting arguments from a dynamically typed language to a static one
//! Helper types in this crate like [`LooseString`] use the rust type system to model various slosh
//! types to avoid accepting a raw slosh [`Value`] as a parameter in the procedural macro. This is
//! not necessary, however, and should only be done if one finds oneself constantly accepting a
//! [`Value`] type in multiple procedural macro functions and functionally duplicating similar
//! parsing code.
//!
//! # On returning arguments from a statically typed language to a dynamically one
//! It is also possible to manually return a [`VMResult`] of [`Ok`]<[`Value`]> if the return
//! type of the function should not be bound by strict typing.
//!
//! # Borrowing and Mutable Borrowing when a rust type does not implement [`Copy`], demonstration with &str and &mut String
//!
//! [`SlAsRef`] and [`SlAsMut`] can be used to avoid cloning from the [`SloshVm`] into the scope of
//! function generated by the procedural macro but the traits must be structured properly.
//!
//!
//! ## For regular borrows, copy this pattern used for &str
//!
//! ```ignore
//!
//! impl<'a> SlFromRef<'a, Value> for &'a str {
//!     fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
//!         (&value).sl_as_ref(vm)
//!     }
//! }
//!
//! impl<'a> SlAsRef<'a, str> for &Value {
//!     fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a str> {
//!         match self {
//!             Value::String(h) => Ok(vm.get_string(*h)),
//!             Value::StringConst(i) => Ok(vm.get_interned(*i)),
//!             _ => Err(VMError::new_conversion(
//!                 ErrorStrings::fix_me_mismatched_type(
//!                     String::from(ValueTypes::from([
//!                         ValueType::String,
//!                         ValueType::StringConst,
//!                     ])),
//!                     self.display_type(vm),
//!                 ),
//!             )),
//!         }
//!     }
//! }
//! ```
//!
//!
//! # Mutable borrowing when a rust type does not implement copy
//!
//! ```ignore
//!
//! impl<'a> SlFromRefMut<'a, Value> for &'a mut String {
//!     fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> VMResult<Self> {
//!         (&value).sl_as_mut(vm)
//!     }
//! }
//!
//! impl<'a> SlAsMut<'a, String> for &Value {
//!     fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut String> {
//!         match self {
//!             Value::String(h) => vm.get_string_mut(*h),
//!             _ => Err(VMError::new_conversion(
//!                 ErrorStrings::fix_me_mismatched_type(
//!                     <&'static str>::from(ValueType::String),
//!                     self.display_type(vm),
//!                 ),
//!             )),
//!         }
//!     }
//! }
//! ```
//!
//!
//! ## rosetta stone for bridge macros
//! Rust Type                   | Slosh Type & Traits   <br>&emsp; <br> S -> R Convert Slosh -> Rust <br> &emsp; - Occurs when coercing slush arguments to the parameter types in the signature of the annotated Rust function. <br> R -> S Convert Rust -> Slosh <br> &emsp; - Occurs when coercing some returned Rust type to a Slosh type. |
//! ----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
//! [`String`]                  | [Value]`::String`           |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`String`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFrom`] `&`[`Value`] for [`String`]
//!                             |                             |
//! `&`[`String`]               | [`Value`]`::String`         |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] `&`[`String`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- take [`String`]
//!                             |                             |     &emsp;* uses Clone unless TODO PC ISSUE #7 the extant value problem
//!                             |                             |
//! `&mut `[`String`]           | [`Value`]`::String`         |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlAsMut`] [`String`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- take `&mut `[`String`]
//!                             |                             |     &emsp;* uses Clone unless TODO PC ISSUE #7 the extant value problem
//!                             |                             |
//! `&`[`str`]                  | [`Value`]`::String` / [`Value`]`::StringConst` |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlAsRef`] &[`str`] for `&`[`Value`]
//!                             |                             |     &emsp;- [`SlIntoRef`] &[`str`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFrom`] for [`Value`]
//!                             |                             |     &emsp;* uses Clone unless TODO PC ISSUE #7 - the extant value problem
//!                             |                             |     &emsp;- TODO PC ISSUE #7 adjacent is it even possible to call vm.alloc_string_ro on something that was *newly* created in the current fcn and returned as a RO value OR should that be made as a custom type so the user can declare their intent.
//!                             |                             |
//! [`char`]                    | [`Value`]`::CodePoint`      |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`char`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFrom`] `&`[`Value`] for [`char`]
//!                             |                             |
//! [SloshChar]               |  [`Value`]`::CharClusterLong` / [`Value`]`::CharCluster` / [`Value`]`::CodePoint` |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`SloshChar`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFromRef`] `&`[`Value`] for [`SloshChar`]
//!                             |                             |
//! [`LooseString`]             | [`Value`]`::String` / [`Value`]`::CodePoint` / [`Value`]`::CharCluster` / [`Value`]`::CharClusterLong` / [`Value`]`::Symbol` / [`Value`]`::Keyword` / [`Value`]`::StringConst` |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`LooseString`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;* Note: Always does an allocation and returns a [`Value`]`::String` type.
//!                             |                             |     &emsp;- [`SlFromRef`] `&`[`Value`] for [`LooseString`]
//!                             |                             |
//! [`bool`]                    | [`Value`]::True / [`Value`]::False / [`Value`]::Nil |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`bool`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFrom`] `&`[`Value`] for [`primitives`]
//!                             |                             |
//! [`u8`]                      | [`Value`]::Byte             |
//!                             |                             | S -> R
//!                             |                             |     &emsp;- [`SlIntoRef`] [`u8`] for `&`[`Value`]
//!                             |                             | R -> S
//!                             |                             |     &emsp;- [`SlFrom`] `&`[`Value`] for [`i8`]
//!                             |                             |
//! [`u32`]/[`i32`]/[`u64`]/[`i64`]/[`usize`]                             | [`Value`]::Int          |
//! TODO PC *current behavior overflows* |  [`Value`]::Int([[u8; 7]]), // Store a 7 byte int (i56...).                           |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//! [`f32`]/[`f64`]             | [`Value`]::Float(F56)       |                             |
//!                             |                             |
//!                             | [`Value`]::Pair(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::List(Handle, u16)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Vector(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Map(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Symbol(Interned)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Keyword(Interned)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Special(Interned) // Intended for symbols that are compiled.       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Builtin(u32)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Undefined       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Nil       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Bytes(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Lambda(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Closure(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Continuation(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::CallFrame(Handle)       |                             |
//!                             |                             |
//!                             |                             |
//!                             | [`Value`]::Error(Handle)       |                             |
//!                             |                             |
//!                             |                             |

use bridge_types::BridgedType;
#[cfg(doc)]
use bridge_types::{LooseString, SloshChar};
use compile_state::state::SloshVm;

use slvm::{VMResult, Value};
mod collections;
pub mod numbers;
pub mod primitives;
pub mod text;

/// Use mutable [`SloshVm`] to take a rust value and convert it to a [`BridgedType`].
pub trait SlFrom<T>: Sized
where
    Self: BridgedType,
{
    /// Converts to this type from the input type.
    fn sl_from(value: T, vm: &mut SloshVm) -> VMResult<Self>;
}

impl<T> SlFrom<Vec<T>> for Value
where
    T: SlInto<Value>,
{
    fn sl_from(value: Vec<T>, vm: &mut SloshVm) -> VMResult<Self> {
        let mut u = Vec::with_capacity(value.len());
        for v in value {
            u.push(v.sl_into(vm)?);
        }
        Ok(vm.alloc_vector(u))
    }
}

impl<'a, T> SlFromRef<'a, slvm::Value> for Vec<T>
where
    T: SlFromRef<'a, Value> + 'a,
{
    fn sl_from_ref(value: slvm::Value, vm: &'a SloshVm) -> VMResult<Self> {
        let mut res = vec![];
        for val in value.iter(vm) {
            let t: T = val.sl_into_ref(vm)?;
            res.push(t);
        }
        Ok(res)
    }
}

/// Inverse of [`SlFrom`]
pub trait SlInto<T>: Sized
where
    T: BridgedType,
{
    /// Converts this type into the (usually inferred) input type.
    fn sl_into(self, vm: &mut SloshVm) -> VMResult<T>;
}

impl<T, U> SlInto<U> for T
where
    U: SlFrom<T>,
{
    fn sl_into(self, vm: &mut SloshVm) -> VMResult<U> {
        U::sl_from(self, vm)
    }
}

pub trait SlFromRef<'a, T: BridgedType>
where
    Self: Sized,
{
    /// Converts to this type from the input type.
    fn sl_from_ref(value: T, vm: &'a SloshVm) -> VMResult<Self>;
}

/// Inverse of [`SlFromRef`]
pub trait SlIntoRef<'a, T>: Sized
where
    T: 'a,
    Self: BridgedType,
{
    /// Converts to this type from the input type.
    fn sl_into_ref(self, vm: &'a SloshVm) -> VMResult<T>;
}

impl<'a, T, U> SlIntoRef<'a, U> for T
where
    T: BridgedType,
    U: SlFromRef<'a, T>,
    U: 'a,
{
    fn sl_into_ref(self, vm: &'a SloshVm) -> VMResult<U> {
        U::sl_from_ref(self, vm)
    }
}

pub trait SlFromRefMut<'a, T: BridgedType>
where
    Self: Sized,
{
    /// Converts to this type from the input type.
    fn sl_from_ref_mut(value: T, vm: &'a mut SloshVm) -> VMResult<Self>;
}

/// Inverse of [`SlFromRefMut`]
pub trait SlIntoRefMut<'a, T>: Sized
where
    T: 'a,
    Self: BridgedType,
{
    /// Converts to this type from the input type.
    fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<T>;
}

impl<'a, T, U> SlIntoRefMut<'a, U> for T
where
    T: BridgedType,
    U: SlFromRefMut<'a, T>,
    U: 'a,
{
    fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<U> {
        U::sl_from_ref_mut(self, vm)
    }
}

/// Converts a [`BridgedType`] to some rust type
pub trait SlAsRef<'a, T: ?Sized>
where
    Self: BridgedType,
{
    /// Converts this type into a shared reference of the (usually inferred) input type.
    fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a T>;
}

// SlAsRef lifts over &
impl<'a, T: ?Sized, U: ?Sized> SlAsRef<'a, U> for &'a T
where
    T: SlAsRef<'a, U>,
    &'a T: BridgedType,
{
    #[inline]
    fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a U> {
        <T as SlAsRef<'a, U>>::sl_as_ref(*self, vm)
    }
}

// SlAsRef lifts over &mut
impl<'a, T: ?Sized, U: ?Sized> SlAsRef<'a, U> for &'a mut T
where
    T: SlAsRef<'a, U> + BridgedType,
    &'a mut T: BridgedType,
{
    #[inline]
    fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a U> {
        <T as SlAsRef<'a, U>>::sl_as_ref(*self, vm)
    }
}

pub trait SlAsMut<'a, T: ?Sized>
where
    Self: BridgedType,
{
    /// Converts this type into a mutable reference of the (usually inferred) input type.
    fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut T>;
}

// SlAsMut lifts over &mut
impl<'a, T: ?Sized, U: ?Sized> SlAsMut<'a, U> for &'a mut T
where
    T: SlAsMut<'a, U>,
    &'a mut T: BridgedType,
{
    #[inline]
    fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut U> {
        (*self).sl_as_mut(vm)
    }
}

impl SlFrom<Value> for Value {
    fn sl_from(value: Value, _vm: &mut SloshVm) -> VMResult<Self> {
        Ok(value)
    }
}