This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
85 lines (82 loc) · 3.01 KB
/
Copy pathparser.cpp
File metadata and controls
85 lines (82 loc) · 3.01 KB
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
#include "parser.h"
#include "error.h"
Parser::Parser(std::deque<TokenPtr> tokens0) {
for (auto it = tokens0.begin(); it != tokens0.end(); ++it) {
tokens.push_back(std::move(*it));
}
}
ValuePtr Parser::parse() {
if (tokens.empty())
//TODO: Specify Error Type
throw(SyntaxError("Input Syntax Error"));
auto token = std::move(tokens.front());
tokens.pop_front();
if (token->getType() == TokenType::NUMERIC_LITERAL) {
auto value = static_cast<NumericLiteralToken&>(*token).getValue();
return std::make_shared<NumericValue>(value);
}
else if (token->getType() == TokenType::BOOLEAN_LITERAL) {
auto value = static_cast<BooleanLiteralToken&>(*token).getValue();
return std::make_shared<BooleanValue>(value);
}
else if (token->getType() == TokenType::STRING_LITERAL) {
auto value = static_cast<StringLiteralToken&>(*token).getValue();
return std::make_shared<StringValue>(value);
}
else if (token->getType() == TokenType::IDENTIFIER) {
auto value = static_cast<IdentifierToken&>(*token).getName();
return std::make_shared<SymbolValue>(value);
}
else if (token->getType() == TokenType::LEFT_PAREN) {
auto value = this->parseTails();
return value;
}
else if (token->getType() == TokenType::QUOTE || token->getType() == TokenType::QUASIQUOTE || token->getType() == TokenType::UNQUOTE) {
std::deque<ValuePtr> tmp;
switch (token->getType()) {
case TokenType::QUOTE: tmp.push_back(std::make_shared<SymbolValue>("quote")); break;
case TokenType::QUASIQUOTE: tmp.push_back(std::make_shared<SymbolValue>("quasiquote")); break;
case TokenType::UNQUOTE: tmp.push_back(std::make_shared<SymbolValue>("unquote")); break;
}
tmp.push_back(this->parse());
return toList(tmp);
}
else {
// TODO: To Be Implemented
throw SyntaxError("Unimplemented");
}
}
ValuePtr Parser::parseTails() {
if (tokens.empty())
throw(SyntaxError("Input Syntax Error"));
if (tokens.front()->getType() == TokenType::RIGHT_PAREN) {
tokens.pop_front();
return std::make_shared<NilValue>();
}
auto car = this->parse();
if (tokens.empty())
throw(SyntaxError("Input Syntax Error"));
if (tokens.front()->getType() == TokenType::DOT) {
tokens.pop_front();
auto cdr = this->parse();
if(tokens.empty() || tokens.front()->getType() != TokenType::RIGHT_PAREN)
throw(SyntaxError("Input Syntax Error"));
tokens.pop_front();
return std::make_shared<PairValue>(car, cdr);
}
else {
auto cdr = this->parseTails();
return std::make_shared<PairValue>(car, cdr);
}
}
ValuePtr toList(std::deque<ValuePtr> list) {
if (list.empty()) {
return std::make_shared<NilValue>();
}
else {
auto car = list.front();
list.pop_front();
auto cdr = toList(list);
return std::make_shared<PairValue>(car, cdr);
}
}