dg_xch_core 2.1.1

Core library containing type/error definitions, CLVM tools, Consensus and Pool definitions
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
pub mod conditions;
pub mod tests;
pub mod tokenizer;
pub mod utils;

use crate::clvm::assemble::assemble_text;
use crate::clvm::compile::conditions::{parse_constant, parse_function, parse_include};
use crate::clvm::compile::tokenizer::{Token, TokenType, Tokenizer};
use crate::clvm::compile::utils::{
    concat_args, get_arg_pointer, get_const_pointer, get_function_pointer, parse_value,
};
use crate::clvm::program::Program;
use crate::clvm::sexp::{IntoSExp, SExp};
use crate::constants::{
    APPLY_SEXP, B_KEYWORD_TO_SEXP, CONS_SEXP, INLINE_CONSTS, INLINE_DEFUNS, NULL_SEXP, QUOTE_SEXP,
};
use parking_lot::{Mutex, RwLock};
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use std::io::{Error, ErrorKind};
use std::mem::take;
use std::sync::atomic::{AtomicBool, Ordering};
use std::vec::IntoIter;

pub struct UnparsedCondition<'a> {
    tokens: Vec<Token<'a>>,
}
impl Debug for UnparsedCondition<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.tokens)
    }
}
#[derive(Clone)]
pub struct Function<'a> {
    name: Token<'a>,
    argument_names: Vec<Token<'a>>,
    function_body: Vec<Token<'a>>,
}
impl Debug for Function<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.name)
    }
}
pub struct Constant<'a> {
    name: Token<'a>,
    value: Token<'a>,
}
impl Debug for Constant<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Constant({:?}: {:?})", self.name, self.value)
    }
}

