Skip to content

Commit f13cd5d

Browse files
committed
cleanup in progress
1 parent 36e9087 commit f13cd5d

File tree

6 files changed

+76
-93
lines changed

6 files changed

+76
-93
lines changed

defs.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ using namespace std;
88
// we will assign a type to each allowed character .
99
// e.g. character 'h' (ascii 104) is a LETT, character '0' (ascii 48 )is a DIGIT. character (40) is PAR_L (left parenthesis)
1010
// the following are the possible character types:
11-
enum KarType
11+
enum TokenType
1212
{ETX, LF, TAB, BLANK, HASHT, PAR_L, PAR_R, DOT, TIMES, DIV, PLUS, MINUS, LT, EQ, GT, QUEST, COLON, LETT, DIGIT, CR, OTHER, EXCLA, TEST};
1313

14+
typedef vector<TokenType> TokenTypeList;
15+
1416
//PASS 2 will scan the output of PASS1 and generate a list of tokens
1517
// precedence is according to https://en.cppreference.com/w/cpp/language/operator_precedence#cite_note-2
1618
// or https://en.wikipedia.org/wiki/Order_of_operations
17-
//enum TokenType
18-
// {COMMENT, VARI, LIT, NUM, CHS, TimesDiv, PlusMin, COMPARE, ELV_Q, ELV_C, BEXPS, BEXPE, EOT, NONE};
19-
typedef KarType TokenType;
2019

2120
// prettyPrint of TokenType:
22-
string ppTokenType[32] = {"ETX", "LF", "TAB", "BLANK", "HASHT", "PAR_L", "PAR_R", "DOT", "TIMES", "DIV", "PLUS", "MINUS", "LT", "EQ", "GT", "QUEST", "COLON", "LETT", "DIGIT", "CR", "OTHER", "EXCLA", "TEST"};
21+
string ppTokenType[32] ={"ETX", "LF", "TAB", "BLANK", "HASHT", "PAR_L", "PAR_R", "DOT", "TIMES", "DIV", "PLUS", "MINUS", "LT", "EQ", "GT", "QUEST", "COLON", "LETT", "DIGIT", "CR", "OTHER", "EXCLA", "TEST"};
2322

