-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn.cow
204 lines (172 loc) · 3.58 KB
/
rpn.cow
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
#
# Copyright (c) 2020 Brian Callahan <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Reverse Polish notation calculator
include "cowgol.coh";
var input: uint8[11];
var stack: int32[4];
var depth: int8 := 0;
sub Push(entry: int32) is
if depth == 4 then
depth := 3;
end if;
stack[(depth as uint8)] := entry;
depth := depth + 1;
if depth > 3 then
depth := 4;
end if;
end sub;
sub Clear() is
stack[0] := 0;
stack[1] := 0;
stack[2] := 0;
stack[3] := 0;
depth := 0;
end sub;
sub Calculate(op: uint8) is
sub Pop(): (entry: int32) is
depth := depth - 1;
if depth < 0 then
depth := 0;
end if;
entry := stack[(depth as uint8)];
stack[(depth as uint8)] := 0;
end sub;
var t: int32 := Pop();
case op is
when '+':
Push(Pop() + t);
when '-':
Push(Pop() - t);
when '*':
Push(Pop() * t);
when '/':
if t == 0 then
t := Pop();
print("\n?\n");
else
Push(Pop() / t);
end if;
when '%':
if t == 0 then
t := Pop();
print("\n?\n");
else
Push(Pop() % t);
end if;
end case;
end sub;
sub GetInput() is
sub Process(buffer: [uint8]): (result: int32) is
var c: uint8 := [buffer];
result := 0;
loop
if c >= '0' and c <= '9' then
c := c - '0';
else
break;
end if;
result := result * 10 + (c as int32);
buffer := buffer + 1;
c := [buffer];
end loop;
end sub;
var c: uint8;
var count: uint8 := 0;
while count < 11 loop
input[count] := 0;
count := count + 1;
end loop;
count := 0;
c := get_char();
if c >= '0' and c <= '9' then
input[count] := c;
loop
count := count + 1;
c := get_char();
if c == 13 or c == 10 then
break;
end if;
input[count] := c;
if count == 9 then
break;
end if;
end loop;
while c != 10 and c != 13 loop
c := get_char();
end loop;
Push(Process(&input[0]));
elseif c == '+' or c == '-' or c == '*' or c == '/' or c == '%' then
count := c;
Calculate(count);
elseif c == 'c' then
Clear();
elseif c == 'q' then
Exit();
end if;
while c != 10 and c != 13 loop
c := get_char();
end loop;
end sub;
sub PrintStack() is
# Print a signed 32-bit integer to the console.
sub print_d32(value: int32) is
var buffer: uint8[12];
var pe := IToA(value, 10, &buffer[0]);
print(&buffer[0]);
end sub;
print_char(0o033);
print("[2J");
print_char(0o33);
print("[H");
print("Reverse Polish Notation Calculator");
print_nl();
print_nl();
if depth == 4 then
print("*3: ");
else
print(" 3: ");
end if;
print_d32(stack[3]);
print_nl();
if depth == 3 then
print("*2: ");
else
print(" 2: ");
end if;
print_d32(stack[2]);
print_nl();
if depth == 2 then
print("*1: ");
else
print(" 1: ");
end if;
print_d32(stack[1]);
print_nl();
if depth == 0 or depth == 1 then
print("*0: ");
else
print(" 0: ");
end if;
print_d32(stack[0]);
print_nl();
print_nl();
print("input: ");
end sub;
Clear();
loop
PrintStack();
GetInput();
end loop;