-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
199 lines (177 loc) · 6.16 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
198
199
using AlmostBinary_Compiler.utils;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace AlmostBinary_Compiler
{
class Parser
{
#region fields
private static ILogger Log => Serilog.Log.ForContext<Parser>();
private List<string> _imports = new List<string>();
TokenList? _tokens;
Block? _currentBlock = null;
Stack<Block> _blockstack = new Stack<Block>();
List<Stmt> _tree = new List<Stmt>();
#endregion
#region properties
public List<string> Imports { get => _imports; set => _imports = value; }
public List<Stmt> Tree { get => _tree; }
#endregion
#region ctor
public Parser(TokenList t)
{
Log.Here().Information("Starting parser.");
_tokens = t;
if (_tokens == null) Log.Here().Warning("Tokenlist is null.");
Log.Here().Debug($"Starting to parse tokenList -> {JsonSerializer.Serialize(_tokens.Tokens)}");
while (true)
{
Token? tok;
try
{
tok = _tokens.GetToken();
}
catch
{
Log.Here().Verbose("Reached end of file. Shutting down parser..");
break;
}
switch (tok.TokenName)
{
case Lexer.Tokens.Import: Imports.Add(ParseImport()); break;
case Lexer.Tokens.Function: ParseFunction(); break;
case Lexer.Tokens.If: ParseIf(); break;
case Lexer.Tokens.ElseIf: ParseElseIf(); break;
case Lexer.Tokens.Else: ParseElse(); break;
case Lexer.Tokens.Repeat: ParseRepeat(); break;
case Lexer.Tokens.Ident: ParseIdent(); break;
case Lexer.Tokens.Return: ParseReturn(); break;
case Lexer.Tokens.RightBrace: ParseRightBrace(); break;
case Lexer.Tokens.EOF: ParseEOF(); break;
case Lexer.Tokens.LeftParan: _tokens.Pos -= 2; break;
case Lexer.Tokens.LeftBrace: break; // do not increment as GetToken() will use next one
default: throw new Exception($"Unexpected token: '{tok.TokenValue}' (Type: {tok.TokenName})");
}
}
}
#endregion
#region methods
private string ParseImport()
{
string ret;
Token t = _tokens.GetToken();
if (t.TokenName == Lexer.Tokens.Ident)
{
ret = t.TokenValue;
} else
{
throw new Exception($"Expected an identifier after an import statement: TokenName->'{t.TokenName}', TokenValue->'{t.TokenValue}'");
}
return ret;
}
private void ParseFunction()
{
Func func = Func.Parse(_tokens);
if (_currentBlock == null)
{
_currentBlock = func;
}
else
{
_currentBlock.AddStmt(new Return(null));
_tree.Add(_currentBlock);
_currentBlock = func;
}
}
private void ParseIf()
{
IfBlock ifblock = IfBlock.Parse(_tokens);
if (_currentBlock != null)
{
_blockstack.Push(_currentBlock);
_currentBlock = ifblock;
}
}
private void ParseElseIf()
{
ElseIfBlock elseifblock = ElseIfBlock.Parse(_tokens);
if (_currentBlock != null)
{
_blockstack.Push(_currentBlock);
_currentBlock = elseifblock;
}
}
private void ParseElse()
{
if (_currentBlock != null)
{
_blockstack.Push(_currentBlock);
_currentBlock = new ElseBlock();
}
}
private void ParseRepeat()
{
if (_currentBlock != null)
{
_blockstack.Push(_currentBlock);
_currentBlock = new RepeatBlock();
}
}
private void ParseIdent()
{
if (_tokens.PeekToken().TokenName == Lexer.Tokens.Equal)
{
_tokens.Pos--;
Assign a = Assign.Parse(_tokens);
_tokens.Pos++;
_currentBlock.AddStmt(a);
}
else if (_tokens.PeekToken().TokenName == Lexer.Tokens.LeftParan)
{
_tokens.Pos--;
Call c = Call.Parse(_tokens);
_currentBlock.AddStmt(c);
}
}
private void ParseReturn()
{
Return r = Return.Parse(_tokens);
_currentBlock.AddStmt(r);
}
private void ParseRightBrace()
{
if (_currentBlock is Func)
{
_currentBlock.AddStmt(new Return(null));
_tree.Add(_currentBlock);
_currentBlock = null;
}
else if (_currentBlock is IfBlock || _currentBlock is ElseIfBlock || _currentBlock is ElseBlock)
{
_currentBlock.AddStmt(new EndIf());
Block block = _currentBlock;
if (_blockstack.Count > 0)
{
_currentBlock = _blockstack.Pop();
_currentBlock.AddStmt(block);
}
}
else if (_currentBlock is RepeatBlock)
{
Block block = _currentBlock;
if (_blockstack.Count > 0)
{
_currentBlock = _blockstack.Pop();
_currentBlock.AddStmt(block);
}
}
}
private void ParseEOF()
{
_tree.Add(_currentBlock);
}
#endregion
}
}