harborc 0.1.1

A language that ports: targeting your thermostat, UFO, and kitchen sink.
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use alloc::collections::BTreeMap;
use core::fmt;
use super::{error, lir::Program};

use lalrpop_util::lalrpop_mod;
lalrpop_mod!(pub mir_parser);


#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Address(pub u32);

impl fmt::Display for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Literal(pub u32);

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Location {
    Address(Address),
    Deref(Box<Self>),
    Offset(Box<Self>, i32),
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Error {
    MacroNotDefined(String),
    CannotGetRuntimeAddress(Location),
    
    ParseError(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "\x1b[91merror: \x1b[m\x1b[0m")?;
        match self {
            Error::MacroNotDefined(name) => write!(f, "macro '{}' not defined", name),
            Error::CannotGetRuntimeAddress(location) => write!(f, "cannot get runtime address of {}", location),
            Error::ParseError(msg) => write!(f, "\n{}", msg),
        }
    }
}

pub fn parse(code: impl ToString) -> Result<Op, Error> {
    let code = code.to_string();
    match mir_parser::MIRParser::new().parse(&code) {
        Ok(parsed) => {
            Ok(parsed)
        },
        Err(e) => {
            Err(Error::ParseError(error::format_error(&code, e)))
        }
    }
}


pub fn function(mut args: Vec<(String, u32)>, ret_size: u32, code: Vec<Op>) -> Op {
    let mut offset = 0;
    let mut args_size = 0;
    let mut result = Op::Do(code);
    for (_, size) in &args {
        args_size += size;
    }
    
    while let Some((name, size)) = args.pop() {
        offset += size;
        result = Op::Let(name, vec![
            Op::LoadFrom(FP, 1),
            Op::Increment(SP.deref(), args_size - offset),
        ], vec![result]);
    }
    
    Op::Frame(args_size, ret_size, vec![result])
}

impl Location {
    pub fn get_address(&self) -> Result<Address, Error> {
        match self {
            Self::Address(address) => Ok(*address),
            Self::Offset(inner, offset) => {
                match inner.get_address() {
                    Ok(address) => Ok(Address((address.0 as i32 + offset) as u32)),
                    Err(error) => Err(error),
                }
            }
            Self::Deref(_) => Err(Error::CannotGetRuntimeAddress(self.clone())),
        }
    }

    pub fn deref(&self) -> Self {
        Self::Deref(Box::new(self.clone()))
    }

    pub fn offset(&self, count: i32) -> Self {
        Self::Offset(Box::new(self.clone()), count)
    }

    pub fn to(&self, program: &mut Program) {
        match self {
            Self::Deref(value) => {
                value.to(program);
                program.deref();
            },

            Self::Address(Address(address)) => {
                program.right(*address);
            }

            Self::Offset(value, count) => {
                value.to(program);
                program.shift(*count);
            }
        }
    }

    pub fn from(&self, program: &mut Program) {
        match self {
            Self::Deref(value) => {
                program.refer();
                value.from(program);
            },

            Self::Address(Address(address)) => {
                program.left(*address);
            }

            Self::Offset(value, count) => {
                program.shift(-*count);
                value.from(program);
            }
        }
    }

    pub fn zero(&self, program: &mut Program) {
        self.to(program);
        program.begin_loop();
        program.minus(1);
        program.end_loop();
        self.from(program);
    }

    pub fn plus(&self, n: u32, program: &mut Program) {
        self.to(program);
        program.plus(n);
        self.from(program);
    }

    pub fn inc(&self, program: &mut Program) {
        self.to(program);
        program.plus(1);
        self.from(program);
    }

    pub fn minus(&self, n: u32, program: &mut Program) {
        self.to(program);
        program.minus(n);
        self.from(program);
    }

    pub fn dec(&self, program: &mut Program) {
        self.to(program);
        program.minus(1);
        self.from(program);
    }

    pub fn set(&self, value: u32, program: &mut Program) {
        self.zero(program);
        self.plus(value, program);
    }

    pub fn alloc(&self, program: &mut Program) {
        self.to(program);
        program.alloc();
        self.from(program);
    }

