Skip to content

Commit f5c2345

Browse files
committed
PASS1 and PASS2
1 parent 84c8f70 commit f5c2345

6 files changed

Lines changed: 182 additions & 49 deletions

File tree

defs.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ void initDinges(){
7777
kartyp[32] = BLANK; KarPP[32] = "BLANK";
7878
kartyp[33] = EXCLA; KarPP[33] = "!END!";
7979
kartyp[35] = HASHT; KarPP[35] = "HASHT";
80-
kartyp[40] = PAR_L; KarPP[40] = "PAR_L";
81-
kartyp[41] = PAR_R; KarPP[41] = "PAR_R";
80+
kartyp[40] = PAR_L; KarPP[40] = "(";
81+
kartyp[41] = PAR_R; KarPP[41] = ")";
8282
kartyp[42] = TIMES; KarPP[42] = "*";
8383
kartyp[43] = PLUS; KarPP[43] = "+";
8484
kartyp[45] = MINUS; KarPP[45] = "-";
@@ -91,24 +91,16 @@ void initDinges(){
9191
kartyp[63] = QUEST; KarPP[63] = "?";
9292
}
9393

94+
// finally our maain data structures:
95+
// 1. the input expression as a text string:
96+
string textIn= "";
97+
// 2. the output of PASS1 as a vector of symbols. it is also the input for PASS2.
98+
// symbols of type Token, the element "content" can be TODO
99+
vector<Token> symList;
100+
// 3. the output of PASS2 as a vector of tokens
101+
vector<Token> symListOut;
94102

95-
bool isa(Token token, vector<TokenType> allowedTypes){
96-
// check if the symbol sym is one of the symbols in the list op
97-
return(count(allowedTypes.begin(), allowedTypes.end(), token.type) > 0);
98-
};
99103

100-
bool isaC(char Kar, vector<KarType> allowedTypes){
101-
// int found = find(allowedTypes.begin(), allowedTypes.end(), int(Kar));
102-
int cnt = count(allowedTypes.begin(), allowedTypes.end(), kartyp[int(Kar)]);
103-
return(cnt > 0);
104-
};
105-
106-
// some operator functions
107-
float add(float x, float y){return (x+y);}
108-
float invoke (float x, float y, float (*function) (float, float)){return function(x,y);}
109-
110-
void reportln(vector<Token> list, int level, int padding){}
111-
void reportln(string s, int level){}
112104

113105

114106
#endif // DEFS_H

expressionparser.pro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ SOURCES += main.cpp
1313

1414
HEADERS += \
1515
defs.h \
16-
pass1.h
16+
pass1.h \
17+
pass2.h \
18+
utils.h
1719

main.cpp

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

13-
#include <defs.h>;
1413

15-
using namespace std;
14+
#include <defs.h>; // global definitions
15+
#include <utils.h>; // utilities
1616

17-
typedef int FileNumber;
17+
using namespace std;
1818

1919
bool elaps = true;
2020
clock_t pTime, cTime = clock();
@@ -28,17 +28,33 @@ void reportElapsedTime(string msg){
2828

2929
#include <pass1.h>
3030

31+
#include <pass2.h>
32+
33+
34+
3135
Token tt = {VARI, 0, "haha", 0}; //test
3236
int main(int argc, char *argv[])
3337
{
3438
(void)(argc);(void)(argv);
3539
initDinges();
3640

3741
//tests
38-
vector<Token> pass1Out;
39-
char c = '1';
40-
cout << "code c =" << int(c) << endl;
42+
cout << "testing ... \n" << int(c) << endl;
43+
44+
45+
textIn = "2+(12*(3<4 ?(4/2)*5:(17-5)*3))!";
46+
cout << "textIn is: " << textIn << endl;
47+
cout << "\nPASS 1 gives : "; parse1();
48+
printVector(symList);
49+
cout << "\nPASS 2 says : "; parse2();
50+
printVector(symListOut);
51+
cout << "\n\nthe end"<< endl;
4152

53+
return 0;
54+
55+
//end TEST
56+
57+
// the following is not used yet
4258
int choice = 0;
4359
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";
4460
while ((choice==0) | (choice>3)) {
@@ -50,17 +66,6 @@ int main(int argc, char *argv[])
5066
break;
5167
case 3:
5268
break;
53-
case 4:
54-
// cout << invoke(BLANK, 3, &add) << endl;
55-
cout << "makesymbol " << makeSymbol({VARI, LIT}).type << endl;
56-
break;
57-
case 5:
58-
cout << "token is : " << isa(tt, {CHS, ELV_C }) << endl;
59-
break;
60-
case 6:
61-
pass1Out = parse("1+2!");
62-
cout << "parser is : " << pass1Out[0].content << " en " << pass1Out[1].content << endl;
63-
break;
6469
default:
6570
cout << "wrong choice " << choice << endl;
6671
break;

pass1.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
#define PASS1_H
33
#include <defs.h>
44

5-
Token symIn = {NONE, 0, "",0};
6-
bool isFirstSymbol=true;
7-
int cursor = 0;
8-
string textIn= "";
95

10-
vector<Token> symList;
11-
bool errorsPresent = false;
6+
bool isFirstSymbol=true;
127
char c= '\u0000'; //???
138

149

@@ -94,27 +89,25 @@ Token makeSymbol(vector<TokenType> expected){
9489
return symIn;
9590
}
9691

97-
void clear() {
92+
void clear1() {
9893
cursor = 0;
9994
errorsPresent = false;
100-
textIn = "";
10195
symList.clear();
10296
}
10397

104-
vector<Token> parse(string s){
105-
clear();
106-
textIn = s;
98+
vector<Token> parse1(){
99+
clear1();
107100
cursor = 0;
108101
errorsPresent = false;
109102
reportln("textIn = $textIn", 0);
110103
do {
111104
symIn = makeSymbol({});
112105
if (isa(symIn, {ELV_C})) { // substitute '?(' for '?'
113-
symList.push_back({BEXPE, 17, "PAR_R", cursor}); //todo pretty print
106+
symList.push_back({BEXPE, 17, ")", cursor}); //todo pretty print
114107
};
115108
symList.push_back({symIn.type, 17, symIn.content, cursor});
116109
if (isa(symIn, {ELV_Q})) { // substitute '?(' for '?'
117-
symList.push_back({BEXPS, 17, "PAR_L", cursor}); //todo
110+
symList.push_back({BEXPS, 17, "(", cursor}); //todo
118111
};
119112
} while (symIn.type != EOT);
120113

pass2.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#ifndef PASS2_H
2+
#define PASS2_H
3+
4+
5+
string textOut= "";
6+
7+
void push(Token sym) {
8+
symListOut.push_back(sym);
9+
textOut += sym.content;
10+
reportln("push >>> " + sym.content, 2);
11+
}
12+
13+
Token nextSymbol(string from, vector<TokenType> expected){
14+
if (symList.empty())
15+
throw invalid_argument("from <$from>: symbols missing!");
16+
Token next = symList.back();
17+
symList.pop_back();
18+
if (expected.empty()) return next;
19+
if (count(expected.begin(), expected.end(), next.type) > 0) return next;
20+
errorsPresent = true;
21+
throw invalid_argument(
22+
"from <$from> at cursor=${next.cursor} symbol={${next.content}, ${next.typ}} NOT IN ${expected.contentToString()}"
23+
);
24+
return next;
25+
}
26+
void bExpression();
27+
28+
void factor() {
29+
//next symbol if success
30+
switch(symIn.type) {
31+
case VARI: case LIT:
32+
push(symIn); // push???
33+
symIn = nextSymbol("factor", {TimesDiv, PlusMin, BEXPE, ELV_Q, EOT});
34+
break;
35+
default: bExpression();
36+
break;
37+
}
38+
}
39+
40+
41+
void term() {
42+
if (isa(symIn, {CHS})) {
43+
Token save = symIn;
44+
symIn = nextSymbol("term", {VARI});
45+
factor();
46+
int code = 177;
47+
if (save.content == "-") push(Token({CHS, 0, "${code.toChar()}", symIn.cursor})); // push???
48+
}
49+
factor();
50+
while (isa(symIn, {TimesDiv})) {
51+
Token save = symIn;
52+
symIn = nextSymbol("term", {VARI, BEXPS});
53+
factor();
54+
push(save); // push ???
55+
}
56+
}
57+
58+
void expression(){
59+
term();
60+
while (isa(symIn, {PlusMin, ELV_Q, ELV_C})) {
61+
Token save = symIn;
62+
symIn = nextSymbol("expression", {VARI, BEXPS});
63+
term();
64+
push(save); //???
65+
}
66+
};
67+
68+
69+
void bExpression() {
70+
if (isa(symIn, {BEXPS})) {
71+
symIn = nextSymbol("bExpression", {VARI, BEXPS, CHS});
72+
expression();
73+
symIn = nextSymbol("bExpression", {VARI, TimesDiv, PlusMin, BEXPS, BEXPE, ELV_Q, ELV_C, EOT});
74+
}
75+
};
76+
77+
78+
void clear2() {
79+
symListOut.clear();
80+
cursor = 0;
81+
errorsPresent = false;
82+
textOut = "";
83+
}
84+
85+
vector<Token> parse2(){
86+
reverse(symList.begin(), symList.end());
87+
clear2();
88+
cursor = 0;
89+
errorsPresent = false;
90+
try {
91+
symIn = nextSymbol("parse", {});
92+
expression();
93+
} catch (invalid_argument) {
94+
cout << "PARSE ERROR: ${e.message}" << endl;
95+
};
96+
if (!errorsPresent)
97+
cout << "expression parser PASS1 ended succesfully\n ";
98+
return symListOut;
99+
};
100+
101+
102+
103+
#endif // PASS2_H

utils.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef UTILS_H
2+
#define UTILS_H
3+
4+
int cursor = 0;
5+
Token symIn = {NONE, 0, "", 0}; // used in both pass1 and pass2
6+
bool errorsPresent = false;
7+
8+
bool isa(Token token, vector<TokenType> allowedTypes){
9+
// check if the symbol sym is one of the symbols in the list op
10+
return(count(allowedTypes.begin(), allowedTypes.end(), token.type) > 0);
11+
};
12+
13+
bool isaC(char Kar, vector<KarType> allowedTypes){
14+
// int found = find(allowedTypes.begin(), allowedTypes.end(), int(Kar));
15+
int cnt = count(allowedTypes.begin(), allowedTypes.end(), kartyp[int(Kar)]);
16+
return(cnt > 0);
17+
};
18+
19+
// some operator functions
20+
float add(float x, float y){return (x+y);}
21+
float invoke (float x, float y, float (*function) (float, float)){return function(x,y);}
22+
// TODO to be completed for the interpreter
23+
24+
25+
// some printing stuff
26+
void printVector(vector<Token> symList){
27+
for (Token element : symList)
28+
cout << element.content << " ";
29+
cout << endl;
30+
// for (Token element : symList)
31+
// cout << element.type << " ";
32+
// cout << endl;
33+
}
34+
35+
void reportln(vector<Token> list, int level, int padding){};
36+
void reportln(string s, int level){};
37+
38+
#endif // UTILS_H

0 commit comments

Comments
 (0)