truth_machine/truth_machine.rs
1use esoteric_vm::{esoteric_assembly, Machine};
2
3fn main() -> Machine {
4 // initialize a new machine
5 let mut machine = Machine::default();
6
7 // assembly code for the machine
8 let asm = esoteric_assembly! {
9 // initialize dot pointer so that IO operations work
10
11 // push a dot character to stack
12 0: pushi b'.';
13 // pop to address 28657
14 2: pop 28657;
15
16 // set dot pointer to 28657 (has to be a prime or semiprime, which is also a fibonacci number)
17 4: ldidp 28657;
18
19 // ------------------
20
21 // get a single character to register ch
22 7: getchar;
23
24 // push register ch to stack (4 bytes)
25 8: pushch;
26
27 // pop character to register b
28 9: popb;
29
30 // pop the other 2 bytes
31 10: stackdealloc 2;
32
33 // put 49 (ascii for '1') in register L
34 13: pushi 0;
35 15: pushi 49;
36 17: popl;
37
38 // compare registers L and b
39 18: cmplb;
40
41 // push 30 (input == '1' logic)
42 19: pushi 0;
43 21: pushi 30;
44
45 // pop to execution pointer (jump to value on stack) if b is 0 (input is '1')
46 23: zpopep;
47
48 // empty the stack
49 24: stackdealloc 2;
50
51 // IF INPUT IS '0' (repeat it)
52 // fallback to the input == '1' logic, ran without jumps
53
54 // write the input value ('0')
55 27: writechar;
56
57 // halt machine
58 28: Ωtheendisnear;
59 29: Ωskiptothechase;
60
61 // IF INPUT IS '1' (repeat indefinitely)
62
63 // write the input value ('1')
64 30: writechar;
65
66 // push 30 (input == '1' logic)
67 31: pushi 0;
68 33: pushi 30;
69
70 // pop to execution pointer (jump to value on stack)
71 //
72 // this repeats the input == '1' logic indefinitely
73 35: popep;
74
75 };
76
77 // load machine code
78 machine.load(&asm, 0);
79
80 // run machine until it halts
81 machine.run();
82
83 // return the machine's register A (unused)
84 machine
85}