1 # Integer arithmetic using postfix notation
4 # Division not implemented yet.
7 # $ ./translate apps/rpn.mu
10 # $ qemu-system-i386 code.img
16 # Error handling is non-existent. This is just a prototype.
18 fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
19 var in-storage: (stream byte 0x80)
20 var in/esi: (addr stream byte) <- address in-storage
21 var y/ecx: int <- copy 0
22 # read-eval-print loop
24 var x/eax: int <- draw-text-rightward screen, "> ", 0/x, 0x80/xmax, y, 3/fg/cyan, 0/bg
25 read-line-from-keyboard keyboard, in, screen, 0xf/fg 0/bg
27 var out/eax: int <- simplify in
30 out, y <- draw-int32-decimal-wrapping-right-then-down screen, out, 0/xmin, y, 0x80/xmax, 0x30/ymax, 0/x, y, 7/fg, 0/bg
39 data: (handle array int)
43 fn simplify in: (addr stream byte) -> _/eax: int {
44 var word-storage: slice
45 var word/ecx: (addr slice) <- address word-storage
46 var stack-storage: int-stack
47 var stack/esi: (addr int-stack) <- address stack-storage
48 initialize-int-stack stack, 0x10
49 $simplify:word-loop: {
51 var done?/eax: boolean <- slice-empty? word
54 # if word is an operator, perform it
56 var is-add?/eax: boolean <- slice-equal? word, "+"
59 var _b/eax: int <- pop-int-stack stack
60 var b/edx: int <- copy _b
61 var a/eax: int <- pop-int-stack stack
63 push-int-stack stack, a
64 loop $simplify:word-loop
67 var is-sub?/eax: boolean <- slice-equal? word, "-"
70 var _b/eax: int <- pop-int-stack stack
71 var b/edx: int <- copy _b
72 var a/eax: int <- pop-int-stack stack
74 push-int-stack stack, a
75 loop $simplify:word-loop
78 var is-mul?/eax: boolean <- slice-equal? word, "*"
81 var _b/eax: int <- pop-int-stack stack
82 var b/edx: int <- copy _b
83 var a/eax: int <- pop-int-stack stack
85 push-int-stack stack, a
86 loop $simplify:word-loop
88 # otherwise it's an int
89 var n/eax: int <- parse-decimal-int-from-slice word
90 push-int-stack stack, n
93 var result/eax: int <- pop-int-stack stack
97 fn initialize-int-stack _self: (addr int-stack), n: int {
98 var self/esi: (addr int-stack) <- copy _self
99 var d/edi: (addr handle array int) <- get self, data
101 var top/eax: (addr int) <- get self, top
105 fn push-int-stack _self: (addr int-stack), _val: int {
106 var self/esi: (addr int-stack) <- copy _self
107 var top-addr/ecx: (addr int) <- get self, top
108 var data-ah/edx: (addr handle array int) <- get self, data
109 var data/eax: (addr array int) <- lookup *data-ah
110 var top/edx: int <- copy *top-addr
111 var dest-addr/edx: (addr int) <- index data, top
112 var val/eax: int <- copy _val
113 copy-to *dest-addr, val
117 fn pop-int-stack _self: (addr int-stack) -> _/eax: int {
118 var self/esi: (addr int-stack) <- copy _self
119 var top-addr/ecx: (addr int) <- get self, top
125 subtract-from *top-addr, 1
126 var data-ah/edx: (addr handle array int) <- get self, data
127 var data/eax: (addr array int) <- lookup *data-ah
128 var top/edx: int <- copy *top-addr
129 var result-addr/eax: (addr int) <- index data, top
130 var val/eax: int <- copy *result-addr