Skip to content

Commit fe3ced3

Browse files
#TDP-8 Modify
1. Modify method getCode 2. add functional if cycle for read file
1 parent cd8096e commit fe3ced3

3 files changed

Lines changed: 77 additions & 28 deletions

File tree

MethodsDevelopmentTranslator.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,38 @@ int main()
4444
getline(fileC, stringC);
4545
for (int i = 0; i < stringC.length()-1; i++)
4646
{
47+
if (i != 0 && stringC[i] == '*' && isLetter((int)stringC[i - 1]) == true)
48+
fileAnalysis << getCodeWord(stringC[i-1]+"*");
4749
if (stringC[i] != ' ')
4850
{
49-
if (isLetter(stringC[i]) == true && (isLetter(stringC[i+1])==false || isDigit(stringC[i+1])==false))
51+
if (isLetter((int)stringC[i]) == true && (isLetter((int)stringC[i+1])==false || isDigit((int)stringC[i+1])==false))
5052
{
51-
53+
temp += stringC[i];
54+
fileAnalysis << getCodeWord(temp) << " ";
55+
temp = "";
56+
continue;
5257
}
5358
else
5459
{
55-
temp += stringC[i];
60+
if(stringC[0] == '#')
61+
temp += stringC[i];
5662
}
63+
temp += stringC[i];
5764
}
5865
else
5966
{
60-
string out = getServiceWordCode(temp);
61-
67+
if (stringC[i] == ' ' && temp == "\0")
68+
continue;
69+
else
70+
if (stringC[i] == ' ')
71+
{
72+
fileAnalysis << getCodeWord(temp) << " ";
73+
temp = "";
74+
}
6275
}
63-
76+
6477
}
78+
fileAnalysis << "\n";
6579
}
6680
}
6781

@@ -72,6 +86,7 @@ int main()
7286
}
7387

7488
fileC.close();
89+
fileAnalysis.close();
7590

7691
return 0;
7792
}

function.cpp

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
#include "table.h"
22
#include "function.h"
33

4-
bool isLetter(char elem)
4+
bool isLetter(int elem)
55
{
6-
return (elem >= '65' && elem <= '90') || (elem >= '97' && elem <= '122') ? true : false;
6+
return (elem >= 65 && elem <= 90) || (elem >= 97 && elem <= 122) ? true : false;
77
}
88

9-
bool isDigit(char elem)
9+
bool isDigit(int elem)
1010
{
11-
return elem >= '48' && elem <= '57' ? true : false;
11+
return elem >= 48 && elem <= 57 ? true : false;
1212
}
1313

1414
bool isNumber(string num)
1515
{
1616
for (int i = 0; i < num.length(); i++)
17-
if (isDigit(num[i]) == false)
17+
if (isDigit((int)num[i]) == false)
1818
return false;
1919
return true;
2020
}
2121

22+
bool isLibrary_header(string word)
23+
{
24+
return (int)word[0] == 34 && (int)word[word.length() - 1] == 34 && (int)word[word.length() - 2] == 104 && (int)word[word.length() - 3] == 46 ? true : false;
25+
}
26+
2227
string getServiceWordCode(string str)
2328
{
2429
for (int i = 0; i < SIZE_serviceWord; i++)
@@ -67,22 +72,43 @@ string getSymbolsConstCode(string str)
6772
return "\0";
6873
}
6974

75+
void addCode(string str, map<string, string> table, int numTable)
76+
{
77+
int indexCode = 0;
78+
for (const auto& word : table)
79+
{
80+
indexCode++;
81+
}
82+
indexCode++;
83+
if(numTable==1)
84+
table.insert(pair<string, string>(str, "I" + indexCode));
85+
if(numTable==2)
86+
table.insert(pair<string, string>(str, "N" + indexCode));
87+
if(numTable==3)
88+
table.insert(pair<string, string>(str, "C" + indexCode));
89+
}
90+
91+
7092
string getCodeWordLength_1(string word)
7193
{
7294
string code = getOperationsCode(word);
7395
if (code == "\0")
7496
code = getSeparatorsCode(word);
7597
if (code=="\0")
7698
{
77-
if (isDigit(word[0]) == true)
99+
if (isDigit((int)word[0]) == true)
78100
{
79101
code = getNumberConstCode(word);
80-
//TODO: Add constant
102+
if(code =="\0")
103+
addCode(word, numberConst, 2);
104+
return getNumberConstCode(word);
81105
}
82-
if (isLetter(word[0]) == true)
106+
if (isLetter((int)word[0]) == true)
83107
{
84108
code = getIdentifierCode(word);
85-
//TODO: Add word
109+
if(code=="\0")
110+
addCode(word,identifier,1);
111+
return getIdentifierCode(word);
86112
}
87113
}
88114
else
@@ -91,8 +117,11 @@ string getCodeWordLength_1(string word)
91117
}
92118
}
93119

120+
94121
string getCodeWordLengthGreaterOne(string word)
95122
{
123+
if (isLetter((int)word[0]) == true && word[1] == '*')
124+
return getSeparatorsCode(word);
96125
string code = getServiceWordCode(word);
97126
if (code == "\0")
98127
code = getOperationsCode(word);
@@ -101,24 +130,31 @@ string getCodeWordLengthGreaterOne(string word)
101130
if (isNumber(word) == true)
102131
{
103132
code = getNumberConstCode(word);
133+
if(code=="\0")
134+
addCode(word, numberConst, 2);
135+
return getNumberConstCode(word);
104136
}
105137
else
106138
{
107-
if (word[0] == '34')
139+
if ((int)word[0] == 34)
108140
{
109-
code = getSymbolsConstCode(word);
110-
if (code == "\0")
141+
if (isLibrary_header(word) == false)
111142
{
112-
//TODO:ADD const sym
143+
code = getSymbolsConstCode(word);
144+
if (code == "\0")
145+
addCode(word, symbolsConst, 3);
146+
return getSymbolsConstCode(word);
113147
}
148+
else
149+
goto addCodeIdentifier;
114150
}
115151
else
116152
{
153+
addCodeIdentifier:
117154
code = getIdentifierCode(word);
118155
if (code == "\0")
119-
{
120-
//TODO:ADD id
121-
}
156+
addCode(word, identifier, 1);
157+
return getIdentifierCode(word);
122158
}
123159

124160
}
@@ -130,11 +166,7 @@ string getCodeWordLengthGreaterOne(string word)
130166
string getCodeWord(string word)
131167
{
132168
if (word.length() == 1)
133-
{
134169
return getCodeWordLength_1(word);
135-
}
136170
else
137-
{
138171
return getCodeWordLengthGreaterOne(word);
139-
}
140172
}

function.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#define FUNCTION_H
44
#include "include.h"
55

6-
bool isDigit(char);
7-
bool isLetter(char);
6+
bool isDigit(int);
7+
bool isLetter(int);
8+
bool isLibrary_header(string);
89
string getServiceWordCode(string);
910
string getOperationsCode(string str);
1011
string getSeparatorsCode(string str);
1112
string getIdentifierCode(string str);
1213
string getNumberConstCode(string str);
1314
string getSymbolsConstCode(string str);
15+
string getCodeWord(string );
1416
#endif

0 commit comments

Comments
 (0)