-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
189 lines (149 loc) · 4.63 KB
/
parser.cpp
File metadata and controls
189 lines (149 loc) · 4.63 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
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
#include "Parser.h"
#include <sstream>
#include <regex>
// Helper methods
char Parser::peek() const {
if (isAtEnd()) return '\0';
return input[position];
}
char Parser::current() const {
if (position == 0 || position > input.length()) return '\0';
return input[position - 1];
}
bool Parser::isAtEnd() const {
return position >= input.length();
}
void Parser::advance() {
if (!isAtEnd()) position++;
}
void Parser::skipWhitespace() {
while (!isAtEnd() && std::isspace(peek())) {
advance();
}
}
bool Parser::match(char expected) {
if (isAtEnd() || peek() != expected) return false;
advance();
return true;
}
bool Parser::isAlpha(char c) const {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
bool Parser::isAlphaNumeric(char c) const {
return isAlpha(c) || (c >= '0' && c <= '9');
}
// Parsing methods
std::shared_ptr<Expression> Parser::parse() {
skipWhitespace();
auto expr = parseExpression();
// Check that we've consumed all input
skipWhitespace();
if (!isAtEnd()) {
std::stringstream ss;
ss << "Unexpected character '" << peek() << "' at position " << position;
throw ParserError(ss.str());
}
return expr;
}
bool Parser::parseDefinition(std::string& name, std::shared_ptr<Expression>& expr) {
// Regular expression to match definitions (name = expression)
std::regex def_regex("^\\s*([a-zA-Z][a-zA-Z0-9]*)\\s*=\\s*(.+)$");
std::smatch matches;
if (std::regex_match(input, matches, def_regex)) {
name = matches[1];
// Create a new parser for the expression part
Parser exprParser(matches[2], environment);
expr = exprParser.parse();
return true;
}
return false;
}
std::shared_ptr<Expression> Parser::parseExpression() {
return parseApplication();
}
std::shared_ptr<Expression> Parser::parseApplication() {
// Parse the first expression
auto expr = parsePrimary();
// Look for additional expressions to create applications
while (true) {
skipWhitespace();
// If we see another expression, it's part of the application
if (!isAtEnd() && (isAlpha(peek()) || peek() == '(' || peek() == 'λ' || peek() == '\\')) {
auto right = parsePrimary();
expr = std::make_shared<Application>(expr, right);
} else {
break;
}
}
return expr;
}
std::shared_ptr<Expression> Parser::parsePrimary() {
skipWhitespace();
if (isAlpha(peek())) {
return parseVariable();
}
if (match('(')) {
return parseParenthesized();
}
if (match('λ') || match('\\')) {
return parseAbstraction();
}
std::stringstream ss;
ss << "Unexpected character '" << peek() << "' at position " << position;
throw ParserError(ss.str());
}
std::shared_ptr<Expression> Parser::parseVariable() {
std::string name = parseIdentifier();
// Check if this is a named reference to a defined expression
if (environment.isDefined(name)) {
return std::make_shared<NamedReference>(name);
}
// Otherwise, it's just a variable
return std::make_shared<Variable>(name);
}
std::string Parser::parseIdentifier() {
std::string name;
// First character must be alphabetic
if (!isAtEnd() && isAlpha(peek())) {
name += peek();
advance();
} else {
std::stringstream ss;
ss << "Expected identifier at position " << position;
throw ParserError(ss.str());
}
// Subsequent characters can be alphanumeric
while (!isAtEnd() && isAlphaNumeric(peek())) {
name += peek();
advance();
}
return name;
}
std::shared_ptr<Expression> Parser::parseAbstraction() {
// Parse the parameter
skipWhitespace();
std::string parameter = parseIdentifier();
// Parse the dot
skipWhitespace();
if (!match('.')) {
std::stringstream ss;
ss << "Expected '.' after lambda parameter at position " << position;
throw ParserError(ss.str());
}
// Parse the body
skipWhitespace();
auto body = parseExpression();
return std::make_shared<Abstraction>(parameter, body);
}
std::shared_ptr<Expression> Parser::parseParenthesized() {
// Parse the expression inside the parentheses
auto expr = parseExpression();
// Make sure we close the parentheses
skipWhitespace();
if (!match(')')) {
std::stringstream ss;
ss << "Expected ')' at position " << position;
throw ParserError(ss.str());
}
return expr;
}