1
+ use std:: rc:: Rc ;
2
+
1
3
use color_eyre:: eyre:: eyre;
2
4
use color_eyre:: Result ;
3
5
4
- use gc:: { Finalize , Gc , Trace } ;
5
-
6
6
use crate :: env:: Env ;
7
7
8
8
/// Evalutation happens here.
9
9
pub mod eval;
10
10
11
11
/// A single value in lwhlisp.
12
- #[ derive( Clone , Trace , Finalize ) ]
12
+ #[ derive( Clone ) ]
13
13
pub enum Atom {
14
14
/// Number
15
15
Number ( f64 ) ,
@@ -21,15 +21,15 @@ pub enum Atom {
21
21
///
22
22
/// This is also used to construct lists, using nested pairs.
23
23
/// For example (pseudocode), `Pair(1, Pair(2, Pair(3, nil)))` would be interpreted as `(1 2 3)`.
24
- Pair ( Gc < Atom > , Gc < Atom > ) ,
24
+ Pair ( Rc < Atom > , Rc < Atom > ) ,
25
25
/// Native Rust function.
26
26
///
27
27
/// This is used to implement some base function that require direct access to the underlying data.
28
- NativeFunc ( fn ( Gc < Atom > ) -> Result < Gc < Atom > > ) ,
28
+ NativeFunc ( fn ( Rc < Atom > ) -> Result < Rc < Atom > > ) ,
29
29
/// Closure
30
- Closure ( Env , Gc < Atom > , Gc < Atom > ) ,
30
+ Closure ( Env , Rc < Atom > , Rc < Atom > ) ,
31
31
/// Macro
32
- Macro ( Env , Gc < Atom > , Gc < Atom > ) ,
32
+ Macro ( Env , Rc < Atom > , Rc < Atom > ) ,
33
33
}
34
34
35
35
impl PartialEq for Atom {
@@ -172,8 +172,8 @@ impl Atom {
172
172
Atom :: Macro ( _env, args, expr) => {
173
173
let mut s = String :: new ( ) ;
174
174
let atom = Atom :: Pair (
175
- Gc :: new ( Atom :: symbol ( "defmacro" ) ) ,
176
- Gc :: new ( Atom :: Pair ( args. clone ( ) , expr. clone ( ) ) ) ,
175
+ Rc :: new ( Atom :: symbol ( "defmacro" ) ) ,
176
+ Rc :: new ( Atom :: Pair ( args. clone ( ) , expr. clone ( ) ) ) ,
177
177
) ;
178
178
write ! ( s, "{}" , atom. pretty_print( indent_level) ) . unwrap ( ) ;
179
179
s
@@ -187,18 +187,18 @@ impl Atom {
187
187
188
188
impl Atom {
189
189
/// Get the car of the atom if it is a pair, else return the atom itself.
190
- pub fn car ( & self ) -> Gc < Atom > {
190
+ pub fn car ( & self ) -> Rc < Atom > {
191
191
match self {
192
192
Atom :: Pair ( car, _) => car. clone ( ) ,
193
- a => Gc :: new ( a. clone ( ) ) ,
193
+ a => Rc :: new ( a. clone ( ) ) ,
194
194
}
195
195
}
196
196
197
197
/// Get the cdr of the atom if it is a pair, else return the atom itself.
198
- pub fn cdr ( & self ) -> Gc < Atom > {
198
+ pub fn cdr ( & self ) -> Rc < Atom > {
199
199
match self {
200
200
Atom :: Pair ( _, cdr) => cdr. clone ( ) ,
201
- a => Gc :: new ( a. clone ( ) ) ,
201
+ a => Rc :: new ( a. clone ( ) ) ,
202
202
}
203
203
}
204
204
@@ -208,9 +208,9 @@ impl Atom {
208
208
///
209
209
/// # Errors
210
210
/// If the atom is not a pair or nil, return an error.
211
- pub fn strict_cdr ( & self ) -> Result < Gc < Atom > > {
211
+ pub fn strict_cdr ( & self ) -> Result < Rc < Atom > > {
212
212
if self . is_nil ( ) {
213
- Ok ( Gc :: new ( self . clone ( ) ) )
213
+ Ok ( Rc :: new ( self . clone ( ) ) )
214
214
} else {
215
215
match self {
216
216
Atom :: Pair ( _, cdr) => Ok ( cdr. clone ( ) ) ,
@@ -230,7 +230,7 @@ impl Atom {
230
230
/// Return true if the atom is a proper list.
231
231
///
232
232
/// A proper list is a cons list where the last element is nil.
233
- pub fn is_proper_list ( expr : Gc < Self > ) -> bool {
233
+ pub fn is_proper_list ( expr : Rc < Self > ) -> bool {
234
234
let mut expr = expr;
235
235
while !expr. is_nil ( ) {
236
236
match expr. as_ref ( ) {
@@ -243,7 +243,7 @@ impl Atom {
243
243
}
244
244
245
245
/// Return true if the atom is a pair.
246
- pub fn is_list ( expr : & Gc < Self > ) -> bool {
246
+ pub fn is_list ( expr : & Rc < Self > ) -> bool {
247
247
matches ! ( expr. as_ref( ) , Atom :: Pair ( _, _) )
248
248
}
249
249
@@ -262,7 +262,7 @@ impl Atom {
262
262
/// Constructs a pair from two atoms
263
263
#[ must_use]
264
264
pub fn cons ( car : Atom , cdr : Atom ) -> Atom {
265
- Atom :: Pair ( Gc :: new ( car) , Gc :: new ( cdr) )
265
+ Atom :: Pair ( Rc :: new ( car) , Rc :: new ( cdr) )
266
266
}
267
267
268
268
/// Constructs a symbol from a string
@@ -310,9 +310,9 @@ impl Atom {
310
310
311
311
fn validate_closure_form (
312
312
env : Env ,
313
- args : Gc < Atom > ,
314
- body : Gc < Atom > ,
315
- ) -> Result < ( Env , Gc < Atom > , Gc < Atom > ) > {
313
+ args : Rc < Atom > ,
314
+ body : Rc < Atom > ,
315
+ ) -> Result < ( Env , Rc < Atom > , Rc < Atom > ) > {
316
316
if Atom :: is_proper_list ( body. clone ( ) ) {
317
317
// check argument names are all symbol
318
318
let mut p = args. clone ( ) ;
@@ -339,25 +339,25 @@ impl Atom {
339
339
///
340
340
/// # Errors
341
341
/// Return an error if an invalid closure form is given
342
- pub fn closure ( env : Env , args : Gc < Atom > , body : Gc < Atom > ) -> Result < Gc < Atom > > {
342
+ pub fn closure ( env : Env , args : Rc < Atom > , body : Rc < Atom > ) -> Result < Rc < Atom > > {
343
343
let ( env, args, body) = Atom :: validate_closure_form ( env, args, body) ?;
344
- Ok ( Gc :: new ( Atom :: Closure ( env, args, body) ) )
344
+ Ok ( Rc :: new ( Atom :: Closure ( env, args, body) ) )
345
345
}
346
346
347
347
/// Set a binding in a closure's environment if the atom is a closure.
348
348
///
349
349
/// # Errors
350
350
/// Returns an error if the given atom is not a closure.
351
351
pub fn closure_add_env_binding (
352
- atom : & Gc < Atom > ,
352
+ atom : & Rc < Atom > ,
353
353
name : String ,
354
- value : Gc < Atom > ,
355
- ) -> Result < Gc < Atom > > {
354
+ value : Rc < Atom > ,
355
+ ) -> Result < Rc < Atom > > {
356
356
match atom. as_ref ( ) {
357
357
Atom :: Closure ( env, a, b) => {
358
358
let mut env = env. clone ( ) ;
359
359
env. set ( name, value) ;
360
- Ok ( Gc :: new ( Atom :: Closure ( env, a. clone ( ) , b. clone ( ) ) ) )
360
+ Ok ( Rc :: new ( Atom :: Closure ( env, a. clone ( ) , b. clone ( ) ) ) )
361
361
}
362
362
a => {
363
363
Err ( eyre ! ( format!( "Tried to change the environment of a closure, but the provided atom was not a closure. Found {}" , a) ) )
@@ -384,7 +384,7 @@ impl Atom {
384
384
///
385
385
/// # Errors
386
386
/// Returns an error if the given atom is not a list, or if the list is not long enough
387
- pub fn get_list_item_by_index ( list : Gc < Self > , index : usize ) -> Result < Gc < Self > > {
387
+ pub fn get_list_item_by_index ( list : Rc < Self > , index : usize ) -> Result < Rc < Self > > {
388
388
let mut list = list;
389
389
let mut index = index;
390
390
while index > 0 {
@@ -396,7 +396,7 @@ impl Atom {
396
396
397
397
/// WARNING: This is probably broken, and should only be used when it doesn't matter much.
398
398
/// Currently it is used in the pretty printer, where it is used to count the lenght of a list.
399
- pub fn into_vec ( atom : Gc < Self > ) -> Vec < Gc < Self > > {
399
+ pub fn into_vec ( atom : Rc < Self > ) -> Vec < Rc < Self > > {
400
400
match atom. as_ref ( ) {
401
401
Atom :: Pair ( car, cdr) => {
402
402
let mut v = vec ! [ car. clone( ) ] ;
0 commit comments