2423
uint cursor = 0;
2524
typedef struct {
@@ -30,8 +29,10 @@ typedef struct {
3029
int precedence;
3130
uint cursor;
3231
} Token;
32+
typedef vector<Token> TokenList;
33+
34+
TokenType kartyp[256]; // type of each character
3335

34-
KarType kartyp[256]; // type of each character
3536
string KarPP[256]; // string representation of each character
3637

3738
void initDinges(){
@@ -63,12 +64,10 @@ void initDinges(){
6364
}
6465

6566
// finally our main data structures:
66-
// 1. the input expression as a text string:
67-
string textIn= "";
6867
// 2. the output of PASS1 as a vector of symbols. it is also the input for PASS2.
6968
// symbols of type Token, the element "content" can be TODO
70-
vector<Token> symList;
69+
TokenList symList;
7170
// 3. the output of PASS2 as a vector of tokens
72-
vector<Token> symListOut;
71+
TokenList symListOut;
7372

7473
#endif // DEFS_H

interpreter.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#ifndef INTERPRETER_H
22
#define INTERPRETER_H
33

4-
float calc(vector<Token>& s){
5-
float v1;
4+
float calc(TokenList& s){
5+
float v1;
66
float v2;
77
float v3;
8-
Token last = s.back(); s.pop_back();
8+
Token last = s.back();
9+
s.pop_back();
910
if (last.type == DIGIT) return stof(last.content); // todo also LIT VARI?
1011
switch (last.arity){
1112
case 1: v1 =calc(s); return -v1;
@@ -19,8 +20,9 @@ float calc(vector<Token>& s){
1920
}
2021

2122

22-
void calcandprint(vector<Token> s) {
23-
cout << "RESULT ==> " << float(calc(s)) << endl;
23+
void calcandprint(TokenList s) {
24+
cout << "\nEVALUATION RESULT ==> " << float(calc(s)) << endl <<
25+
"_______________________________________________________________________________" << endl;
2426
}
2527

2628
#endif // INTERPRETER_H

main.cpp

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,11 @@
1010
#include <unistd.h>
1111
#include <algorithm> // count function
1212

13-
1413
#include <defs.h> // global definitions
1514
#include <utils.h> // utilities
1615

1716
using namespace std;
1817

19-
bool elaps = true;
20-
clock_t pTime, cTime = clock();
21-
void reportElapsedTime(string msg){
22-
if (elaps){
23-
cTime = clock();
24-
std::cout << "time passed: " << (cTime - pTime)/1000000.0 << " in " << msg << endl;
25-
pTime = cTime;
26-
}
27-
}
28-
2918
#include <pass1.h>
3019
#include <pass2.h>
3120
#include <interpreter.h>
@@ -36,60 +25,52 @@ int main(int argc, char *argv[])
3625
initDinges();
3726
initOperators();
3827

39-
//tests
40-
cout << "testing ... \n" << int(c) << endl;
41-
// cout << "invoke: " << op2[1](2,3) << endl;
42-
43-
// test cases: uncomment 1 of them to try
44-
45-
// textIn = "1<-2!";
46-
// textIn = "(2+3)*(1<(-2)) + (6/2)!";
47-
// textIn = "(2+3)*(1<-2) + (6/2)!"; //CALC CRASH
48-
// textIn = "1+-2!";
49-
// textIn = "6*2*4/5!";
50-
// textIn = "(6)!";
51-
// textIn = "(6)";
52-
// textIn = "5*(-1 + (6-2)*2)!";
53-
textIn = "2*3+5<1?777:((1<2 + (6/2))+17/4)";
54-
// textIn = "2*-5+17*2-(45<2)!";
55-
// textIn = "0?2:3!";
56-
// textIn = "-1+(5<4 ?4*5:(17-5)*3)!";
57-
58-
59-
cout << "textIn is: " << textIn << endl;
60-
cout << "\nPASS 1 gives the tokenized input :\n"; parse1();
28+
string text = "";
29+
30+
vector<string> inputs = {"","type it yourself",
31+
"1<-2",
32+
"1 -2",
33+
"(2+3)*(1<(-2)) + (6/2)",
34+
"(2+3)*(1<-2) + (6/2)",
35+
"1+-2",
36+
"6*2*4/5",
37+
"(6)",
38+
"(6)",
39+
"5*(-1 + (6-2)*2)",
40+
"2*3+5<1?777:((1<2 + (6/2))+17/4)",
41+
"2*-5+17*2-(45<2)",
42+
"0?2:3",
43+
"-1+(5<4 ?4*5:(17-5)*3)"};
44+
45+
uint choice = 0;
46+
string ch = "";
47+
48+
while (true) {
49+
cout << "\nchoose: -- choose 0 to exit\n"
50+
"\n1. enter expression";
51+
for (uint i = 2; i<inputs.size(); i++) cout << "\n" << i << ". " << inputs[i] ;
52+
cout << "\n\nchoice ==> ";
53+
// cin >> choice; //because cin stops at spaces, we use getline now. do not mix cin and getline!
54+
getline(cin, ch); choice = stoi(ch);
55+
if (choice > inputs.size()) {cout << "wrong choice " << choice << endl; continue;}
56+
if (choice == 0) return 0;
57+
if (choice == 1) getline(cin, text);
58+
// if (choice == 1) cin >> text;
59+
else text = inputs[choice];
60+
61+
cin.clear();
62+
cin.ignore();
63+
cout << "\ntext in: " << text << endl;
64+
cout << "\nPASS 1 gives the tokenized input :\n"; parse1(text);
6165
printPass(symList, 5);
6266
cout << "\n\nPASS 2 gives the RPN form of the expression"; parse2();
6367
printPass(symListOut, 5);
64-
6568
calcandprint(symListOut);
66-
67-
cout << "\n\nthe end"<< endl;
69+
};
6870

6971
return 0;
7072

7173
//end TEST
7274

73-
// the following is not used yet
74-
int choice = 0;
75-
cout << "choose:\n1. create the 4 world part files\n2. create srtmfiles\n3. create both\n4. reduce files to Res400\n5. create both and reduce files to Res400\n";
76-
while ((choice==0) | (choice>3)) {
77-
cin >> choice;
78-
switch (choice) {
79-
case 1:
80-
break;
81-
case 2:
82-
break;
83-
case 3:
84-
break;
85-
default:
86-
cout << "wrong choice " << choice << endl;
87-
break;
88-
};
89-
cin.clear();
90-
cin.ignore();
91-
};
92-
cout << "optionChosen final = " << choice << endl;
93-
9475
return 0;
9576
}

pass1.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
bool isFirstSymbol=true;
77
char c;
88

9-
Token makeSymbol(vector<TokenType> expected){
9+
Token makeSymbol(string textIn, TokenTypeList expected){
1010
string s = "";
1111
if (cursor >= textIn.length()) return Token{ETX,"",-1,-1,-1};
1212
c = textIn[cursor];
1313
if (isaC(c, {BLANK, TAB})) {
1414
cursor++;
15-
return makeSymbol({}); //makesymbol advances the cursor
15+
return makeSymbol(textIn, {}); //makesymbol advances the cursor
1616
}
1717

1818
symIn.type = kartyp[int(c)];
@@ -21,7 +21,7 @@ Token makeSymbol(vector<TokenType> expected){
2121
symIn.arity = -1;
2222
symIn.precedence = -1;
2323
symIn.cursor = cursor; // not used for now
24-
KarType karTypeIn = kartyp[int(c)];
24+
TokenType karTypeIn = kartyp[int(c)];
2525
switch (karTypeIn) {
2626
case LETT: case DIGIT: case DOT: //c is a KAR, so we're building a string
2727
s = "";
@@ -75,13 +75,15 @@ Token makeSymbol(vector<TokenType> expected){
7575
isFirstSymbol = false;
7676
cursor++;
7777
if (expected.empty()) return symIn; //default, we do not complain
78-
if (count(expected.begin(), expected.end(), symIn.type) > 0) return symIn; //check on expected char is ok, no complaints
78+
if (count(expected.begin(), expected.end(), symIn.type) > 0) return symIn; //check on expected char is ok, no complaints
7979
errorsPresent = true;
80-
// throw IllegalArgumentException(
81-
// "SEPARATOR in line at cursor " +
80+
string errors = "SEPARATOR in line at cursor " + to_string(cursor) ;
81+
throw invalid_argument(errors
82+
// "SEPARATOR in line at cursor " &
8283
// "$cursor < ${textIn.substring(0, cursor)} > \n" +
8384
// " char=$c, symbol=${symIn.content} ==> < ${expected.contentToString()} >\n"
84-
// );
85+
// adf cpp problem
86+
);
8587
return symIn;
8688
}
8789

@@ -91,12 +93,12 @@ void clear1() {
9193
symList.clear();
9294
}
9395

94-
vector<Token> parse1(){
96+
TokenList parse1(string textIn){
9597
clear1();
9698
cursor = 0;
9799
errorsPresent = false;
98100
do {
99-
symIn = makeSymbol({});
101+
symIn = makeSymbol(textIn, {});
100102
symList.push_back(symIn);
101103
} while (symIn.type != ETX);
102104
return symList;

pass2.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ void push(Token sym) {
88
textOut += sym.content;
99
}
1010

11-
Token nextSymbol(string from, vector<TokenType> expected){
12-
// param from: the cpp function from where nextsymbol is called. used for error reporting
13-
if (symList.empty()) throw invalid_argument("from" + from + ": symbols missing!");
11+
Token nextSymbol(string from, TokenTypeList expected){
12+
// param from: the cpp function from where nextsymbol is called. used for error reporting
13+
// expected: the list of possible TokenTypes. it is filled in by the calling function
14+
string ppexpectedList = "";
15+
for (TokenType t: expected) ppexpectedList += " " + ppTokenType[t];
16+
if (symList.empty()) throw invalid_argument("from " + from + ": symbols missing!");
1417
Token next = symList.back(); symList.pop_back();
1518
if (expected.empty()) return next;
1619
if (count(expected.begin(), expected.end(), next.type) > 0) return next;
17-
errorsPresent = true;
1820
throw invalid_argument(
1921
"from " + from + " at cursor=" + to_string(next.cursor) + " symbol=" + next.content + ", " + ppTokenType[next.type] +
20-
"} NOT IN " + "expected" + "}" // "expected" should be the list of expected types
22+
" is not one of {" + ppexpectedList + "}" // "expected" should be the list of expected types
2123
);
2224
return next;
2325
}
@@ -113,22 +115,19 @@ void expr13(){
113115
void clear2() {
114116
symListOut.clear();
115117
cursor = 0;
116-
errorsPresent = false;
117118
textOut = "";
118119
}
119120

120-
vector<Token> parse2(){
121+
TokenList parse2(){
121122
reverse(symList.begin(), symList.end());
122123
clear2();
123124
cursor = 0;
124-
errorsPresent = false;
125125
try {
126126
symIn = nextSymbol("parse", {});
127127
expr();
128128
} catch (const exception& e) {
129129
cout << "\n\n!!! PARSE ERROR:" << e.what() << endl<< endl;;
130130
};
131-
if (!errorsPresent) cout << endl;
132131
return symListOut;
133132
};
134133

utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
Token symIn = {OTHER, "", -1, -1, 0, 0}; // used in both pass1 and pass2
99
bool errorsPresent = false;
1010

11-
bool isa(Token token, vector<TokenType> allowedTypes){
11+
bool isa(Token token, TokenTypeList allowedTypes){
1212
// check if the symbol sym is one of the symbols in the list op
1313
return(count(allowedTypes.begin(), allowedTypes.end(), token.type) > 0);
1414
}
1515

16-
bool isaC(char Kar, vector<KarType> allowedTypes){
16+
bool isaC(char Kar, TokenTypeList allowedTypes){
1717
// int found = find(allowedTypes.begin(), allowedTypes.end(), int(Kar));
1818
int cnt = count(allowedTypes.begin(), allowedTypes.end(), kartyp[int(Kar)]);
1919
return(cnt > 0);
@@ -47,7 +47,7 @@ void initOperators(){
4747
}
4848

4949
// some printing stuff
50-
void printPass(vector<Token> symList, int tab){
50+
void printPass(TokenList symList, int tab){
5151
cout << "type : ";
5252
for (Token element : symList)
5353
cout << setw(tab) << ppTokenType[element.type] << " ";

0 commit comments

Comments
 (0)