Skip to content

Commit d7ceba7

Browse files
committed
initial version of migration from kotlin to cpp
0 parents  commit d7ceba7

8 files changed

Lines changed: 408 additions & 0 deletions

File tree

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# In repository we don't need to have:
2+
3+
#custom ADF
4+
files*.conf
5+
6+
#build directories
7+
[Bb]uild*\
8+
[Bb]uild*/
9+
10+
11+
# C++ objects and libs
12+
*.slo
13+
*.lo
14+
*.o
15+
*.a
16+
*.la
17+
*.lai
18+
*.so
19+
*.dll
20+
*.dylib
21+
22+
# Qt-es
23+
24+
/.qmake.cache
25+
/.qmake.stash
26+
*.pro.user
27+
*.pro.user.*
28+
*.qbs.user
29+
*.qbs.user.*
30+
*.moc
31+
moc_*.cpp
32+
qrc_*.cpp
33+
ui_*.h
34+
Makefile*
35+
*build-*
36+
37+
# QtCreator
38+
39+
*.autosave
40+
41+
#QtCtreator Qml
42+
*.qmlproject.user
43+
*.qmlproject.user.*
44+
45+
#QtCtreator CMake
46+
CMakeLists.txt.user
47+
48+
#data archives
49+
*.zip
50+
*.rar

defs.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#ifndef DEFS_H
2+
#define DEFS_H
3+
#include <string>
4+
5+
using namespace std;
6+
// PASS 1 will scan the input test and generate a list of symbols (see PASS1)
7+
// we will assign a type to each allowed character .
8+
// e.g. character 'h' (ascii 104) is a LETT, character '0' (ascii 48 )is a DIGIT. character (40) is PAR_L (left parenthesis)
9+
// the following are the possible character types:
10+
enum KarType {ETX,
11+
LF,
12+
TAB,
13+
BLANK,
14+
HASHT,
15+
PAR_L,
16+
PAR_R,
17+
DOT,
18+
TIMES,
19+
DIV,
20+
PLUS,
21+
MINUS,
22+
EQ,
23+
GT,
24+
LT,
25+
COLON,
26+
QUEST, // we ignore this operator, becase ':' takes care of ternary, else QUESTION("[?]"),
27+
LETT,
28+
DIGIT,
29+
CR,
30+
OTHER,
31+
EXCLA,
32+
TEST};
33+
34+
//PASS 2 will scan the output of PASS1 and generate a list of symbols
35+
enum TokenType {
36+
// https://en.cppreference.com/w/cpp/language/operator_precedence#cite_note-2
37+
// of https://en.wikipedia.org/wiki/Order_of_operations
38+
COMMENT,
39+
VARI,
40+
LIT, //literal
41+
CHS, // CHS "change sign " was OP_3
42+
TimesDiv, // * or /, according to precedence order - was OP5
43+
PlusMin, // + or - - was OP6
44+
OP_16, // like elvis
45+
ELV_Q,
46+
ELV_C,
47+
BEXPS,
48+
BEXPE,
49+
EOT,
50+
NONE} ;
51+
52+
typedef struct {
53+
TokenType type;
54+
int arity;
55+
string content;
56+
int cursor;
57+
} Token;
58+
59+
KarType kartyp[256]; // type of each character
60+
string KarPP[256]; // string representation of each character
61+
62+
//KarPP[1] = 2;
63+
64+
void initDinges(){
65+
for (int8_t j= 1; j<256; j++) {kartyp[j] = OTHER; KarPP[j] = "OTHER";};
66+
for (int8_t j= 48; j<58; j++) {kartyp[j] = DIGIT; KarPP[j] = "D";};
67+
for (int8_t j= 65; j<91; j++) {kartyp[j] = LETT; KarPP[j] = "L";};
68+
for (int8_t j= 97; j<123; j++) {kartyp[j] = LETT; KarPP[j] = "L";};
69+
70+
kartyp[95] = LETT; KarPP[95] = "L";
71+
kartyp[0] = ETX; KarPP[0] = "ETX";
72+
kartyp[10] = LF; KarPP[10] = "LF";
73+
kartyp[11] = TAB; KarPP[11] = "TAB";
74+
kartyp[13] = CR; KarPP[13] = "CR";
75+
kartyp[32] = BLANK; KarPP[32] = "BLANK";
76+
kartyp[33] = EXCLA; KarPP[33] = "!END!";
77+
kartyp[35] = HASHT; KarPP[35] = "HASHT";
78+
kartyp[40] = PAR_L; KarPP[40] = "PAR_L";
79+
kartyp[41] = PAR_R; KarPP[41] = "PAR_R";
80+
kartyp[42] = TIMES; KarPP[42] = "*";
81+
kartyp[43] = PLUS; KarPP[43] = "+";
82+
kartyp[45] = MINUS; KarPP[45] = "-";
83+
kartyp[46] = DOT; KarPP[46] = ".";
84+
kartyp[47] = DIV; KarPP[47] = "/";
85+
kartyp[58] = COLON; KarPP[58] = ":";
86+
kartyp[60] = LT; KarPP[60] = "<";
87+
kartyp[61] = EQ; KarPP[61] = "=";
88+
kartyp[62] = GT; KarPP[62] = ">";
89+
kartyp[63] = QUEST; KarPP[63] = "?";
90+
}
91+
92+
93+
bool isa(Token token, vector<TokenType> allowedTypes){
94+
// check if the symbol sym is one of the symbols in the list op
95+
return(count(allowedTypes.begin(), allowedTypes.end(), token.type) > 0);
96+
};
97+
98+
bool isaC(char Kar, vector<KarType> allowedTypes){
99+
return(count(allowedTypes.begin(), allowedTypes.end(), int(Kar)) > 0);
100+
};
101+
102+
// some operator functions
103+
float add(float x, float y){return (x+y);}
104+
float invoke (float x, float y, float (*function) (float, float)){return function(x,y);}
105+
106+
107+
108+
109+
#endif // DEFS_H

