-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
158 lines (140 loc) · 3.87 KB
/
main.cpp
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
// $Id: main.cpp,v 1.41 2014-07-02 20:01:17-07 - - $
//Lou George ligeorge
//Ryan Kwok rfkwok
#include <deque>
#include <iostream>
#include <map>
#include <stdexcept>
#include <utility>
using namespace std;
#include <unistd.h>
#include "bigint.h"
#include "debug.h"
#include "iterstack.h"
#include "scanner.h"
#include "util.h"
using bigint_stack = iterstack<bigint>;
void do_arith (bigint_stack& stack, const char oper) {
if (stack.size() < 2) throw ydc_exn ("stack empty");
bigint right = stack.top();
stack.pop();
DEBUGF ('d', "right = " << right);
bigint left = stack.top();
stack.pop();
DEBUGF ('d', "left = " << left);
bigint result;
switch (oper) {
case '+': result = left + right; break;
case '-': result = left - right; break;
case '*': result = left * right; break;
case '/': result = left / right; break;
case '%': result = left % right; break;
case '^': result = pow (left, right); break;
default: throw invalid_argument (
string ("do_arith operator is ") + oper);
}
DEBUGF ('d', "result = " << result);
stack.push (result);
}
void do_clear (bigint_stack& stack, const char) {
DEBUGF ('d', "");
stack.clear();
}
void do_dup (bigint_stack& stack, const char) {
bigint top = stack.top();
DEBUGF ('d', top);
stack.push (top);
}
void do_printall (bigint_stack& stack, const char) {
for (const auto &elem: stack) cout << elem << endl;
}
void do_print (bigint_stack& stack, const char) {
cout << stack.top() << endl;
}
void do_debug (bigint_stack& stack, const char) {
(void) stack; // SUPPRESS: warning: unused parameter 'stack'
cout << "Y not implemented" << endl;
}
class ydc_quit: public exception {};
void do_quit (bigint_stack&, const char) {
throw ydc_quit();
}
using function_t = void (*)(bigint_stack&, const char);
using fn_map = map<string,function_t>;
fn_map do_functions = {
{"+", do_arith},
{"-", do_arith},
{"*", do_arith},
{"/", do_arith},
{"%", do_arith},
{"^", do_arith},
{"Y", do_debug},
{"c", do_clear},
{"d", do_dup},
{"f", do_printall},
{"p", do_print},
{"q", do_quit},
};
//
// scan_options
// Options analysis: The only option is -Dflags.
//
void scan_options (int argc, char** argv) {
if (sys_info::execname().size() == 0) sys_info::execname (argv[0]);
opterr = 0;
for (;;) {
int option = getopt (argc, argv, "@:");
if (option == EOF) break;
switch (option) {
case '@':
debugflags::setflags (optarg);
break;
default:
complain() << "-" << (char) optopt << ": invalid option"
<< endl;
break;
}
}
if (optind < argc) {
complain() << "operand not permitted" << endl;
}
}
//
// Main function.
//
int main (int argc, char** argv) {
sys_info::execname (argv[0]);
scan_options (argc, argv);
bigint_stack operand_stack;
scanner input;
try {
for (;;) {
try {
token_t token = input.scan();
if (token.symbol == SCANEOF) break;
switch (token.symbol) {
case NUMBER:
operand_stack.push (token.lexinfo);
break;
case OPERATOR: {
fn_map::const_iterator fn
= do_functions.find (token.lexinfo);
if (fn == do_functions.end()) {
throw ydc_exn (octal (token.lexinfo[0])
+ " is unimplemented");
}
fn->second (operand_stack, token.lexinfo.at(0));
break;
}
default:
break;
}
}catch (ydc_exn& exn) {
cout << exn.what() << endl;
}
}
}catch (ydc_quit&) {
// Intentionally left empty.
}
return sys_info::status();
}