-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
81 lines (65 loc) · 1.23 KB
/
node.cpp
File metadata and controls
81 lines (65 loc) · 1.23 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
#include <iostream>
#include <map>
#include "node.hpp"
using namespace std;
//prinTb1 is token's dict for debug
map<NodeKind,string> prinTb1 ={
{ND_ADD,"+"},
{ND_MIN,"-"},
{ND_MUL,"*"},
{ND_DIV,"/"},
{ND_ASSIGN,"="},
{ND_LPAREN,"("},
{ND_RPAREN,")"},
};
//Create identifer(ex: function name, variable name, string value..) node
Node::Node(string name){
this->op = ND_IDENT;
this->name = name;
}
//Create operation leaf
Node::Node(NodeKind kind){
this->op = kind;
}
//Create figure node
Node::Node(int val){
this->op = ND_NUM;
this->intvalue = val;
}
//Create node tree
Node::Node(NodeKind kind,Node *left,Node *right){
this->op = kind;
this->left = left;
this->right = right;
}
//Output parsed tokens
//This function is used for debug
void Node::print(void)
{
switch (this->op)
{
case ND_NUM:
cout << this->intvalue;
return;
case ND_IDENT:
cout << this->name;
return;
default:
break;
}
cout << "( "<<prinTb1[op] << " ";
if(left){
left->print();
}
else{
cout <<"_";
}
cout <<" ";
if(right){
right->print();
}
else{
cout <<"_";
}
cout << " )";
}