-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
executable file
·146 lines (127 loc) · 3.71 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
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
#ifndef PARSER_H
#define PARSER_H
#include "token.h"
#include "lexer.h"
#include "symboltable.h"
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <sstream>
using namespace std;
class Parser {
private:
enum Operation {
ADD, SUB, MULT, DIV, // Arithmetic Operators
ISEQ, ISNE, ISLT, ISLE, ISGT, ISGE, // Relational Operators
AND, OR, // Logical Operators
PUSHL, PUSHV, STORE, // Value Transfer Instructions
JUMP, JUMPF, JUMPT, CALL, RET, // Location Transfer Instructions
PRINTF, // Misc
LABEL, SEQ, FUNC, PARAM // Pseudo Operations
};
public:
class TreeNode {
public:
Operation op;
string val; // Variable name or jump label
TreeNode *leftChild;
TreeNode *rightChild;
Symboltable table;
// Utility function used by constructors
void init(Operation opx, string valx, TreeNode *leftChildx, TreeNode *rightChildx) {
op = opx;
val = valx;
leftChild = leftChildx;
rightChild = rightChildx;
}
TreeNode(Operation op, string val) {
init(op, val, NULL, NULL);
}
TreeNode(Operation op, string val, TreeNode *leftChild, TreeNode *rightChild) {
init(op, val, leftChild, rightChild);
}
TreeNode(Operation op) {
init(op, "", NULL, NULL);
}
TreeNode(Operation op, TreeNode *leftChild, TreeNode *rightChild) {
init(op, "", leftChild, rightChild);
}
static string toString(TreeNode *node) {
return toString0(node, 0);
}
static string toString0(TreeNode *node, int spaces) {
static string blanks =" ";
string left = "";
string right = "";
bool isLeaf = true;
if (node->leftChild != NULL) {
left = toString0(node->leftChild, spaces+2);
isLeaf = false;
}
if (node->rightChild != NULL) {
right = toString0(node->rightChild, spaces+2);
isLeaf = false;
}
string ret;
if (isLeaf) {
ret = blanks.substr(0, spaces) + ops[node->op] + "[" + node->val + "]";
} else {
ret = blanks.substr(0, spaces) + ops[node->op] + "(\n" + left + ",\n" + right + "\n" +
blanks.substr(0, spaces) + ")";
}
return ret;
}
};
private:
Lexer lexer;
Token token;
ostream& out;
int lindex;
int tindex;
int jindex;
Symboltable table;
string itos(int i) { stringstream ss; ss << i; string res = ss.str(); return res;}
string makeLabel() { string tmp = "L"; stringstream ss; ss << ++lindex; string res = ss.str(); tmp = tmp + res; return tmp;}
string JmakeLabel() { string tmp = "J"; stringstream ss; ss << ++jindex; string res = ss.str(); tmp = tmp + res; return tmp;}
static const string ops[];
// string vars[100];
// int vindex;
void error(string message);
void check(int tokenType, string message);
// string currentFunc;
// int nfmts;
// string fmts[100];
// string fmt;
// int nparams;
public:
TreeNode* funcall(string functionName);
TreeNode* factor();
TreeNode* term();
TreeNode* expression();
TreeNode* relationalExpression();
TreeNode* logicalExpression();
TreeNode* assignmentExpression();
TreeNode* returnStatement();
// TreeNode* printfStatement();
TreeNode* whileStatement();
TreeNode* ifStatement();
TreeNode* assignStatement();
TreeNode* vardefStatement();
TreeNode* statement();
TreeNode* block();
TreeNode* parameterdef();
TreeNode* parameterdefs();
TreeNode* function();
TreeNode* compilationunit();
TreeNode* printfStatement();
void vardefs(TreeNode *node);
void genasm(TreeNode *node);
void emit(string a);
void genRelational(string a);
void geninst(TreeNode *node);
bool isNew(string a);
Parser(Lexer& lexer, ostream& out);
~Parser();
};
#endif