    pub fn free(&self, program: &mut Program) {
        self.to(program);
        program.free();
        program.zero();
        self.from(program);
    }

    pub fn put(&self, program: &mut Program) {
        self.to(program);
        program.put();
        self.from(program);
    }

    pub fn get(&self, program: &mut Program) {
        self.to(program);
        program.get();
        self.from(program);
    }

    pub fn putnum(&self, program: &mut Program) {
        self.to(program);
        program.putnum();
        self.from(program);
    }

    pub fn getnum(&self, program: &mut Program) {
        self.to(program);
        program.getnum();
        self.from(program);
    }


    pub fn begin_loop(&self, program: &mut Program) {
        self.to(program);
        program.begin_loop();
        self.from(program);
    }
    
    pub fn end_loop(&self, program: &mut Program) {
        self.to(program);
        program.end_loop();
        self.from(program);
    }

    pub fn push(&self, program: &mut Program) {
        SP.inc(program);
        copy_cell(
            SP.deref(),
            self.clone(),
            program
        );
    }

    pub fn pop_into(&self, program: &mut Program) {
        copy_cell(
            self.clone(),
            SP.deref(),
            program
        );
        // SP.deref().zero(program);
        SP.dec(program);
    }

    pub fn load_ptr(&self, size: u32, program: &mut Program) {
        for i in 0..size {
            self.offset(i as i32).push(program);
        }
    }

    pub fn store_ptr(&self, size: u32, program: &mut Program) {
        for i in 0..size {
            self.offset((size - i - 1) as i32).pop_into(program);
        }
    }
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Address(loc) => if self == &SP {
                write!(f, "SP")
            } else if self == &FP {
                write!(f, "FP")

            } else if self == &TMP0 {
                write!(f, "TMP0")
            } else if self == &TMP1 {
                write!(f, "TMP1")
            } else if self == &TMP2 {
                write!(f, "TMP2")
            } else if self == &TMP3 {
                write!(f, "TMP3")
            } else if self == &TMP4 {
                write!(f, "TMP4")
            } else if self == &TMP5 {
                write!(f, "TMP5")
                
            } else if self == &R0 {
                write!(f, "R0")
            } else if self == &R1 {
                write!(f, "R1")
            } else if self == &R2 {
                write!(f, "R2")
            } else if self == &R3 {
                write!(f, "R3")
            } else if self == &R4 {
                write!(f, "R4")
            } else if self == &R5 {
                write!(f, "R5")

            } else {
                write!(f, "{}", loc.0)
            },
            Self::Offset(inner, offset) => write!(f, "{} {} +", inner, offset),
            Self::Deref(inner) => write!(f, "{}@", inner),
        }
    }
}

pub const TOTAL_REGISTERS: u32 = 14;


/// Stack pointer
pub const SP: Location = Location::Address(Address(0));

// Frame pointer
pub const FP: Location = Location::Address(Address(3));

/// Builtin registers for assembler use only
pub const TMP0: Location = Location::Address(Address(1));
pub const TMP1: Location = Location::Address(Address(2));
pub const TMP2: Location = Location::Address(Address(4));
pub const TMP3: Location = Location::Address(Address(5));
pub const TMP4: Location = Location::Address(Address(6));
pub const TMP5: Location = Location::Address(Address(7));

