-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpn.opal
executable file
·126 lines (118 loc) · 2.17 KB
/
rpn.opal
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
module RPN
use Lang
pub type rpn_cmd
= CmdConst(int)
or CmdDrop()
or CmdDup()
or CmdSwap()
or CmdFn1(fn(int) -> int)
or CmdFn2(fn(int, int) -> int)
impl array[int] { fn exec (cmd : rpn_cmd) {
match cmd {
CmdConst(n) {
self.push(n)
}
CmdDrop() {
self.pop()
}
CmdDup() {
self.push(self[self.len() - 1])
}
CmdSwap() {
let i = self.len() - 1;
let j = self.len() - 2;
let a = self[i];
let b = self[j];
self[i] = b;
self[j] = a;
}
CmdFn1(f) {
let a = self[self.len() - 1];
self.pop();
self.push(f(a));
}
CmdFn2(f) {
let a = self[self.len() - 2];
let b = self[self.len() - 1];
self.pop();
self.pop();
self.push(f(a, b));
}
}
}}
impl string {
fn triml () {
let i = 0;
while i < self.len() and self[i].space?() {
i = i + 1;
}
self[i,]
}
}
pub fn parseAll (str : string) {
str = str.triml();
if str.empty?() {
[]
} else {
let (cmd, str') = parse(str);
cmd $ parseAll(str')
}
}
pub fn parse (str : string) {
if str[0].digit?() {
let (num, str') = parseNumber(str);
(CmdConst(num), str')
} else {
let (id, str') = parseCmdName(str);
(cmdByName(id), str')
}
}
fn parseNumber (str : string) {
let i = 0;
while i < str.len() and str[i].digit?() {
i = i + 1;
}
(str[0, i].to_int(), str[i,])
}
fn parseCmdName (str : string) {
let i = 0;
while i < str.len() and not str[i].space?() {
i = i + 1;
}
(str[0, i], str[i,])
}
fn cmdByName (name : string) {
match name {
"drop" -> CmdDrop()
"dup" -> CmdDup()
"swap" -> CmdSwap()
"neg" -> CmdFn1(.neg)
"+" -> CmdFn2(.add)
"-" -> CmdFn2(.sub)
"*" -> CmdFn2(.mul)
"/" -> CmdFn2(.div)
}
}
pub fn main () {
let console = IO::console();
let stack = array();
let going = true;
while going {
console.write("\n");
if stack.empty?() {
console.write("(none)");
} else {
stack.each |x| {
console.write(x.str() + " ");
}
}
console.write("\n");
let input = console.prompt("# ");
if input.empty?() {
going = false;
} else {
parseAll(input).each(stack.exec);
}
}
console.write("\n")
}