-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp.save-failed
235 lines (210 loc) · 5.3 KB
/
main.cpp.save-failed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
class Node
{
public:
string value;
Node *child;
Node *parent;
private:
};
Node packageNode;
Node importNode;
string x;
string result;
int counter;
string leaf;
int parsePackage();
int parseImport();
int parseFunc();
int parseBody();
int parseConst();
int parseVar();
int parseVar()
{
int varCounter = 1+parseBody();
while(iswspace(x[varCounter]) || x[varCounter] == '{' || x[varCounter] == '}')
{
varCounter++;
}
if(isalnum(x[varCounter]))
{
int q = varCounter;
string varName;
cout << "I am printing from inside 'parseVar()'"<< endl;
for(int i = q; !iswspace(x[i]) && x[i] != '}'; i++)
{
varName = varName + x[i];
counter = i;
}
cout << "var--> " << varName << endl;
leaf = "var";
return counter;
}
else
{
cout << "Error: invalid syntax"<< endl;
}
return 0;
}
int parseConst()
{
int constCounter = 1+parseBody();
while(iswspace(x[constCounter]) || x[constCounter] == '{' || x[constCounter] == '}')
{
constCounter++;
}
if(isalnum(x[constCounter]) && x.substr(constCounter, 5) == "const")
{
int n = constCounter;
string constName;
cout << "I am printing from inside 'parseConst()'"<< endl;
for(int i = n; !iswspace(x[i]) && x[i] != '}'; i++)
{
constName = constName + x[i];
counter = i;
}
cout << "const--> " << constName << endl;
leaf = "const";
return counter;
}
else
{
parseVar();
cout << "Error: invalid syntax"<< endl;
}
return 0;
}
int parseBody()
{
int bodyCounter = 1 + parseFunc();
while(iswspace(x[bodyCounter]) || x[bodyCounter] == '{' || x[bodyCounter] == '}')
{
bodyCounter++;
}
cout << bodyCounter << endl;
if(x.substr(bodyCounter, 5) == "const" && leaf != "const")
{
return bodyCounter+4;
}
else if(x.substr(bodyCounter, 3) == "var" && leaf != "var")
{
return bodyCounter+2;
}
else
{
cout << "Error: invalid syntax" << endl;
}
return 99;
}
int parseFunc()
{
int funcCounter = 1 + parseImport();
while(iswspace(x[funcCounter]))
{
funcCounter++;
}
cout << funcCounter << endl;
if(x.substr(funcCounter, 4) == "func")
{
funcCounter = funcCounter + 4;
while(iswspace(x[funcCounter]))
{
funcCounter++;
}
int j = funcCounter;
string funcName;
cout << "I am printing from inside 'parseFunc()'"<< endl;
for(int i = j; !iswspace(x[i]); i++)
{
funcName = funcName + x[i];
counter = i;
}
cout << "func--> " << funcName << endl;
return counter;
}
else
{
cout << "Error: invalid syntax"<< endl;
}
return 0;
}
int parseImport()
{
int importCounter = 1 + parsePackage();
while(iswspace(x[importCounter]))
{
importCounter++;
}
cout << importCounter << endl;
if(x.substr(importCounter, 6) == "import")
{
importCounter = importCounter + 6;
while(iswspace(x[importCounter]))
{
importCounter++;
}
int k = importCounter;
string importName;
cout << "I am printing from inside 'parseImport()'"<< endl;
for(int i = k; !iswspace(x[i]) && x[i] != '('; i++)
{
importName = importName + x[i];
counter = i;
}
cout << "import--> " << importName << endl;
return counter;
}
else
{
cout << "Error: invalid syntax"<< endl;
}
return 0;
}
int parsePackage()
{
if(x.substr(0,7) == "package") //find subset of string, compare to target string (i.e. 'package')
{
int packageCounter = 7;
cout << "I am printing from inside 'parsePackage()'"<< endl;
while(iswspace(x[packageCounter]))
{
packageCounter++;
}
for(int i = packageCounter; !iswspace(x[i]); i++)
{
result = result + x[i];
counter = i;
}
cout << "package--> " << result << endl;
packageNode.value = result;
packageNode.child = &importNode;
return counter;
}
else
{
cout << "Error: invalid syntax"<< endl;
}
return 0;
}
int main()
{
//x = "package main\n";
x = "package main\nimport \"fmt\"\nfunc main()\n{\nvar Pi = 50\n}";
//x = "package main\nimport \"fmt\"\nfunc main()\n{\n\tprintln(\"helloworld\")\n}";
//x = "package main\nfunc main()\n{\n\tprintln(\"helloworld\")\n}";
int result = parseVar();
cout << "\n\n\npackageNode's value-->" << packageNode.value << endl;
cout << "\n\n\npackageNode's child-->" << importNode.child << endl;
return 0;
}
/*TODO*/
//the possibility of more than one import
//parse variable types?
//allow for whitespace before package?
//parseVar and ParseConst as normal, like other functions, return as though skiping over the function
//parse whitespace, don't just +1 the counter!
//build generic node class, have nodes for each part e.g. packageNode, importNode etc., then create node in each respective function, thus tree?