/// General purpose registers
pub const R0: Location = Location::Address(Address(8));
pub const R1: Location = Location::Address(Address(9));
pub const R2: Location = Location::Address(Address(10));
pub const R3: Location = Location::Address(Address(11));
pub const R4: Location = Location::Address(Address(12));
pub const R5: Location = Location::Address(Address(13));

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Op {
    Let(String, Vec<Self>, Vec<Self>),
    Macro(String),

    Frame(u32, u32, Vec<Self>),
    Do(Vec<Self>),


    /// Pop an address and store a value at that address
    Set(Literal),

    /// Store a number of cells at an address
    StoreAt(Location, u32),
    
    /// Load a number of cells from an address
    LoadFrom(Location, u32),

    /// Pop an address and store a number of cells at that address
    Store(u32),
    /// Pop an address and load cells at that address
    Load(u32),

    /// Pop a size off the stack, and allocate that number of cells
    Alloc,
    /// Pop an address off the stack, and free that number of cells
    Free,
    /// Duplicate the top cell on the stack
    Duplicate,

    /// Pop a cell and print it
    Putchar,

    /// Get a character from STDIN and push it
    Getchar,

    /// Pop a cell and print it as a number
    Putnum,
    
    /// Get a number from STDIN and push it
    Getnum,

    /// While loop
    While(Vec<Self>, Vec<Self>),

    /// If statement
    If(Vec<Self>, Vec<Self>),

    Increment(Location, u32),
    Decrement(Location, u32),

    /// Pop a value and push its logical not
    Not,

    /// Pop two cells and push their logical or
    Or,

    /// Pop two cells and push their logical and
    And,

    /// Pop two cells and push their sum
    Add,

    /// Pop two cells and push their difference
    Sub,

    /// Pop two cells and push their product
    Mul,

    /// Pop two cells and push their quotient
    Div,

    /// Pop two cells and push their equality
    Eq,

    /// Pop two cells and push their inequality
    Neq,

    /// Pop a value from the stack into an address
    Pop(Location),

    /// Push a literal onto the stack
    PushLiteral(Literal),
    /// Push an address onto the stack
    PushAddress(Address),
    /// Push a value onto the stack
    Push(Location),

    /// Allocate a number of cells on the stack
    Stalloc(u32),
    /// Pop a number of cells off the stack
    Stfree(u32),
}

pub fn copy_cell(x: Location, y: Location, program: &mut Program) {
    TMP0.zero(program);
    x.zero(program);
    
    y.begin_loop(program);
    x.inc(program);
    TMP0.inc(program);
    y.dec(program);
    y.end_loop(program);
    
    TMP0.begin_loop(program);    
    y.inc(program);
    TMP0.dec(program);
    TMP0.end_loop(program);
}

impl Op {
    pub fn assemble(&self, program: &mut Program) -> Result<(), Error> {
        self.assemble_with_scope(&BTreeMap::new(), program)
    }

