-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
106 lines (92 loc) · 2.79 KB
/
parser.h
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
#ifndef PARSER_H
#define PARSER_H
#include "lexer.h"
#include <ostream>
#include <map>
#include <vector>
#include<memory>
using namespace std;
class ASTNode {
public:
virtual void generateCode(ostream& out) = 0 ;
virtual void generateL(ostream& out) = 0 ;
virtual void generateR(ostream& out) = 0 ;
};
class NumberNode : public ASTNode{
public:
int val;
NumberNode(int val);
void generateCode(ostream& out);
void generateL(ostream &out);
void generateR(ostream& out);
};
class VariableNode : public ASTNode {
public:
static map<string,int> mem_loc;
static int counter;
string name;
int id ;
VariableNode(string name);
VariableNode(){};
void generateCode(ostream& out);
void generateL(ostream &out);
void generateR(ostream& out);
};
class VariableDec : public VariableNode{
public:
string varN ;
shared_ptr<ASTNode> val ;
VariableDec(string varN , shared_ptr<ASTNode> val);
void generateCode(ostream& out);
void generateL(ostream &out){};
void generateR(ostream& out){};
};
class BinaryOpNode : public ASTNode {
public:
shared_ptr<ASTNode> l , r ;
char op ;
BinaryOpNode(shared_ptr<ASTNode> l, char op , shared_ptr<ASTNode> r);
void generateL(ostream &out){};
void generateR(ostream& out){};
void generateCode(ostream &out);
};
class ConditionalNode : public ASTNode {
public:
shared_ptr<ASTNode> cond , then_br , else_br ;
ConditionalNode(shared_ptr<ASTNode>cond , shared_ptr<ASTNode> then_br , shared_ptr<ASTNode> else_br);
void generateCode(ostream &out);
void generateL(ostream &out){};
void generateR(ostream& out){};
};
class BlockNode : public ASTNode {
public:
vector<shared_ptr<ASTNode>> statements;
BlockNode(){}
void addStat(shared_ptr<ASTNode> statement);
void generateCode(ostream &out);
void generateL(ostream &out){};
void generateR(ostream& out){};
};
class Parser {
public:
Parser(vector<Token>& tokens);
shared_ptr<ASTNode> parse();
private:
vector<Token> tokens;
size_t pos ;
shared_ptr<ASTNode> parseExp();
shared_ptr<ASTNode> parseExpStat();
shared_ptr<ASTNode> parsePrim();
shared_ptr<ASTNode> parseStat();
shared_ptr<ASTNode> parseVarDec();
shared_ptr<ASTNode> parseVarAssign();
shared_ptr<ASTNode> parseCond();
shared_ptr<ASTNode> parseBlock();
bool match(vector<Tokentype> types);
bool isAtEnd();
bool check(Tokentype type);
void advance();
Token previous();
void consume(Tokentype type,string);
};
#endif