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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use tl_lang::compiler::ast::*;
use tl_lang::compiler::error::{SemanticErrorKind, TlError};
use tl_lang::compiler::semantics::SemanticAnalyzer;
// Helper to create basic expressions
fn expr_int(n: i64) -> Expr {
Spanned::dummy(ExprKind::Int(n))
}
fn expr_bool(b: bool) -> Expr {
Spanned::dummy(ExprKind::Bool(b))
}
fn expr_var(name: &str) -> Expr {
Spanned::dummy(ExprKind::Variable(name.to_string()))
}
#[test]
fn test_variable_scope() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// Global scope
analyzer
.declare_variable("g".to_string(), Type::I32, true)
.unwrap();
// Check global access
let ty = analyzer.check_expr(&mut expr_var("g")).unwrap();
assert_eq!(ty, Type::I32);
// Enter local scope
analyzer.enter_scope();
analyzer
.declare_variable("l".to_string(), Type::F32, true)
.unwrap();
// Check local access
let ty = analyzer.check_expr(&mut expr_var("l")).unwrap();
assert_eq!(ty, Type::F32);
// Check global access from local
let ty = analyzer.check_expr(&mut expr_var("g")).unwrap();
assert_eq!(ty, Type::I32);
// Shadowing
analyzer
.declare_variable("g".to_string(), Type::Bool, true)
.unwrap();
let ty = analyzer.check_expr(&mut expr_var("g")).unwrap();
assert_eq!(ty, Type::Bool); // Local g shadows global g
analyzer.exit_scope();
// Local variable should be gone
let res = analyzer.check_expr(&mut expr_var("l"));
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::VariableNotFound(_),
..
})
));
// Global variable should be back to original
let ty = analyzer.check_expr(&mut expr_var("g")).unwrap();
assert_eq!(ty, Type::I32);
}
#[test]
fn test_binary_ops_type_check() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// Int + Int => I64 (Default Int literal type is I64 in semantics currently)
// Actually semantics says: ExprKind::Int(_) => Ok(Type::I64)
let mut expr = Spanned::dummy(ExprKind::BinOp(
Box::new(expr_int(1)),
BinOp::Add,
Box::new(expr_int(2)),
));
let ty = analyzer.check_expr(&mut expr).unwrap();
assert_eq!(ty, Type::I64);
// Mismatch: Int + Bool
let mut expr = Spanned::dummy(ExprKind::BinOp(
Box::new(expr_int(1)),
BinOp::Add,
Box::new(expr_bool(true)),
));
let res = analyzer.check_expr(&mut expr);
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::TypeMismatch { .. },
..
})
));
// Comparison: Int < Int => Bool
let mut expr = Spanned::dummy(ExprKind::BinOp(
Box::new(expr_int(1)),
BinOp::Lt,
Box::new(expr_int(2)),
));
let ty = analyzer.check_expr(&mut expr).unwrap();
assert_eq!(ty, Type::Bool);
}
#[test]
fn test_if_stmt() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// Valid If
let mut stmt = Spanned::dummy(StmtKind::If {
cond: expr_bool(true),
then_block: vec![],
else_block: None,
});
assert!(analyzer.check_stmt(&mut stmt).is_ok());
// Invalid Condition (Int instead of Bool)
let mut stmt = Spanned::dummy(StmtKind::If {
cond: expr_int(1),
then_block: vec![],
else_block: None,
});
// If check_stmt implementation for If is strict, this should fail.
// If it currently passes (due to bug/incomplete impl), we should fix impl or update test expectation.
// Based on standard semantic rules, this MUST fail.
let res = analyzer.check_stmt(&mut stmt);
if res.is_ok() {
// If it accidentally succeeds, print warning (or fail if we want to enforce fix now)
// Given the task is "write tests", finding a bug is good.
// Let's enforce failure to drive fix if needed.
// assert!(res.is_err(), "If statement condition must be Bool");
// Actually, let's verify if implementation enforces it.
// Previous run passed, meaning it MIGHT have succeeded if test code was active?
// Wait, previous run passed because I had NO assertion.
// Now adding assertion.
}
// Note: Assuming implementation is correct or I will fix it.
// Let's try to Assert it fails.
// If SemanticAnalyzer doesn't return Err, this test will fail, prompting me to look at semantics.rs again.
// Ideally I'd fix semantics.rs first if I knew it was broken.
// Looking at semantics.rs snippet line 675:
// if cond_type != Type::Bool { /* strict check */ }
// If that block is empty, it does NOTHING.
// I should fix semantics.rs as well to ensure this test passes meaningfully.
// For now, let's assume I will fix semantics.rs next if this fails.
// Actually, I'll relax the test temporarily to "expect whatever current behavior is"
// NO, that's bad practice.
// I will write the CORRECT test.
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::TypeMismatch { .. },
..
})
));
}
#[test]
fn test_block_scope() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// ExprKind::Block { stmts }
// { let inner = 10; inner } -> should return Int
let mut block_expr = Spanned::dummy(ExprKind::Block(vec![
Spanned::dummy(StmtKind::Let {
name: "inner".to_string(),
type_annotation: Some(Type::I64),
value: expr_int(10),
mutable: false,
}),
Spanned::dummy(StmtKind::Expr(expr_var("inner"))),
]));
// The type of the block is the type of the last expression (if implicit return)
// OR Void if last is stmt?
// semantics.rs check_expr for Block logic needs to be checked.
// Taking a peek at semantics.rs would be good, but let's assume standard Rust-like behavior.
// Actually, implementation of check_expr for Block is not in previous snippet (lines 801-1600).
// It is likely after 1600.
// Let's assume basic scoping works.
analyzer.check_expr(&mut block_expr).unwrap();
// "inner" should not be available here
let res = analyzer.check_expr(&mut expr_var("inner"));
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::VariableNotFound(_),
..
})
));
}
#[test]
fn test_for_loop_range() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// for i in 0..10
let mut stmt = Spanned::dummy(StmtKind::For {
loop_var: "i".to_string(),
iterator: Spanned::dummy(ExprKind::Range(
Box::new(expr_int(0)),
Box::new(expr_int(10)),
)),
body: vec![],
});
assert!(analyzer.check_stmt(&mut stmt).is_ok());
// Inside body, 'i' should be defined as I64
// But check_stmt processes body in its own scope call.
// To verify 'i' type, we need to inject a statement into body that checks 'i'.
// StmtKind::Expr(ExprKind::Variable("i")) -> check validity
let mut body_check_stmt = Spanned::dummy(StmtKind::For {
loop_var: "i".to_string(),
iterator: Spanned::dummy(ExprKind::Range(
Box::new(expr_int(0)),
Box::new(expr_int(10)),
)),
body: vec![Spanned::dummy(StmtKind::Let {
name: "check".to_string(),
type_annotation: Some(Type::I64),
value: expr_var("i"),
mutable: false,
})],
});
assert!(analyzer.check_stmt(&mut body_check_stmt).is_ok());
}
#[test]
fn test_builtin_function_calls() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// print(1) - Valid
let mut call = Spanned::dummy(ExprKind::FnCall("print".to_string(), vec![expr_int(1)]));
let ty = analyzer.check_expr(&mut call).unwrap();
assert_eq!(ty, Type::Void);
// print() - Invalid args
let mut call = Spanned::dummy(ExprKind::FnCall("print".to_string(), vec![]));
let res = analyzer.check_expr(&mut call);
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::ArgumentCountMismatch { .. },
..
})
));
// len(tensor)
// First need a tensor variable
analyzer
.declare_variable("t".to_string(), Type::Tensor(Box::new(Type::F32), 1), true)
.unwrap();
let mut call = Spanned::dummy(ExprKind::FnCall("len".to_string(), vec![expr_var("t")]));
let ty = analyzer.check_expr(&mut call).unwrap();
assert_eq!(ty, Type::I64);
// len(int) - Error
let mut call = Spanned::dummy(ExprKind::FnCall("len".to_string(), vec![expr_int(1)]));
let res = analyzer.check_expr(&mut call);
assert!(matches!(
res,
Err(TlError::Semantic {
kind: SemanticErrorKind::TypeMismatch { .. },
..
})
));
}
#[test]
fn test_tensor_decl_compatibility() {
let mut analyzer = SemanticAnalyzer::new(String::new());
// tensor T: Tensor<f32, 1>; (No init)
let mut stmt = Spanned::dummy(StmtKind::TensorDecl {
name: "T".to_string(),
type_annotation: Type::Tensor(Box::new(Type::F32), 1),
init: None,
});
assert!(analyzer.check_stmt(&mut stmt).is_ok());
// Verify T exists
let ty = analyzer.check_expr(&mut expr_var("T")).unwrap();
// SemanticAnalyzer::declare_variable stores it.
if let Type::Tensor(inner, rank) = ty {
assert_eq!(*inner, Type::F32);
assert_eq!(rank, 1);
} else {
panic!("T not tensor");
}
}