expressionparser.pro

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
QT += core
2+
QT -= gui
3+
4+
CONFIG += c++11
5+
6+
TARGET = expressionparser
7+
CONFIG += console
8+
CONFIG -= app_bundle
9+
10+
TEMPLATE = app
11+
12+
SOURCES += main.cpp \
13+
kars.cpp
14+
15+
HEADERS += \
16+
defs.h \
17+
kars.h \
18+
pass1.h
19+

kars.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "kars.h"
2+
3+
Kars::Kars()
4+
{
5+
6+
}

kars.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef KARS_H
2+
#define KARS_H
3+
4+
5+
class Kars
6+
{
7+
public:
8+
Kars();
9+
10+
};
11+
12+
#endif // KARS_H

main.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
#include <string>
3+
#include <iostream>
4+
#include <fstream>
5+
#include <limits>
6+
#include <sstream>
7+
#include <vector>
8+
#include <ctime>
9+
#include <sys/stat.h>
10+
#include <unistd.h>
11+
#include <algorithm> // count function
12+
13+
#include <defs.h>;
14+
15+
using namespace std;
16+
17+
typedef int FileNumber;
18+
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+
29+
#include <pass1.h>
30+
31+
Token tt = {VARI, 0, "haha", 0}; //test
32+
int main(int argc, char *argv[])
33+
{
34+
(void)(argc);(void)(argv);
35+
36+
//tests
37+
38+
39+
int choice = 0;
40+
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";
41+
while ((choice==0) | (choice>3)) {
42+
cin >> choice;
43+
switch (choice) {
44+
case 1:
45+
break;
46+
case 2:
47+
break;
48+
case 3:
49+
break;
50+
case 4:
51+
// cout << invoke(BLANK, 3, &add) << endl;
52+
cout << "makesymbol " << makeSymbol({VARI, LIT}).type << endl;
53+
break;
54+
case 5:
55+
cout << "token is : " << isa(tt, {CHS, ELV_C }) << endl;
56+
break;
57+
default:
58+
cout << "wrong choice " << choice << endl;
59+
break;
60+
};
61+
cin.clear();
62+
cin.ignore();
63+
};
64+
cout << "optionChosen final = " << choice << endl;
65+
66+
return 0;
67+
}