#[derive(Debug, Default)]
pub struct Compiler<'a> {
    pub argument_names: RwLock<Vec<Token<'a>>>,
    pub functions: RwLock<Vec<Function<'a>>>,
    pub inline_functions: RwLock<Vec<Function<'a>>>,
    pub constants: RwLock<Vec<Constant<'a>>>,
    pub body: Mutex<Vec<Token<'a>>>,
    pub reader: Tokenizer<'a>,
    pub include_dirs: &'a [&'a str],
    pub flags: u32,
    pub opt_level: u8,
    pub in_nested: AtomicBool,
}
impl<'a> Compiler<'a> {
    pub fn new(
        source: Cow<'a, [u8]>,
        flags: u32,
        opt_level: u8,
        include_dirs: &'a [&'a str],
    ) -> Self {
        Self {
            reader: Tokenizer::new(source),
            flags,
            opt_level,
            include_dirs,
            ..Default::default()
        }
    }
    pub fn compile(&'a self) -> Result<Program, Error> {
        self.pre_process()?;
        let program = self.process()?;
        self.post_process(program)
    }
    fn pre_process(&'a self) -> Result<(), Error> {
        self.ensure_token(TokenType::StartCons)?;
        self.ensure_token_value(TokenType::Expression, b"mod")?;
        self.parse_argument_names()?;
        self.parse_conditions()?;
        if self.flags & INLINE_DEFUNS == INLINE_DEFUNS {
            let funcs: Vec<Function<'a>> = take(self.functions.write().as_mut());
            let (defun, inline) =
                funcs
                    .into_iter()
                    .fold((vec![], vec![]), |(mut defun, mut inline), func| {
                        if self.can_inline_function(&func) {
                            inline.push(func);
                        } else {
                            defun.push(func);
                        }
                        (defun, inline)
                    });
            *self.functions.write().as_mut() = defun;
            self.inline_functions.write().extend(inline);
        }
        Ok(())
    }
    fn post_process(&'a self, program: Program) -> Result<Program, Error> {
        assemble_text(&format!("{program:?}")).map(|v| v.to_program())
    }
    fn process(&'a self) -> Result<Program, Error> {
        let mut output = None;
        let body: Vec<Token<'a>> = take(self.body.lock().as_mut());
        let mut iter = body.into_iter();
        while let Some(token) = iter.next() {
            match token.t_type {
                TokenType::StartCons | TokenType::DotCons => match output {
                    None => {
                        output = Some(self.process_pair(&mut iter)?);
                    }
                    Some(existing) => {
                        output = Some(existing.cons(self.process_pair(&mut iter)?));
                    }
                },
                TokenType::Expression => match output {
                    None => {
                        output = Some(self.process_atom(token, &mut iter)?);
                        break;
                    }
                    Some(existing) => {
                        output = Some(existing.cons(self.process_atom(token, &mut iter)?));
                    }
                },
                TokenType::EndCons => {
                    break;
                }
                TokenType::Comment => {}
            }
        }
        let body = output.ok_or(Error::new(ErrorKind::InvalidData, "No body found"))?;
        if self.functions.read().is_empty() {
            Program::from_sexp(body)
        } else {
            Program::from_sexp(
                APPLY_SEXP.clone().cons(
                    QUOTE_SEXP
                        .clone()
                        .cons(body)
                        .cons(self.get_program_args_sexp()?),
                ),
            )
        }
    }

    fn create_pair_sexp(&self, mut entries: Vec<SExp>, found_end: bool) -> Result<SExp, Error> {
        if entries.is_empty() {
            return if found_end {
                Ok(NULL_SEXP.clone())
            } else {
                Err(Error::new(
                    ErrorKind::UnexpectedEof,
                    "No closing cons found",
                ))
            };
        }
        if entries.len() == 1 {
            entries.pop().ok_or(Error::new(
                ErrorKind::UnexpectedEof,
                "Expected Entry, Length Was Checked",
            ))
        } else if entries.len() == 2 {
            let rest = entries.pop().ok_or(Error::new(
                ErrorKind::UnexpectedEof,
                "Expected Entry, Length Was Checked",
            ))?;
            let first = entries.pop().ok_or(Error::new(
                ErrorKind::UnexpectedEof,
                "Expected Entry, Length Was Checked",
            ))?;
            Ok(first.cons(rest))
        } else {
            concat_args(entries)
        }
    }

    fn process_pair(&'a self, token_stream: &mut IntoIter<Token<'a>>) -> Result<SExp, Error> {
        let mut entries = vec![];
        let mut found_end_cons = false;
        while let Some(token) = token_stream.next() {
            match token.t_type {
                TokenType::StartCons | TokenType::DotCons => {
                    entries.push(self.process_pair(token_stream)?);
                }
                TokenType::EndCons => {
                    found_end_cons = true;
                    break;
                }
                TokenType::Expression => {
                    entries.push(self.process_atom(token, token_stream)?);
                }
                TokenType::Comment => {}
            }
        }
        self.create_pair_sexp(entries, found_end_cons)
    }

    fn process_atom(
        &'a self,
        token: Token<'a>,
        token_stream: &mut IntoIter<Token<'a>>,
    ) -> Result<SExp, Error> {
        if self
            .functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_function(token, token_stream)
        } else if self
            .inline_functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_inline_function(token, token_stream)
        } else if self
            .constants
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_constant(token)
        } else if self
            .argument_names
            .read()
            .iter()
            .any(|v| v.bytes == token.bytes)
        {
            self.get_arg(token)
        } else {
            Ok(
                if let Some(kw) = B_KEYWORD_TO_SEXP.get(token.bytes.as_ref()) {
                    kw.clone()
                } else {
                    QUOTE_SEXP.clone().cons(parse_value(token.bytes.as_ref())?)
                },
            )
        }
    }

    fn get_function(
        &'a self,
        token: Token<'a>,
        token_stream: &mut IntoIter<Token<'a>>,
    ) -> Result<SExp, Error> {
        let (index, num_args) = self
            .functions
            .read()
            .iter()
            .enumerate()
            .find(|v| v.1.name.bytes == token.bytes)
            .map(|v| (v.0, v.1.argument_names.len() as u32))
            .ok_or(Error::new(ErrorKind::InvalidData, "Function not found"))?;
        let constants_count =
            self.constants.read().len() * (self.flags & INLINE_CONSTS != INLINE_CONSTS) as usize;
        let func_pointer =
            get_function_pointer(index as u8, constants_count, self.functions.read().len())?;
        let mut i_set_the_value = false;
        if !self.in_nested.load(Ordering::Relaxed) {
            self.in_nested.store(true, Ordering::Relaxed);
            i_set_the_value = true;
        }
        let args = self.get_function_args(num_args, token_stream)?;
        if i_set_the_value {
            self.in_nested.store(false, Ordering::Relaxed);
        }
        Ok(APPLY_SEXP
            .clone()
            .cons((func_pointer as u8).to_sexp().cons({
                let val = CONS_SEXP
                    .clone()
                    .cons(APPLY_SEXP.clone().cons(CONS_SEXP.clone().cons(
                        if self.in_nested.load(Ordering::Relaxed) {
                            args
                        } else {
                            args.cons(NULL_SEXP.clone())
                        },
                    )));
                if self.in_nested.load(Ordering::Relaxed) {
                    val
                } else {
                    val.cons(NULL_SEXP.clone())
                }
            })))
    }

    fn get_function_args(
        &'a self,
        num_args: u32,
        token_stream: &mut IntoIter<Token<'a>>,
    ) -> Result<SExp, Error> {
        let mut args = vec![];
        for _ in 0..num_args {
            let token = token_stream.next().ok_or(Error::new(
                ErrorKind::UnexpectedEof,
                "Expected Function argument",
            ))?;
            match token.t_type {
                TokenType::StartCons => {
                    args.push(self.process_pair(token_stream)?);
                }
                TokenType::Expression => {
                    args.push(self.process_atom(token, token_stream)?);
                }
                TokenType::DotCons | TokenType::EndCons | TokenType::Comment => {
                    return Err(Error::new(
                        ErrorKind::InvalidData,
                        format!("Expected Atm or Pair, Got {token:?}"),
                    ));
                }
            }
        }
        if args.len() == 1 {
            Ok(args.pop().expect("Expected at least one argument"))
        } else if args.len() == 2 {
            let rest = args.pop().expect("Expected at least two arguments");
            let first = args.pop().expect("Expected at least two arguments");
            Ok(first.cons(rest))
        } else {
            concat_args(args)
        }
    }

    fn get_program_args_sexp(&'a self) -> Result<SExp, Error> {
        let mut entries = vec![];
        for func in self.functions.read().clone().into_iter() {
            entries.push(self.get_function_body(func)?);
        }
        if self.flags & INLINE_CONSTS != INLINE_CONSTS {
            for constant in self.constants.read().iter().rev() {
                entries.push(parse_value(constant.value.bytes.as_ref())?);
            }
        }
        entries.push(QUOTE_SEXP.clone());
        let mut rtn = None;
        for arg in entries.into_iter() {
            match rtn {
                None => rtn = Some(arg),
                Some(r) => {
                    rtn = Some(arg.cons(r));
                }
            }
        }
        Ok(CONS_SEXP
            .clone()
            .cons(rtn.unwrap_or(NULL_SEXP.clone()).cons(QUOTE_SEXP.clone())))
    }

    fn get_function_body(&'a self, function: Function<'a>) -> Result<SExp, Error> {
        let mut tokens = function.function_body.into_iter();
        let args = &function.argument_names;
        self.process_function_pair(&mut tokens, args, 0)
    }

    fn process_function_pair(
        &'a self,
        token_stream: &mut IntoIter<Token<'a>>,
        function_args: &[Token<'a>],
        depth: u32,
    ) -> Result<SExp, Error> {
        let mut entries = vec![];
        let mut found_end_cons = false;
        while let Some(token) = token_stream.next() {
            match token.t_type {
                TokenType::StartCons | TokenType::DotCons => {
                    let pair =
                        self.process_function_pair(token_stream, function_args, depth + 1)?;
                    if depth == 1 {
                        entries.push(pair.cons(NULL_SEXP.clone()));
                    } else {
                        entries.push(pair);
                    }
                }
                TokenType::EndCons => {
                    found_end_cons = true;
                    break;
                }
                TokenType::Expression => {
                    entries.push(self.process_function_atom(token, token_stream, function_args)?);
                }
                TokenType::Comment => {}
            }
        }
        self.create_pair_sexp(entries, found_end_cons)
    }

    fn process_function_atom(
        &'a self,
        token: Token<'a>,
        token_stream: &mut IntoIter<Token<'a>>,
        function_args: &[Token<'a>],
    ) -> Result<SExp, Error> {
        if self
            .functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_function(token, token_stream)
        } else if self
            .inline_functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_inline_function(token, token_stream)
        } else if self
            .constants
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_constant(token)
        } else if function_args.iter().any(|v| v.bytes == token.bytes) {
            let (index, _) = function_args
                .iter()
                .enumerate()
                .find(|v| v.1.bytes == token.bytes)
                .ok_or(Error::new(ErrorKind::InvalidData, "Argument not found"))?;
            let arg_pointer = get_arg_pointer((index + 1) as u8)?;
            Ok((arg_pointer as u8).to_sexp())
        } else if let Some(kw) = B_KEYWORD_TO_SEXP.get(&token.bytes.as_ref()) {
            Ok(kw.clone())
        } else {
            Ok(QUOTE_SEXP.clone().cons(parse_value(token.bytes.as_ref())?))
        }
    }

    fn get_inline_function(
        &'a self,
        token: Token<'a>,
        token_stream: &mut IntoIter<Token<'a>>,
    ) -> Result<SExp, Error> {
        let cloned_func = self
            .inline_functions
            .read()
            .iter()
            .find(|v| v.name.bytes == token.bytes)
            .cloned();
        match cloned_func {
            Some(func) => {
                if self.can_inline_function(&func) {
                    let mut args = vec![];
                    for _ in 0..func.argument_names.len() {
                        if let Some(token) = token_stream.next() {
                            match token.t_type {
                                TokenType::StartCons | TokenType::DotCons => {
                                    args.push(self.process_pair(token_stream)?);
                                }
                                TokenType::EndCons => {
                                    break;
                                }
                                TokenType::Expression => {
                                    args.push(self.process_atom(token, token_stream)?);
                                }
                                TokenType::Comment => {}
                            }
                        } else {
                            return Err(Error::new(
                                ErrorKind::InvalidData,
                                "Failed to parse function arguments",
                            ));
                        }
                    }
                    let mut body_tokens = func.function_body.into_iter();
                    let func_body = self.process_inline_function_pair(
                        &mut body_tokens,
                        &func.argument_names,
                        &args,
                        0,
                    )?;
                    println!("Func Body: {func_body:?}");
                    Ok(func_body)
                } else {
                    Err(Error::new(
                        ErrorKind::InvalidData,
                        "Unable to Inline Function Marked as Inline",
                    ))
                }
            }
            None => Err(Error::new(ErrorKind::InvalidData, "Inline Func not found")),
        }
    }

    fn process_inline_function_pair(
        &'a self,
        token_stream: &mut IntoIter<Token<'a>>,
        function_args: &[Token<'a>],
        mapped_args: &[SExp],
        depth: u32,
    ) -> Result<SExp, Error> {
        let mut entries = vec![];
        let mut found_end_cons = false;
        while let Some(token) = token_stream.next() {
            match token.t_type {
                TokenType::StartCons | TokenType::DotCons => {
                    let pair = self.process_inline_function_pair(
                        token_stream,
                        function_args,
                        mapped_args,
                        depth + 1,
                    )?;
                    if depth == 1 {
                        entries.push(pair.cons(NULL_SEXP.clone()));
                    } else {
                        entries.push(pair);
                    }
                }
                TokenType::EndCons => {
                    found_end_cons = true;
                    break;
                }
                TokenType::Expression => {
                    entries.push(self.process_inline_function_atom(
                        token,
                        token_stream,
                        function_args,
                        mapped_args,
                    )?);
                }
                TokenType::Comment => {}
            }
        }
        self.create_pair_sexp(entries, found_end_cons)
    }

    fn process_inline_function_atom(
        &'a self,
        token: Token<'a>,
        token_stream: &mut IntoIter<Token<'a>>,
        function_args: &[Token<'a>],
        mapped_args: &[SExp],
    ) -> Result<SExp, Error> {
        if self
            .functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_function(token, token_stream)
        } else if self
            .inline_functions
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_inline_function(token, token_stream)
        } else if self
            .constants
            .read()
            .iter()
            .any(|v| v.name.bytes == token.bytes)
        {
            self.get_constant(token)
        } else if function_args.iter().any(|v| v.bytes == token.bytes) {
            let (index, _) = function_args
                .iter()
                .enumerate()
                .find(|v| v.1.bytes == token.bytes)
                .ok_or(Error::new(ErrorKind::InvalidData, "Argument not found"))?;
            Ok(mapped_args[index].clone())
        } else if let Some(kw) = B_KEYWORD_TO_SEXP.get(token.bytes.as_ref()) {
            Ok(kw.clone())
        } else {
            Ok(QUOTE_SEXP.clone().cons(parse_value(token.bytes.as_ref())?))
        }
    }

    fn can_inline_function(&'a self, function: &Function) -> bool {
        let mut found_sub_functions = HashSet::new();
        for token in &function.function_body {
            if token.t_type == TokenType::Expression
                && self
                    .functions
                    .read()
                    .iter()
                    .any(|v| v.name.bytes == token.bytes)
            {
                found_sub_functions.insert(&token.bytes);
            }
        }
        found_sub_functions.is_empty()
    }

    fn get_constant(&'a self, token: Token<'a>) -> Result<SExp, Error> {
        let (index, _) = self
            .constants
            .read()
            .iter()
            .enumerate()
            .find(|v| v.1.name.bytes == token.bytes)
            .ok_or(Error::new(ErrorKind::InvalidData, "Argument not found"))?;
        if self.flags & INLINE_CONSTS == 1 {
            self.constants
                .read()
                .iter()
                .find(|v| v.name.bytes == token.bytes)
                .ok_or(Error::new(ErrorKind::InvalidData, "Argument not found"))
                .map(|v| {
                    Ok(QUOTE_SEXP
                        .clone()
                        .cons(parse_value(v.value.bytes.as_ref())?))
                })?
        } else {
            let const_pointer = get_const_pointer(index as u8)?;
            Ok((const_pointer as u8).to_sexp())
        }
    }

    fn get_arg(&'a self, token: Token<'a>) -> Result<SExp, Error> {
        let (index, _) = self
            .argument_names
            .read()
            .iter()
            .enumerate()
            .find(|v| v.1.bytes == token.bytes)
            .ok_or(Error::new(ErrorKind::InvalidData, "Argument not found"))?;
        let arg_pointer = if self.functions.read().is_empty() {
            get_arg_pointer(index as u8)?
        } else {
            get_arg_pointer((index + !self.functions.read().is_empty() as usize) as u8)?
        };
        Ok((arg_pointer as u8).to_sexp())
    }
    fn ensure_token(&'a self, t_type: TokenType) -> Result<Token<'a>, Error> {
        if let Some(token) = self.reader.next_token() {
            if token.t_type != t_type {
                Err(Error::new(
                    ErrorKind::InvalidInput,
                    format!(
                        "Unexpected Token, Expected {t_type:?} Got {:?}",
                        token.t_type
                    ),
                ))
            } else {
                Ok(token)
            }
        } else {
            Err(Error::new(
                ErrorKind::UnexpectedEof,
                format!("Expected {t_type:?}"),
            ))
        }
    }
    fn ensure_token_value(
        &'a self,
        t_type: TokenType,
        expected_val: &[u8],
    ) -> Result<Token<'a>, Error> {
        let token = self.ensure_token(t_type)?;
        if token.bytes != expected_val {
            Err(Error::new(
                ErrorKind::InvalidInput,
                format!("Unexpected token value got {token:?}"),
            ))
        } else {
            Ok(token)
        }
    }
    fn parse_argument_names(&'a self) -> Result<(), Error> {
        self.ensure_token(TokenType::StartCons)?;
        while let Some(token) = self.reader.next_token() {
            match token.t_type {
                TokenType::EndCons => {
                    break;
                }
                TokenType::Expression => {
                    self.argument_names.write().push(token);
                }
                TokenType::StartCons | TokenType::DotCons | TokenType::Comment => {
                    return Err(Error::new(
                        ErrorKind::InvalidInput,
                        "Unexpected token, Expected Expression",
                    ))
                }
            }
        }
        Ok(())
    }
    fn parse_conditions(&'a self) -> Result<(), Error> {
        let mut conditions = vec![];
        while let Some(token) = self.reader.next_token() {
            if token.t_type == TokenType::StartCons {
                let mut tokens = vec![token];
                let mut depth = 0;
                while let Some(token) = self.reader.next_token() {
                    match token.t_type {
                        TokenType::EndCons => {
                            tokens.push(token);
                            if depth == 0 {
                                break;
                            }
                            depth -= 1;
                        }
                        TokenType::Expression | TokenType::DotCons | TokenType::Comment => {
                            tokens.push(token);
                        }
                        TokenType::StartCons => {
                            tokens.push(token);
                            depth += 1;
                        }
                    }
                }
                let cond = UnparsedCondition { tokens };
                conditions.push(cond);
            } else if token.t_type == TokenType::EndCons {
                match conditions.pop() {
                    Some(entry_node) => {
                        for condition in conditions {
                            self.parse_condition(condition)?
                        }
                        *self.body.lock().as_mut() = entry_node.tokens;
                    }
                    None => {
                        return Err(Error::new(
                            ErrorKind::InvalidInput,
                            "Expected At Least 1 Condition",
                        ))
                    }
                }
                return Ok(());
            } else {
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    format!("Unexpected token, Expected Start Cons got {token:?}"),
                ));
            }
        }
        Err(Error::new(ErrorKind::UnexpectedEof, "Expected Start Cons"))
    }
    fn parse_condition(&'a self, condition: UnparsedCondition<'a>) -> Result<(), Error> {
        assert!(condition.tokens.len() >= 2);
        let mut conditions_queue = condition.tokens.into_iter();
        assert_eq!(
            conditions_queue
                .next()
                .ok_or(Error::new(
                    ErrorKind::InvalidInput,
                    "Unexpected End of Token Stream"
                ))?
                .t_type,
            TokenType::StartCons
        );
        let operator = conditions_queue.next().ok_or(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected End of Token Stream",
        ))?;
        assert_eq!(operator.t_type, TokenType::Expression);
        match operator.bytes.as_ref() {
            b"defconstant" => {
                self.constants
                    .write()
                    .push(parse_constant(&mut conditions_queue)?);
            }
            b"defun" => {
                self.functions
                    .write()
                    .push(parse_function(&mut conditions_queue)?);
            }
            b"defun-inline" => {
                self.inline_functions
                    .write()
                    .push(parse_function(&mut conditions_queue)?);
            }
            b"include" => {
                let _results = parse_include(&mut conditions_queue, &[])?;
            }
            // b"defmacro" => {
            //     todo!()
            // }
            // b"lambda" => {
            //     todo!()
            // }
            _ => {
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    format!("Unexpected Expression: {:?}", operator),
                ))
            }
        }
        Ok(())
    }
}