-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_iden_pass.rs
129 lines (123 loc) · 3.47 KB
/
validate_iden_pass.rs
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
use crate::{fe::{ast::*, loc::Loc}, mw::pass::AstPass};
use std::collections::HashSet;
pub struct ValidateIdenPass;
#[derive(Clone)]
struct Context {
idens: HashSet<String>,
labels: HashSet<String>,
}
impl Context {
fn new() -> Self {
Self{idens: HashSet::new(), labels: HashSet::new()}
}
fn validate_iden(&self, node: &IdenAstNode) {
if !self.idens.contains(&node.name) {
node.loc.message(String::from("validate_iden_pass: failed"));
node.loc.error(format!("unknown identifier '{}'", node.name));
}
}
fn insert_iden(&mut self, iden: String) {
let _ = self.idens.insert(iden);
}
fn validate_label(&self, label: &String, loc: Loc) {
if !self.labels.contains(label) {
loc.message(String::from("validate_iden_pass: failed"));
loc.error(format!("unknown label identifier '{}'", label));
}
}
fn insert_label(&mut self, label: &String, loc: Loc) {
if self.labels.contains(label) {
loc.message(String::from("validate_iden_pass: failed"));
loc.error(format!("label identifier already exists '{}'", label));
}
let _ = self.labels.insert(label.clone());
}
}
impl AstPass for ValidateIdenPass {
fn apply(&self, nodes: &mut Vec<AstNode>) {
for node in nodes.iter() {
let mut context: Context = Context::new();
validate_node(node, &mut context);
validate_label(node, &context);
}
}
fn name(&self) -> String {String::from("validate_iden_pass")}
}
fn validate_node(node: &AstNode, context: &mut Context) {
match node {
AstNode::Call(node) => {
for param in node.params.iter() {
if let AstNode::Iden(iden_node) = param {
context.validate_iden(iden_node);
}
}
context.insert_iden(node.id.clone());
},
AstNode::Arith(node) => {
if let AstNode::Iden(ref iden_lhs) = *node.lhs {
context.validate_iden(iden_lhs);
}
if let AstNode::Iden(ref iden_rhs) = *node.rhs {
context.validate_iden(iden_rhs);
}
},
AstNode::Relop(node) => {
if let AstNode::Iden(ref iden_lhs) = *node.lhs {
context.validate_iden(iden_lhs);
}
if let AstNode::Iden(ref iden_rhs) = *node.rhs {
context.validate_iden(iden_rhs);
}
},
AstNode::Unary(node) => {
if let AstNode::Iden(ref iden_var) = *node.var {
context.validate_iden(iden_var);
}
},
AstNode::Function(node) => {
for arg in node.args.iter() {
if let AstNode::Iden(iden_node) = arg {
context.insert_iden(iden_node.name.clone());
}
}
for body_node in node.body.iter() {
validate_node(body_node, context);
}
},
AstNode::Assignment(node) => {
validate_node(&*node.var, context);
context.insert_iden(node.name.clone());
},
AstNode::Label(node) => {
context.insert_label(&node.name, node.loc.clone());
let mut inner_context: Context = context.clone();
for body_node in node.body.iter() {
validate_node(body_node, &mut inner_context);
}
},
AstNode::If(node) => validate_node(&*node.condition, context),
AstNode::Ret(node) => {
if let AstNode::Iden(ref iden_var) = *node.var {
context.validate_iden(iden_var);
}
},
_ => {},
}
}
fn validate_label(node: &AstNode, context: &Context) {
match node {
AstNode::Function(node) => {
for body_node in node.body.iter() {
validate_label(body_node, context);
}
},
AstNode::Label(node) => {
for body_node in node.body.iter() {
validate_label(body_node, context);
}
},
AstNode::If(node) => context.validate_label(&node.label, node.loc.clone()),
AstNode::Goto(node) => context.validate_label(&node.name, node.loc.clone()),
_ => {},
}
}