pass1.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#ifndef PASS1_H
2+
#define PASS1_H
3+
#include <defs.h>
4+
5+
Token symIn = {NONE, 0, "",0};
6+
bool isFirstSymbol=true;
7+
int cursor = 0;
8+
string textIn= "";
9+
10+
vector<Token> symList;
11+
bool errorsPresent = false;
12+
char c= '\u0000'; //???
13+
14+
15+
Token makeSymbol(vector<TokenType> expected){
16+
string s = "";
17+
if (cursor >= textIn.length()) return symIn;
18+
c = textIn[cursor];
19+
if (isaC(c, {BLANK, TAB})) {
20+
cursor++;
21+
return makeSymbol({}); //makesymbol advances the cursor
22+
}
23+
int tup = kartyp[int(c)];
24+
symIn.content = KarPP[int(c)]; // pretty print
25+
switch (tup) {
26+
case LETT: case DIGIT: case DOT: //c is a KAR, so we're building a string
27+
s = "";
28+
while (isaC(c, {LETT, DIGIT, DOT})) {
29+
s += c;
30+
cursor++;
31+
c = textIn[cursor];
32+
};
33+
symIn.type = VARI;
34+
symIn.content = s;
35+
cursor--;
36+
break;
37+
case EXCLA: case ETX: case LF: case CR: case OTHER:
38+
symIn.type = EOT;
39+
return symIn;
40+
break;
41+
case PAR_L:
42+
symIn.type = BEXPS; //the special character becomes the typ
43+
break;
44+
case PAR_R:
45+
symIn.type = BEXPE; //the special character becomes the typ
46+
break;
47+
case TIMES: case DIV: case GT: case LT: case EQ:
48+
if (! (isFirstSymbol)) { //we have a separator
49+
symIn.type = TimesDiv; //the special character becomes the typ
50+
}
51+
break;
52+
case QUEST:
53+
symIn.type = ELV_Q; //the special character becomes the typ
54+
break;
55+
case COLON:
56+
symIn.type = ELV_C; //the special character becomes the typ
57+
break;
58+
case PLUS: case MINUS:
59+
if (isFirstSymbol) {
60+
symIn.type = CHS;
61+
} else if (isaC(textIn[cursor - 1], {TIMES, DIV, PLUS, MINUS, PAR_L, QUEST})) {
62+
// we are not adding to the previous, this is a unary operator
63+
symIn.type = CHS;
64+
} else symIn.type = PlusMin;
65+
break;
66+
case TEST: //c is a KAR, so we're building a string
67+
// var s = "";
68+
// s += c;
69+
// cursor++
70+
// c = textIn[cursor]
71+
// while (isaC(c, LETT, DIGIT)) {
72+
// s += c;
73+
// cursor++
74+
// c = textIn[cursor]
75+
// }
76+
// symIn.typ = VARI;
77+
// symIn.content = s
78+
break;
79+
default:
80+
symIn.type = NONE; //the special character becomes the typ
81+
break;
82+
}
83+
isFirstSymbol = false;
84+
cursor++;
85+
if (expected.empty()) return symIn; //default, we do not complain
86+
if (count(expected.begin(), expected.end(), symIn.type) > 0) return symIn; //check on expected char is ok, no complaints
87+
errorsPresent = true;
88+
// throw IllegalArgumentException(
89+
// "SEPARATOR in line at cursor " +
90+
// "$cursor < ${textIn.substring(0, cursor)} > \n" +
91+
// " char=$c, symbol=${symIn.content} ==> < ${expected.contentToString()} >\n"
92+
// );
93+
return symIn;
94+
}
95+
96+
97+
#endif // PASS1_H

0 commit comments

Comments
 (0)