    pub fn assemble_with_scope(&self, scope: &BTreeMap<String, Vec<Self>>, program: &mut Program) -> Result<(), Error> {
        match self {
            Self::Do(code) => {
                for op in code {
                    op.assemble_with_scope(&scope, program)?;
                }
            }
            Self::Let(name, val, ret) => {
                let mut new_scope = scope.clone();
                new_scope.insert(name.clone(), val.clone());
                
                for op in ret {
                    op.assemble_with_scope(&new_scope, program)?;
                }
            }

            Self::Macro(name) => {
                if let Some(code) = scope.get(name) {
                    let mut new_scope = scope.clone();
                    new_scope.remove(name);
                    for op in code {
                        op.assemble_with_scope(&new_scope, program)?;
                    }
                } else {
                    return Err(Error::MacroNotDefined(name.clone()));
                }
            }

            Self::Frame(args_size, ret_size, code) => {
                // Allocate some space on the heap and temporarily spill the arguments there

                if *args_size > 0 {
                    Self::PushLiteral(Literal(*args_size)).assemble_with_scope(scope, program)?;
                    Self::Alloc.assemble_with_scope(scope, program)?;
                    Self::Duplicate.assemble_with_scope(scope, program)?;
                    TMP5.pop_into(program);
                    Self::Store(*args_size).assemble_with_scope(scope, program)?;
                }


                // push old frame pointer
                FP.push(program);
                copy_cell(
                    FP,
                    SP,
                    program
                );
                // Increment it so it points to the first argument
                FP.inc(program);

                if *args_size > 0 {
                    // Load the arguments from the heap
                    TMP5.push(program);
                    Self::Load(*args_size).assemble_with_scope(scope, program)?;
                    // Free the memory we allocated to store them temporarily
                    TMP5.push(program);
                    Self::Free.assemble_with_scope(scope, program)?;
                }
                // Run the code in the new frame
                for op in code {
                    op.assemble_with_scope(scope, program)?;
                }

                if *ret_size > 0 {
                    // Spill the return value to some memory on the heap
                    Self::PushLiteral(Literal(*ret_size)).assemble_with_scope(scope, program)?;
                    Self::Alloc.assemble_with_scope(scope, program)?;
                    Self::Duplicate.assemble_with_scope(scope, program)?;
                    TMP5.pop_into(program);
                    Self::Store(*ret_size).assemble_with_scope(scope, program)?;
                }
                if *args_size > 0 {
                    // Remove the arguments from the stack
                    Self::Stfree(*args_size).assemble_with_scope(scope, program)?;
                }
                // Restore the frame pointer
                FP.pop_into(program);

                if *ret_size > 0 {
                    // Load the return value from the heap, and free the memory
                    TMP5.push(program);
                    Self::Load(*ret_size).assemble_with_scope(scope, program)?;
                    TMP5.push(program);
                    Self::Free.assemble_with_scope(scope, program)?;
                }
                TMP5.zero(program);
            }

            Self::Pop(loc) => {
                loc.pop_into(program);
            },

            Self::PushLiteral(Literal(n)) | Self::PushAddress(Address(n)) => {
                SP.inc(program);
                SP.deref().set(*n, program);
            },

            Self::Push(loc) => {
                loc.push(program);
            },

            Self::Load(size) => {
                TMP2.pop_into(program);
                TMP2.deref().load_ptr(*size, program);
            },

            Self::Store(size) => {
                // TMP2.pop_into(program);
                // TMP2.deref().store_ptr(*size, program);
                SP.dec(program);
                for i in 1..*size+1 {
                    SP.deref()
                      .offset(i as i32)
                      .deref()
                      .offset((*size - i) as i32)
                      .pop_into(program);
                }
            },

            Self::LoadFrom(loc, size) => {
                loc.load_ptr(*size, program);
            },

            Self::StoreAt(loc, size) => {
                loc.store_ptr(*size, program);
            },

            Self::Stalloc(size) => {
                if *size > 0 {
                    SP.plus(*size, program);
                }
            }

            Self::Stfree(size) => {
                if *size > 0 {
                    SP.minus(*size, program);
                }
            }

            Self::Alloc => {
                SP.deref().alloc(program);
            }

            Self::Free => {
                SP.deref().free(program);
                SP.dec(program);
            }

            Self::Duplicate => {
                copy_cell(SP.deref().offset(1), SP.deref(), program);
                SP.inc(program);
            }

            Self::Decrement(loc, n) => {
                loc.minus(*n, program)
            }

            Self::Increment(loc, n) => {
                loc.plus(*n, program)
            }

            Self::Not => {
                let x = SP.deref();

                TMP0.zero(program);
                x.begin_loop(program);
                TMP0.inc(program);
                x.zero(program);
                x.end_loop(program);
                x.inc(program);

                TMP0.begin_loop(program);
                x.dec(program);
                TMP0.dec(program);
                TMP0.end_loop(program);
            }

            Self::Add | Self::Or => {
                let x = SP.deref().offset(-1);
                let y = SP.deref();
                
                // do addition
                TMP0.zero(program);
                y.begin_loop(program);
                x.inc(program);
                TMP0.inc(program);
                y.dec(program);
                y.end_loop(program);

                TMP0.begin_loop(program);
                y.inc(program);
                TMP0.dec(program);
                TMP0.end_loop(program);

                SP.dec(program);
            }

            Self::Sub => {
                let x = SP.deref().offset(-1);
                let y = SP.deref();
                
                // do subtraction
                TMP0.zero(program);
                y.begin_loop(program);
                x.dec(program);
                TMP0.inc(program);
                y.dec(program);
                y.end_loop(program);

                TMP0.begin_loop(program);
                y.inc(program);
                TMP0.dec(program);
                TMP0.end_loop(program);

                SP.dec(program);
            }

            Self::Mul | Self::And => {
                let x = SP.deref().offset(-1);
                let y = SP.deref();
                
                // do multiplication
                TMP0.zero(program);
                TMP1.zero(program);
                
                x.begin_loop(program);
                TMP1.inc(program);
                x.dec(program);
                x.end_loop(program);

                TMP1.begin_loop(program);
                y.begin_loop(program);
                x.inc(program);
                TMP0.inc(program);
                y.dec(program);
                y.end_loop(program);

                TMP0.begin_loop(program);
                y.inc(program);
                TMP0.dec(program);
                TMP0.end_loop(program);

                TMP1.dec(program);
                TMP1.end_loop(program);

                SP.dec(program);
            }

            Self::Div => {
                let x = SP.deref().offset(-1);
                let y = SP.deref();

                // do division
                TMP0.zero(program);
                TMP1.zero(program);
                TMP2.zero(program);
                TMP3.zero(program);

                x.begin_loop(program);
                TMP0.inc(program);
                x.dec(program);
                x.end_loop(program);

                TMP0.begin_loop(program);

                y.begin_loop(program);
                TMP1.inc(program);
                TMP2.inc(program);
                y.dec(program);
                y.end_loop(program);

                TMP2.begin_loop(program);
                y.inc(program);
                TMP2.dec(program);
                TMP2.end_loop(program);

                TMP1.begin_loop(program);

                TMP2.inc(program);
                TMP0.dec(program);
                TMP0.begin_loop(program);
                TMP2.zero(program);
                TMP3.inc(program);
                TMP0.dec(program);
                TMP0.end_loop(program);
                
                TMP3.begin_loop(program);
                TMP0.inc(program);
                TMP3.dec(program);
                TMP3.end_loop(program);

                TMP2.begin_loop(program);
                TMP1.dec(program);
                TMP1.begin_loop(program);
                x.dec(program);
                TMP1.zero(program);
                TMP1.end_loop(program);
                TMP1.inc(program);

                TMP2.dec(program);
                TMP2.end_loop(program);
                TMP1.dec(program);

                TMP1.end_loop(program);
                x.inc(program);
                TMP0.end_loop(program);

                SP.dec(program);
            }

            Self::If(cond, body) => {
                let x = TMP2;
                for op in cond {
                    op.assemble_with_scope(scope, program)?;
                }
                x.pop_into(program);
                x.begin_loop(program);
                for op in body {
                    op.assemble_with_scope(scope, program)?;
                }
                x.zero(program);
                x.end_loop(program);
            }

            Self::While(cond, body) => {
                for op in cond {
                    op.assemble_with_scope(scope, program)?;
                }
                SP.deref().begin_loop(program);
                SP.dec(program);
                for op in body {
                    op.assemble_with_scope(scope, program)?;
                }
                for op in cond {
                    op.assemble_with_scope(scope, program)?;
                }
                SP.deref().end_loop(program);
                SP.dec(program);
            }

            Self::Eq => {
                let x = SP.deref().offset(-1);
                let y = SP.deref();
                // let x = TMP2;
                // let y = TMP3;
                // y.pop_into(program);
                // x.pop_into(program);

                TMP0.zero(program);
                TMP1.zero(program);

                x.begin_loop(program);
                TMP1.inc(program);
                x.dec(program);
                x.end_loop(program);
                x.inc(program);

                y.begin_loop(program);
                TMP1.dec(program);
                TMP0.inc(program);
                y.dec(program);
                y.end_loop(program);

                TMP0.begin_loop(program);
                y.inc(program);
                TMP0.dec(program);
                TMP0.end_loop(program);

                TMP1.begin_loop(program);
                x.dec(program);
                TMP1.zero(program);
                TMP1.end_loop(program);

                SP.dec(program);
                // x.push(program);
            }
            
            Self::Neq => {
                Self::Eq.assemble_with_scope(scope, program)?;
                Self::Not.assemble_with_scope(scope, program)?;
                // Self::Sub.assemble_with_scope(scope, program)?;
            }

            Self::Putnum => {
                SP.deref().putnum(program);
                // SP.deref().zero(program);
                SP.dec(program);
            }
            Self::Getnum => {
                // TMP2.getnum(program);
                // TMP2.push(program);
                SP.inc(program);
                SP.deref().getnum(program);
            }

            Self::Putchar => {
                // SP.deref().zero(program);
                SP.deref().put(program);
                SP.dec(program);
            }
            Self::Getchar => {
                // TMP2.get(program);
                // TMP2.push(program);
                SP.inc(program);
                SP.deref().get(program);
            }

            _ => unimplemented!()
        }
        Ok(())
    }
}