-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
89 lines (68 loc) · 2.85 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
81
82
83
84
85
86
87
88
89
#ifndef PARSER_H
#define PARSER_H
#include "lexer.h"
#include "AST.cpp"
#include <vector>
#include <memory>
#include <stdexcept>
#include <stack> // Include <stack> header for using std::stack
class Parser {
std::vector<lexer> tokens;
std::stack<lexer> tokenStack;
public:
Parser(std::vector<lexer> tokens) : tokens(tokens) {
for (auto it = tokens.rbegin(); it != tokens.rend(); ++it) {
tokenStack.push(*it); // Push tokens onto the stack in reverse order
}
}
std::vector<std::shared_ptr<ASTNode>> parse() {
std::vector<std::shared_ptr<ASTNode>> statements;
while (!tokenStack.empty()) {
statements.push_back(parseStatement());
}
return statements;
}
private:
std::shared_ptr<ASTNode> parseStatement() {
while (!tokenStack.empty()) {
auto token = tokenStack.top();
tokenStack.pop();
if (token.tokenType == TokenType::Keyword && token.tokenValue == "summonsoul") {
if (tokenStack.empty()) {
throw std::runtime_error("Unexpected end of input after 'summonsoul'");
}
std::string varName = tokenStack.top().tokenValue;
tokenStack.pop();
if (tokenStack.empty() || tokenStack.top().tokenType != TokenType::Assignment) {
throw std::runtime_error("Expected '=' after variable name");
}
tokenStack.pop(); // Consume "="
std::string expression = "";
while (!tokenStack.empty() && tokenStack.top().tokenType != TokenType::Keyword) {
expression += tokenStack.top().tokenValue;
tokenStack.pop();
}
// if (tokenStack.empty() || tokenStack.top().tokenType != TokenType::Eol) {
// throw std::runtime_error("Expected ';' at the end of the statement");
// }
// tokenStack.pop(); // Consume ";"
// std::cout << varName << " = " << expression << std::endl;
return std::make_shared<ASTVarDecl>(varName, "Declaration", expression);
}
if(token.tokenType ==TokenType::Keyword && token.tokenValue == "chant"){
if(tokenStack.top().tokenValue=="("){
std::string exp = "";
tokenStack.pop();
while(tokenStack.top().tokenValue!=")" && !tokenStack.empty()){
exp+=tokenStack.top().tokenValue;
tokenStack.pop();
}
tokenStack.pop();
// std::cout<<exp<<std::endl;
return std::make_shared<ASTVarDecl>("NULL","Print",exp);
}
}
}
}
};
#endif // PARSER_H