-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.h
More file actions
111 lines (67 loc) · 2.8 KB
/
Parser.h
File metadata and controls
111 lines (67 loc) · 2.8 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
#ifndef BASHCOMPILER_PARSER_H
#define BASHCOMPILER_PARSER_H
#include "Token.h"
#include "Identifier.h"
#include "TokenContainer.h"
#include "ASTNode.h"
#include "Lexer.h"
#include <iostream>
#include <utility>
#include <string>
#include <vector>
#include <unordered_map>
#include <queue>
#include <stack>
class Parser {
private:
BinOpNode* createBinOpNode(BinOpType::Type type, ASTNode* left, ASTNode* right);
ConstNumberNode* createNumberNode(double value);
ConstBoolNode* createBoolNode(bool value);
IdentifierNode* createIdentifierNode(const std::string& name);
ReturnStmtNode* createReturnStmtNode(ASTNode* expr);
BreakStmtNode* createBreakStmtNode();
FuncCallNode* createFuncCallNode(const std::string& name, const std::vector<ASTNode*>& args);
DeclFuncNode* createDeclFuncNode(const std::string& name,
ValueType::Type returnType, const std::vector<IdentifierNode*>& args,
BlockStmtNode* body);
DeclVarNode* createDeclVarNode(IdentifierNode* id, ASTNode* expr);
BlockStmtNode* createBlockStmtNode(const std::vector<ASTNode*>& statements);
IfStmtNode* createIfStmtNode(ASTNode* condition, BlockStmtNode* stmtList, std::vector<IfStmtNode*> elseIfStmts,
BlockStmtNode* elseStmtList);
ForLoopNode* createForLoopNode(ASTNode* init, ASTNode* cond, BinOpNode* inc, BlockStmtNode* stmtList);
double getNumTokenValue(const Token& numToken);
bool getBoolTokenValue(const Token& boolToken);
ASTNode* parseExpression();
DeclVarNode* parseDeclVar();
IdentifierNode* parseIdentifier();
std::string parseFuncName();
ValueType::Type parseDeclFuncReturnType();
std::vector<IdentifierNode*> parseDeclFuncParams();
DeclFuncNode* parseDeclFunc();
std::vector<ASTNode*> parseFuncCallParams();
FuncCallNode* parseFuncCall();
BlockStmtNode* parseBlockStmt();
ReturnStmtNode* parseReturnStmt();
BreakStmtNode* parseBreakStmt();
IfStmtNode* parseIfStmt();
IfStmtNode* parseElseIfStmt();
ASTNode* parseForLoopInit();
ForLoopNode* parseForLoop();
std::pair<std::queue<Token>, std::queue<ASTNode*>> convertToReversePolish();
bool isOperator(const Token& token);
bool isOpLeftAssociative(const Token& token);
bool isBinaryOperator(const Token& token);
void expect(const std::string& expected);
void errorExpected(const std::string& expected);
void errorExpected(const std::string& expected, const Token& foundTok);
TokenContainer tokens;
ASTNode* parseStatement();
bool parenthesesControl;
void skipWhitespaces();
public:
Parser() {
parenthesesControl = false;
}
ProgramTranslationNode* parse(const TokenContainer& sourceData);
};
#endif //BASHCOMPILER_PARSER_H