-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_analysis.rs
168 lines (144 loc) · 5.07 KB
/
semantic_analysis.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::collections::HashMap;
use ast::{Expr, Identifier};
#[derive(Debug, PartialEq, Clone)]
pub enum Type {
Let,
Const,
}
#[derive(Debug, Default, Clone)]
pub struct SymbolTable {
table: HashMap<String, Type>,
}
impl SymbolTable {
pub fn new() -> SymbolTable {
SymbolTable::default()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum AnalysisError {
UnboundIdentifier(Identifier),
RedeclaringIdentifier(Identifier),
ReassigningConst(Identifier),
FunctionCallWithIncorrectArity(Identifier),
}
#[derive(Debug, Clone)]
pub enum AnalysisWarning {
ReassigningIdentifier(Identifier),
}
#[derive(Debug)]
pub struct AnalysisResults {
pub errors: Vec<AnalysisError>,
pub warnings: Vec<AnalysisWarning>,
}
impl AnalysisResults {
fn new() -> AnalysisResults {
AnalysisResults {
errors: Vec::new(),
warnings: Vec::new(),
}
}
fn append(&mut self, other: AnalysisResults) {
self.errors.append(&mut other.errors.clone());
self.warnings.append(&mut other.warnings.clone());
}
}
#[derive(Debug, Default, Clone)]
pub struct Analyzer {
symbol_table: SymbolTable,
function_arity: HashMap<String, usize>,
}
impl Analyzer {
pub fn new() -> Analyzer {
Analyzer::default()
}
fn add_function_arity(&mut self, ident: &Identifier, rhs: &Expr) {
if let Expr::Function(ref idents, _) = &rhs {
self.function_arity
.insert(ident.clone().value, idents.len());
}
}
fn analyze_expr(&mut self, expr: &Expr) -> AnalysisResults {
let mut results = AnalysisResults::new();
match expr {
Expr::Literal(ref _v) => (),
Expr::Identifier(ref ident) => {
if !self.symbol_table.table.contains_key(&ident.value) {
results
.errors
.push(AnalysisError::UnboundIdentifier(ident.clone()))
}
}
Expr::Assignment(ref ident, ref rhs) => {
if !self.symbol_table.table.contains_key(&ident.value) {
results
.errors
.push(AnalysisError::UnboundIdentifier(ident.clone()))
} else if self.symbol_table.table.get(&ident.value).unwrap() == &Type::Const {
results
.errors
.push(AnalysisError::ReassigningConst(ident.clone()))
}
results
.warnings
.push(AnalysisWarning::ReassigningIdentifier(ident.clone()));
self.add_function_arity(ident, rhs);
results.append(self.analyze_expr(rhs))
}
Expr::LetAssignment(ref ident, ref rhs) => {
if self.symbol_table.table.contains_key(&ident.value) {
results
.errors
.push(AnalysisError::RedeclaringIdentifier(ident.clone()))
}
self.symbol_table
.table
.insert(ident.value.clone(), Type::Let);
self.add_function_arity(ident, rhs);
results.append(self.analyze_expr(rhs))
}
Expr::ConstAssignment(ref ident, ref rhs) => {
if self.symbol_table.table.contains_key(&ident.value) {
results
.errors
.push(AnalysisError::RedeclaringIdentifier(ident.clone()))
}
self.symbol_table
.table
.insert(ident.value.clone(), Type::Const);
self.add_function_arity(ident, rhs);
results.append(self.analyze_expr(rhs))
}
Expr::BinaryOp(ref a, _, ref b) => {
results.append(self.analyze_expr(a));
results.append(self.analyze_expr(b));
}
Expr::Function(params, body) => {
let mut fn_analyzer = self.clone();
// add params to symbol table
fn_analyzer.symbol_table.table.extend(
params
.iter()
.map(|param| (param.value.clone(), Type::Const)));
let fn_results = fn_analyzer.analyze(body);
results.append(fn_results);
}
Expr::CallFunction(ref ident, ref arg_exprs) => {
let expected_len = self.function_arity.get(&ident.value).unwrap();
if arg_exprs.len() != *expected_len {
results
.errors
.push(AnalysisError::FunctionCallWithIncorrectArity(ident.clone()))
}
results.append(self.analyze(arg_exprs));
}
}
results
}
pub fn analyze(&mut self, exprs: &[Expr]) -> AnalysisResults {
let mut results = AnalysisResults::new();
for expr in exprs {
results.append(self.analyze_expr(&expr))
}
results
}
}