-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.js
129 lines (109 loc) · 3.25 KB
/
interpreter.js
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
const TOKEN_TYPE = require('./constent')
//###############################################################################
//# #
//# INTERPRETER #
//# #
//###############################################################################
class NodeVisitor {
getNodeName(node) {
return node.__proto__.constructor.name
}
genericVisit(node) {
throw Error(`No visit_${this.getNodeName(node)} method`)
}
visit(node) {
const method_name = 'visit_' + this.getNodeName(node)
const visitor = this[method_name] || this.genericVisit(node)
return visitor(node)
}
}
class Interpreter extends NodeVisitor {
constructor(tree) {
super()
this.tree = tree
this.GLOBAL_SCOPE = {}
this.visit_Program = this.visit_Program.bind(this)
this.visit_Block = this.visit_Block.bind(this)
this.visit_VarDecl = this.visit_VarDecl.bind(this)
this.visit_Type = this.visit_Type.bind(this)
this.visit_BinOp = this.visit_BinOp.bind(this)
this.visit_Num = this.visit_Num.bind(this)
this.visit_UnaryOp = this.visit_UnaryOp.bind(this)
this.visit_Compound = this.visit_Compound.bind(this)
this.visit_Assign = this.visit_Assign.bind(this)
this.visit_Var = this.visit_Var.bind(this)
this.visit_NoOp = this.visit_NoOp.bind(this)
}
visit_Program(node) {
this.visit(node.block)
}
visit_Block(node) {
for (const declaration of node.declarations) {
this.visit(declaration)
}
this.visit(node.compoundStatement)
}
visit_VarDecl(node) {
return null
}
visit_Type(node) {
return null
}
visit_BinOp(node) {
if (node.op.type === TOKEN_TYPE.PLUS) {
return this.visit(node.left) + this.visit(node.right)
} else if (node.op.type === TOKEN_TYPE.MINUS) {
return this.visit(node.left) - this.visit(node.right)
} else if (node.op.type === TOKEN_TYPE.MUL) {
return this.visit(node.left) * this.visit(node.right)
} else if (node.op.type === TOKEN_TYPE.INTEGER_DIV) {
return this.visit(node.left) / this.visit(node.right)
} else if (node.op.type === TOKEN_TYPE.FLOAT_DIV) {
return this.visit(node.left) / this.visit(node.right)
}
}
visit_Num(node) {
return node.value
}
visit_UnaryOp(node) {
const op = node.op.type
if (op === TOKEN_TYPE.PLUS) {
return +this.visit(node.expr)
} else if (op === TOKEN_TYPE.MINUS) {
return -this.visit(node.expr)
}
}
visit_Compound(node) {
for (let child of node.children) {
if (child) {
this.visit(child)
}
}
}
visit_Assign(node) {
const var_name = node.left.value
this.GLOBAL_SCOPE[var_name] = this.visit(node.right)
}
visit_Var(node) {
const var_name = node.value
const val = this.GLOBAL_SCOPE[var_name]
if (!val) {
throw Error(`no value ${var_name}`)
}
return val
}
visit_NoOp(node) {
return null
}
visit_ProcedureDecl(node) {
return null
}
interpret() {
const tree = this.tree
if (!tree) {
return ''
}
return this.visit(tree)
}
}
module.exports = { NodeVisitor, Interpreter }