-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope_analysis.nit
More file actions
128 lines (105 loc) · 2.45 KB
/
Copy pathscope_analysis.nit
File metadata and controls
128 lines (105 loc) · 2.45 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module scope_analysis
import minilang_test_parser
class Variable
var assigned = false
end
class Scope
var variables = new ArrayMap[String, Variable]
var methods = new ArrayMap[String, Node]
init do
end
init inherit(s: Scope) do
for key,value in s.variables do
variables[key] = value
end
for key,value in s.methods do
methods[key] = value
end
end
end
class ScopeAnalysis
super Visitor
var scopes = new Array[Scope]
redef fun visit(n) do n.accept_scope(self)
end
redef class Nstmt_if
redef fun accept_scope(v: ScopeAnalysis) do
v.scopes.insert(new Scope.inherit(v.scopes.first), 0)
v.enter_visit(n_stmts)
v.scopes.shift
v.scopes.insert(new Scope.inherit(v.scopes.first), 0)
v.enter_visit(n_else)
v.scopes.shift
end
end
redef class Ndef
redef fun accept_scope(v: ScopeAnalysis) do
var new_scope = new Scope.inherit(v.scopes.first)
v.scopes.insert(new_scope, 0)
if n_params != null then
v.enter_visit(n_params.as(not null))
end
v.enter_visit(n_stmts)
v.scopes.shift
end
end
redef class Nparam
redef fun accept_scope(v: ScopeAnalysis) do
v.scopes.first.variables[n_id.text] = new Variable
v.scopes.first.variables[n_id.text].assigned = true
end
end
redef class Nstmt_while
redef fun accept_scope(v: ScopeAnalysis) do
v.scopes.insert(new Scope.inherit(v.scopes.first), 0)
v.enter_visit(n_stmts)
v.scopes.shift
end
end
redef class Nelse_else
redef fun accept_scope(v: ScopeAnalysis) do
v.scopes.insert(new Scope.inherit(v.scopes.first), 0)
v.enter_visit(n_stmts)
v.scopes.shift
end
end
redef class Nelse_elseif
redef fun accept_scope(v: ScopeAnalysis) do
v.scopes.insert(new Scope.inherit(v.scopes.first), 0)
v.enter_visit(n_stmts)
v.scopes.shift
end
end
redef class Node
fun accept_scope(v: ScopeAnalysis) do
v.scopes.push(new Scope)
visit_children(v)
end
end
redef class Nstmt_decl
redef fun accept_scope(v) do
super
v.scopes.first.variables[n_id.text] = new Variable
end
end
redef class Nexpr_var
redef fun accept_scope(v: ScopeAnalysis) do
if not v.scopes.first.variables.has_key(n_id.text) then
print "Undeclared variable"
exit(1)
end
if not v.scopes.first.variables[n_id.text].assigned then
print "Unassigned variable"
exit(1)
end
end
end
redef class Nstmt_assign
redef fun accept_scope(v) do
if not v.scopes.first.variables.has_key(n_id.text) then
print "Undeclared variable"
exit(1)
end
v.scopes.first.variables[n_id.text].assigned = true
end
end