-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
158 lines (136 loc) · 2.74 KB
/
Command.cpp
File metadata and controls
158 lines (136 loc) · 2.74 KB
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
#include "Command.h"
#include <regex>
#include <iomanip>
using namespace std;
Command::Command(int currentline, int lastline) :
currentline_{ currentline },
lastline_{ lastline },
cmdstr{ "" },
comma_{ false },
cmd_{ '\0' },
first_{ 0 },
second_{ 0 }
{}
Command::~Command() {}
bool Command::parse(string input) {
cmdstr = input;
//use regular expression to validate command string
regex reg("^(\\$|\\.|\\d*)(,*)(\\$|\\.|\\d*)([a|i|v|u|d|x|r|j|p|c|\\-|\\+|g|w|q|\\*]*)$");
//match with regex
if (regex_match(input, reg)) {
//extract substring
smatch m;
regex_search(input, m, reg);
/*
m[1]: fist address
m[2]: comma
m[3]: second address
m[4]: command
*/
string tmp_cmd;
//empty cmd_
if (input.size() == 0) {
first_ = 1;
second_ = 1;
cmd_ = '+';
return true;
}
//illegal when user input address is "0"
//however, address "0" is used in some Led member functions
if (m[1] == "0" || m[3] == "0")
return false;
//deal with single ".", "$", +, - and *
if (input.size() == 1) {
char single_cmd = input.c_str()[0];
switch (single_cmd)
{
case '.':
first_ = currentline_;
second_ = currentline_;
cmd_ = 'p';
return true;
case '$':
first_ = lastline_;
second_ = lastline_;
cmd_ = 'g';
return true;
case '+':
case '-':
first_ = 1;
second_ = 1;
cmd_ = single_cmd;
return true;
case '*':
first_ = 1;
second_ = lastline_;
cmd_ = 'p';
return true;
}
}
int tmp_first = 0;
int tmp_second = 0;
//has first address
if (m[1] != "")
{
if (m[1] == ".")
tmp_first = currentline_;
else if (m[1] == "$")
tmp_first = lastline_;
else
tmp_first = stoi(m[1]);
}
else
tmp_first = currentline_;
//has comma
if (m[2] != "") {
comma_ = true;
}
//has second address
if (m[3] != "")
if (m[3] == ".")
tmp_second = currentline_;
else if (m[3] == "$")
tmp_second = lastline_;
else
tmp_second = stoi(m[3]);
else {
//has comma: x,z=>x,.z; x,=>x,.p
if (comma_)
tmp_second = currentline_;
//no comma: xz=>x,xz; x=>x,xg
else
tmp_second = tmp_first;
}
first_ = tmp_first;
second_ = tmp_second;
//has command
if (m[4] != "")
tmp_cmd = m[4];
else {
//has comma
if (comma_)
tmp_cmd = "p";
else
//x=>x,xg
tmp_cmd = "g";
}
cmd_ = tmp_cmd.c_str()[0];
return true;
}
else {
return false;
}
}
int Command::getFirstNumber() const {
return first_;
}
int Command::getSecondNumber() const {
return second_;
}
char Command::getCommand() const {
return cmd_;
}
std::ostream& operator<<(std::ostream& out, const Command& cmd) {
out << setw(4) << cmd.first_ << setw(4) << cmd.comma_ << setw(4) << cmd.second_ << setw(4) << cmd.cmd_;
return out;
}