-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
80 lines (77 loc) · 2.47 KB
/
parser.cpp
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
#include "parser.h"
Object* ReadToken(Tokenizer* tokenizer) {
if (tokenizer->IsEnd()) {
throw SyntaxError("Unexpected end");
}
auto cur_token = tokenizer->GetToken();
tokenizer->Next();
if (cur_token == Token{BracketToken::OPEN}) {
auto res = ReadList(tokenizer);
if (tokenizer->IsEnd()) {
throw SyntaxError("Unexpected end");
}
cur_token = tokenizer->GetToken();
if (cur_token != Token{BracketToken::CLOSE}) {
throw SyntaxError("Bad list");
}
tokenizer->Next();
return res;
}
if (cur_token == Token{BracketToken::CLOSE}) {
throw SyntaxError("Bad brackets");
}
if (auto num_token = std::get_if<ConstantToken>(&cur_token)) {
return Heap::Instance()->Allocate<Number>(num_token->value);
}
if (auto symbol_token = std::get_if<SymbolToken>(&cur_token)) {
if (symbol_token->name == "#t" || symbol_token->name == "#f") {
return Heap::Instance()->Allocate<Boolean>(symbol_token->name);
}
return Heap::Instance()->Allocate<Symbol>(symbol_token->name);
}
if (auto dot_token = std::get_if<DotToken>(&cur_token)) {
return Heap::Instance()->Allocate<Symbol>(".");
}
if (auto quote_token = std::get_if<QuoteToken>(&cur_token)) {
auto first = Heap::Instance()->Allocate<Symbol>("'");
auto second = ReadToken(tokenizer);
return Heap::Instance()->Allocate<Cell>(first, second);
}
return nullptr;
}
Object* Read(Tokenizer* tokenizer) {
if (tokenizer->IsEnd()) {
throw SyntaxError("Unexpected end");
}
Object* res;
auto cur_token = tokenizer->GetToken();
if (cur_token == Token{DotToken()}) {
throw SyntaxError("Bad dot");
}
while (!tokenizer->IsEnd()) {
res = ReadToken(tokenizer);
}
return res;
}
Object* ReadList(Tokenizer* tokenizer) {
if (tokenizer->IsEnd()) {
throw SyntaxError("Bad brackets");
}
auto cur_token = tokenizer->GetToken();
if (cur_token == Token{BracketToken::CLOSE}) {
return nullptr;
}
if (cur_token == Token{DotToken()}) {
throw SyntaxError("");
}
auto left = ReadToken(tokenizer);
cur_token = tokenizer->GetToken();
Object* right;
if (cur_token != Token{DotToken{}}) {
right = ReadList(tokenizer);
} else {
tokenizer->Next();
right = ReadToken(tokenizer);
}
return Heap::Instance()->Allocate<Cell>(left, right);
}