Skip to content

Commit 3b2c437

Browse files
committed
go directly from expression to RPN, some * replaced by &
1 parent 0a80df7 commit 3b2c437

8 files changed

Lines changed: 90 additions & 70 deletions

File tree

calculator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <iostream>
22
#include "calculator.h"
33

4-
float calc(std::vector<RPNToken>& tokenlist, VarTable * vartabel){
4+
float calc(std::vector<RPNToken>& tokenlist, VarTable& vartabel){
55
if (tokenlist.size() == 0) return 0.f;
66

77
float res = 0;
@@ -15,7 +15,7 @@ float calc(std::vector<RPNToken>& tokenlist, VarTable * vartabel){
1515
//remember that operands will be eaten in reverse order
1616
switch (last.opcode) {
1717
case OC::NUM: res = last.value; break;
18-
case OC::VAR: res = vartabel->getValue((int)last.value); break;
18+
case OC::VAR: res = vartabel.getValue((int)last.value); break;
1919
case OC::MUL: res = calc(tokenlist, vartabel) * calc(tokenlist, vartabel); break;
2020
case OC::DIV: v1 = calc(tokenlist, vartabel); res = calc(tokenlist, vartabel)/v1; break;
2121
case OC::ADD: res = calc(tokenlist, vartabel) + calc(tokenlist, vartabel); break;
@@ -29,7 +29,7 @@ float calc(std::vector<RPNToken>& tokenlist, VarTable * vartabel){
2929
case OC::EQ: res = calc(tokenlist, vartabel) == calc(tokenlist, vartabel); break;
3030
case OC::NE: res = calc(tokenlist, vartabel) != calc(tokenlist, vartabel); break;
3131
case OC::ASS: res = calc(tokenlist, vartabel);
32-
vartabel->setVar(tokenlist[cursor-1].value, res); cursor--; break;
32+
vartabel.setVar(tokenlist[cursor-1].value, res); cursor--; break;
3333
case OC::COL: v1 = calc(tokenlist, vartabel); v2=calc(tokenlist, vartabel);
3434
res = calc(tokenlist, vartabel)?v2:v1; break;
3535
default: break;
@@ -39,7 +39,7 @@ float calc(std::vector<RPNToken>& tokenlist, VarTable * vartabel){
3939

4040
void calcandprint(std::vector<RPNToken> &tokenlist, VarTable * vartabel, bool prt) {
4141
cursor = tokenlist.size();
42-
float result = calc(tokenlist, vartabel);
42+
float result = calc(tokenlist, *vartabel);
4343
if (prt){
4444
std::cout << "EVALUATION RESULT ==> " << std::to_string(result) << std::endl;
4545
vartabel->printVarTable();

defs.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

main.cpp

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,39 @@
1313
#endif
1414

1515
using namespace std;
16-
void sleep(unsigned msec) {
16+
void sleep(unsigned msec)
17+
{
1718
struct timespec sleepTime;
1819
sleepTime.tv_sec = msec / 1000;
1920
sleepTime.tv_nsec = (msec % 1000) * 1000000;
2021
nanosleep(&sleepTime, NULL);
2122
}
2223

2324
int main(int argc, char *argv[])
24-
{ (void)(argc);(void)(argv);
25+
{
26+
(void)(argc);
27+
(void)(argv);
2528
string text = "";
2629

2730
const map<std::string, Token> keywords = {
28-
{"NUM", {OC::NUM, 0, 0, 0}}, // {opcode, arity, precedence, value}
29-
{"VAR", {OC::VAR, 0, 0, 0}},
30-
{"*", {OC::MUL, 2, 3, 0}},
31-
{"/", {OC::DIV, 2, 3, 0}},
32-
{"+", {OC::ADD, 2, 4, 0}},
33-
{"-", {OC::SUB, 2, 4, 0}},
34-
{"<", {OC::LT, 2, 6, 0}},
35-
{"<=", {OC::LE, 2, 6, 0}},
36-
{">", {OC::GT, 2, 6, 0}},
37-
{">=", {OC::GE, 2, 6, 0}},
38-
{"==", {OC::EQ, 2, 7, 0}},
39-
{"!=", {OC::NE, 2, 7, 0}},
40-
{"?", {OC::QU, -1, 13, 0}},
41-
{":", {OC::COL, 3, 13, 0}},
42-
{"=", {OC::ASS, 2, 14, 0}},
43-
{"(", {OC::PAR_L,0, 0, 0}},
44-
{")", {OC::PAR_R,0, 0, 0}},
45-
{"NIL", {OC::NIL, 0, 0, 0}}
46-
};
31+
{"NUM", {OC::NUM, 0, 0, 0}}, // {opcode, arity, precedence, value}
32+
{"VAR", {OC::VAR, 0, 0, 0}},
33+
{"*", {OC::MUL, 2, 3, 0}},
34+
{"/", {OC::DIV, 2, 3, 0}},
35+
{"+", {OC::ADD, 2, 4, 0}},
36+
{"-", {OC::SUB, 2, 4, 0}},
37+
{"<", {OC::LT, 2, 6, 0}},
38+
{"<=", {OC::LE, 2, 6, 0}},
39+
{">", {OC::GT, 2, 6, 0}},
40+
{">=", {OC::GE, 2, 6, 0}},
41+
{"==", {OC::EQ, 2, 7, 0}},
42+
{"!=", {OC::NE, 2, 7, 0}},
43+
{"?", {OC::QU, -1, 13, 0}},
44+
{":", {OC::COL, 3, 13, 0}},
45+
{"=", {OC::ASS, 2, 14, 0}},
46+
{"(", {OC::PAR_L, 0, 0, 0}},
47+
{")", {OC::PAR_R, 0, 0, 0}},
48+
{"NIL", {OC::NIL, 0, 0, 0}}};
4749

4850
VarTable vvv;
4951

@@ -53,24 +55,26 @@ int main(int argc, char *argv[])
5355

5456
// SPEEDTEST
5557

56-
if (1==2){ // 1==1 if you want it!
58+
if (1 == 2)
59+
{ // 1==1 if you want it!
5760
text = "b=a*a*a/100000000";
58-
tokenList = makeTokenList(text, &keywords, &vvv);
61+
tokenList = makeTokenList(text, keywords, vvv);
5962
// tokensRPN = p3.makeRPN(tokenList);
6063
int n = 0;
61-
while (n<500000){
64+
while (n < 500000)
65+
{
6266
std::cout << "\033[H\033[2J\033[3J";
6367
vvv.setVar(0, n);
6468
// calc.calcandprint(tokensRPN, false);
6569
n++;
6670
}
67-
sleep(100); // without sleep, cout gets in trouble in this final phase
71+
sleep(100); // without sleep, cout gets in trouble in this final phase
6872
// calc.calcandprint(tokensRPN, true);
6973
return 0;
7074
}
7175
// SPEEDTEST END
7276

73-
vector<string> inputs = {"","type it yourself",
77+
vector<string> inputs = {"", "type it yourself",
7478
"4/5",
7579
"2<3",
7680
"2>3",
@@ -101,40 +105,52 @@ int main(int argc, char *argv[])
101105
cout << "\nchoose: (0 to exit)\n"
102106
"\n1. enter expression";
103107

104-
for (long unsigned int i = 2; i<inputs.size(); i++)
105-
cout << "\n" << i << ". " << inputs[i] ;
108+
for (long unsigned int i = 2; i < inputs.size(); i++)
109+
cout << "\n"
110+
<< i << ". " << inputs[i];
106111
cout << endl;
107112
cout << "\nvartable at start:";
108113
vvv.printVarTable();
109-
while (true) {
114+
while (true)
115+
{
110116
cout << "\nchoice (0 to exit, 1 to enter expression manually, r to toggle reporting level) ==> ";
111-
cin >> ch; cin.ignore();
112-
if (ch!=""){
113-
if (isNumeric(ch) ){
117+
cin >> ch;
118+
cin.ignore();
119+
if (ch != "")
120+
{
121+
if (isNumeric(ch))
122+
{
114123
choice = stoi(ch);
115-
if (choice < inputs.size()){
116-
if (choice == 0) return 0;
117-
if (choice == 1) {
124+
if (choice < inputs.size())
125+
{
126+
if (choice == 0)
127+
return 0;
128+
if (choice == 1)
129+
{
118130
getline(cin, text);
119-
if (text == "") continue;
131+
if (text == "")
132+
continue;
120133
}
121-
else text = inputs[choice];
122-
cout << "text in:\t\t" << text << "\t\t";
134+
else
135+
text = inputs[choice];
136+
cout << "\ntext in:\t\t" << text << "\t\t";
123137

124-
vector<Token> tokenList = makeTokenList(text,
125-
&keywords,
126-
&vvv);
138+
// directly from expression to RPN:
139+
vector<RPNToken> tokensRPN = makeRPN(text, keywords, vvv);
127140

128-
vector<RPNToken> tokensRPN = makeRPN(tokenList);
141+
// separate calls:
142+
// vector<Token> tokenList = makeTokenList(text, keywords, vvv);
143+
// vector<RPNToken> tokensRPN = makeRPN(tokenList);
129144

130145
calcandprint(tokensRPN, &vvv, true);
131-
132-
} else cout << "choice not allowed" << endl;
133-
}else
134-
if (ch=="r")
135-
vvv.errorlevel = 1 - vvv.errorlevel;
146+
}
136147
else
137-
cout << "no number. retry" << endl;
148+
cout << "choice not allowed" << endl;
149+
}
150+
else if (ch == "r")
151+
vvv.errorlevel = 1 - vvv.errorlevel;
152+
else
153+
cout << "no number. retry" << endl;
138154
}
139155
};
140156

rpngenerator.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void pushToken(Token tk, vector<RPNToken> &out)
6868
out.push_back(newRPNToken);
6969
}
7070

71-
7271
vector<RPNToken> makeRPN(vector<Token> tkListIn)
7372
{
7473
// we will move the input token list into a structure TkList, equipped with member funtions
@@ -92,6 +91,14 @@ vector<RPNToken> makeRPN(vector<Token> tkListIn)
9291
return tokensout;
9392
}
9493

94+
vector<RPNToken> makeRPN(string textIn,
95+
const map<std::string, Token> &keywords,
96+
VarTable &vartabel)
97+
{
98+
vector<Token> tokens = makeTokenList(textIn, keywords, vartabel);
99+
return makeRPN(tokens);
100+
}
101+
95102
Token nextToken(TkList &tkList)
96103
{
97104
if (tkList.done())

rpngenerator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ struct RPNToken
8181
};
8282

8383
vector<RPNToken> makeRPN(vector<Token> tkListIn);
84+
vector<RPNToken> makeRPN(string textIn,
85+
const map<std::string, Token>& keywords,
86+
VarTable& vartabel);

tokengenerator.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using namespace std;
99

1010
void printtokengenerator(const vector<Token>& RPNTokens,
11-
VarTable *vartabel){
11+
VarTable& vartabel){
1212
const string ppOC[20] ={"NUM", "VAR", "MUL", "DIV", "ADD", "SUB", "PAS", "CHS", "LT", "LE", "GT", "GE", "EQ",
1313
"NE", "ASS", "QU", "COL", "PAR_L", "PAR_R", "NIL"};
1414

15-
if (vartabel->errorlevel==0) return;
15+
if (vartabel.errorlevel==0) return;
1616
cout << "opcode : ";
1717
for (const Token& element : RPNTokens)
1818
// std::cout << "\t" << element.value << " ";
@@ -61,8 +61,8 @@ void storeValueOrIndex(string param,
6161
}
6262

6363
vector<Token> makeTokenList(string textIn,
64-
const map<std::string,Token> * keywords,
65-
VarTable * vartabel){
64+
const map<std::string,Token>& keywords,
65+
VarTable& vartabel){
6666
string out = ""; // only for reporting
6767
int cursor = 0;
6868
string operand = "";
@@ -74,10 +74,10 @@ vector<Token> makeTokenList(string textIn,
7474

7575
while (cursor < textIn.size() ){
7676
if (textIn[cursor] == ' '){cursor++; continue;}
77-
if (findKeyword(*keywords, textIn.substr(cursor), keyPairFound)){
77+
if (findKeyword(keywords, textIn.substr(cursor), keyPairFound)){
7878
// now deal with the operand finished building
7979
if (operand.size() > 0) { // operand is a num or var
80-
storeValueOrIndex(operand, token, *vartabel);
80+
storeValueOrIndex(operand, token, vartabel);
8181
// if num store it in token, if var reserve an index for it in var array
8282
tokens.push_back(token);
8383
out += "\t{" + operand + "}";
@@ -96,14 +96,14 @@ vector<Token> makeTokenList(string textIn,
9696
cursor += moveCursor;
9797
}
9898
if (operand.size() > 0){ // operand is a num or var
99-
storeValueOrIndex(operand, token, *vartabel);
99+
storeValueOrIndex(operand, token, vartabel);
100100
tokens.push_back(token);
101101
out += "\t{" + operand + "} ";
102102
}
103103
tokens.push_back({OC::NIL, 0, 0, 0});
104104
out += "\t{"; out += "ETX"; out += "} ";
105105

106-
if (vartabel->errorlevel) cout << "\ntokenList\nout=\t" << out <<endl;
106+
if (vartabel.errorlevel) cout << "\ntokenList\nout=\t" << out <<endl;
107107
printtokengenerator(tokens, vartabel);
108108
return tokens;
109109
}

tokengenerator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Token {
1616
float value;} ;
1717

1818
vector<Token> makeTokenList(string textIn,
19-
const map<std::string,Token> * keywords,
20-
VarTable * vartabel);
19+
const map<std::string,Token>& keywords,
20+
VarTable& vartabel);
2121

2222
#endif // tokengenerator_H

utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef UTILS_H
33
#define UTILS_H
44

5+
//pretty print opcodes
56
const std::string ppOC[20] ={"NUM", "VAR", "MUL", "DIV", "ADD", "SUB", "PAS", "CHS", "LT", "LE", "GT", "GE", "EQ",
67
"NE", "ASS", "QU", "COL", "PAR_L", "PAR_R", "NIL"};
78

0 commit comments

Comments
 (0)