-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
197 lines (177 loc) · 7.74 KB
/
Parser.cs
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
using static NormaLang.Lexer;
namespace NormaLang
{
public class Parser
{
/*
* The parser will find all statements, structs, and defined functions.
*/
public static Line[] Parse(Line[] lines)
{
Line[] parserLines = [];
for (int i = 0; i < lines.Length; i++)
{
Line line = lines[i];
Token[] tokens = line.Tokens;
if (tokens.Length > 0 && tokens[0].Type == TokenType.Reserved)
{
line = ReturnLineWithReservedToken(tokens, lines, ref i);
}
if (tokens.Length != 0)
{
parserLines = [.. parserLines, line];
}
}
return parserLines;
}
internal static Line ReturnLineWithReservedToken(Token[] tokens, Line[] lines, ref int i)
{
if (Statement.StatementTypes.Any(x => x == tokens[0].Value))
{
return ReturnStatement(tokens, lines, ref i);
}
else if (tokens[0].Value == "def")
{
return ReturnDefine(tokens, lines, ref i);
}
else if (tokens[0].Value == "struct")
{
return ReturnStruct(tokens, lines, ref i);
}
else
{
return lines[i];
}
}
internal static Line ReturnStatement(Token[] tokens, Line[] lines, ref int i)
{
Line[] statementLines = [];
Statement.StatementType statementType =
tokens[0].Value == "if" ? Statement.StatementType.If :
tokens[0].Value == "elif" ? Statement.StatementType.ElIf :
tokens[0].Value == "else" ? Statement.StatementType.Else :
tokens[0].Value == "for" ? Statement.StatementType.For :
Statement.StatementType.While;
Token[] tokenInput = statementType != Statement.StatementType.Else ?
tokens.Skip(1).TakeWhile(x => !(x.Type == TokenType.Symbol && x.Value == "{")).ToArray() : [];
statementLines = GetLinesInBrackets(tokens, lines, ref i);
Statement statement = new Statement(tokenInput, statementLines, statementType);
return new Line(lines[i].Number, [], statement: statement);
}
internal static Line ReturnDefine(Token[] tokens, Line[] lines, ref int i)
{
Line line = lines[i];
string name = tokens[1].Value;
Line[] defLines = [];
Variable[] parameters = [];
bool containsParameters = line.Tokens.Any(x => x.Value == ":");
if (containsParameters)
{
string paramtersAll = string.Join("", tokens.Skip(3).TakeWhile(x => x.Value != "{").Select(x => x.Value));
string[] parameterParts = paramtersAll.Split(',');
parameters = parameterParts.Select(x => new Variable(x, null)).ToArray();
}
defLines = GetLinesInBrackets(tokens, lines, ref i);
Define def = new Define(name, defLines, parameters);
return new Line(line.Number, [], define: def);
}
internal static Line ReturnStruct(Token[] tokens, Line[] lines, ref int i)
{
string name = tokens[1].Value;
Line[] structLines = [];
string[] properties = [];
structLines = GetLinesInBrackets(tokens, lines, ref i);
Token[] tokenAr = [];
structLines.Select(x =>
{
tokenAr = tokenAr.Concat(x.Tokens).ToArray();
return x;
}).ToArray();
string[] stringAr = tokenAr.Select(x => x.Value).ToArray();
bool lastWasComma = true;
for (int j = 0; j < stringAr.Length; j++)
{
string str = stringAr[j];
if (lastWasComma)
{
if (str == ",")
{
throw new Exception($"Incorrect syntax in struct '{name}', no commas were found in between property names");
}
else
{
properties = [.. properties, str];
lastWasComma = false;
}
}
else
{
if (str == ",")
{
lastWasComma = true;
}
else
{
throw new Exception($"Incorrect syntax in struct '{name}', two commas ',,' were found with no property in between");
}
}
}
Struct struc = new Struct(name, properties);
return new Line(lines[i].Number, [], @struct: struc);
}
internal static Line[] GetLinesInBrackets(Token[] tokens, Line[] lines, ref int i)
{
Line[] returnLines = [];
Line line = lines[i];
bool openbracketOnSameLine = tokens.Any(x => x.Type == TokenType.Symbol && x.Value == "{");
bool closebracketOnSameLine = tokens.Any(x => x.Type == TokenType.Symbol && x.Value == "}");
if (openbracketOnSameLine && closebracketOnSameLine)
{
var tokensInBrackets = tokens.SkipWhile(x => !(x.Type == TokenType.Symbol && x.Value == "{")).TakeWhile(x => !(x.Type == TokenType.Symbol && x.Value == "}")).Skip(1).ToArray();
returnLines = [new Line(line.Number, tokensInBrackets)];
}
else if (!openbracketOnSameLine && closebracketOnSameLine)
{
throw new Exception("Closing bracket without opening bracket for statement in line " + line.Number);
}
else
{
int startIndex = i + 2;
if (openbracketOnSameLine)
{
startIndex = i + 1;
var tokensInBrackets = tokens.SkipWhile(x => !(x.Type == TokenType.Symbol && x.Value == "{")).Skip(1).ToArray();
if (tokensInBrackets.Length != 0)
{
throw new Exception("Expected closing bracket on same line if there is code after '{'");
}
}
else if (lines[startIndex].Tokens.Length > 0 && lines[startIndex].Tokens[0].Type != TokenType.Symbol && lines[startIndex].Tokens[0].Value == "{")
{
throw new Exception("Expected next line to start with opening bracket in line " + line.Number);
}
int bracketCount = 1;
for (i = startIndex; i < lines.Length; i++)
{
Line l = lines[i];
if (l.Tokens.Length > 0 && l.Tokens[0].Type is TokenType.Reserved)
{
l = ReturnLineWithReservedToken(l.Tokens, lines, ref i);
}
if (l.Tokens.Any(x => x.Type == TokenType.Symbol && x.Value == "{")) bracketCount++;
if (l.Tokens.Any(x => x.Type == TokenType.Symbol && x.Value == "}"))
{
bracketCount--;
if (bracketCount == 0 && l.Tokens.LastOrDefault().Value == "}" && l.Tokens.LastOrDefault().Type == TokenType.Symbol && l.Tokens.Length > 1)
{
l.Tokens = l.Tokens.Take(l.Tokens.Length - 1).ToArray();
}
}
if (bracketCount == 0) break;
returnLines = [.. returnLines, l];
}
}
return returnLines;
}
}
}