-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_hover_info.rs
More file actions
152 lines (128 loc) · 4.27 KB
/
test_hover_info.rs
File metadata and controls
152 lines (128 loc) · 4.27 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Test script to verify hover info returns correct types
use std::cell::RefCell;
use std::rc::Rc;
use veld_common::lexer::Lexer;
use veld_common::parser::Parser;
use veld_common::types::checker::TypeChecker;
fn main() {
// Initialize tracing for debug output
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
let source = r#"
import std.io as io
pub fn main() => do
io.println("Hello, World!")
end
fn no_params() => do
io.println("No parameters")
end
fn add(a, b) => do
a + b
end
"#;
println!("Testing hover info for functions...\n");
println!("Source code:");
println!("{}", source);
println!("\n{}", "=".repeat(60));
// Lex and parse
let mut lexer = Lexer::new(source);
let tokens = match lexer.collect_tokens() {
Ok(t) => t,
Err(e) => {
eprintln!("Lexer error: {}", e);
return;
}
};
let mut parser = Parser::new(tokens);
let statements = match parser.parse() {
Ok(s) => s,
Err(e) => {
eprintln!("Parser error: {}", e);
return;
}
};
println!("\nParsed {} statements", statements.len());
// Type check
let mut type_checker = TypeChecker::new();
// Register the import alias
type_checker
.module_registry()
.borrow_mut()
.register_alias("io".to_string(), "std.io".to_string());
if let Err(e) = type_checker.check_program(&statements) {
eprintln!("Type check error: {}", e);
}
let type_checker_rc = Rc::new(RefCell::new(type_checker));
println!("\n{}", "=".repeat(60));
println!("Function Types:");
println!("{}", "=".repeat(60));
// Check function types
let tc = type_checker_rc.borrow();
// Check main function
if let Some(main_type) = tc.env().get("main") {
println!("\nmain: {:?}", main_type);
if let veld_common::types::Type::Function {
params,
return_type,
} = main_type
{
println!(" - Parameters: {} (expected: 0)", params.len());
println!(" - Return type: {:?}", return_type);
match return_type.as_ref() {
veld_common::types::Type::Unit => {
println!(" ✓ PASS: Return type is Unit (())")
}
veld_common::types::Type::TypeVar(id) => {
println!(" ✗ FAIL: Return type is TypeVar({}) instead of Unit", id);
println!(" This is the bug we're trying to fix!");
}
other => {
println!(" ? Return type is: {:?}", other);
}
}
}
} else {
println!("\n✗ FAIL: main function not found in type environment");
}
// Check no_params function
if let Some(no_params_type) = tc.env().get("no_params") {
println!("\nno_params: {:?}", no_params_type);
if let veld_common::types::Type::Function {
params,
return_type,
} = no_params_type
{
println!(" - Parameters: {} (expected: 0)", params.len());
println!(" - Return type: {:?}", return_type);
match return_type.as_ref() {
veld_common::types::Type::Unit => {
println!(" ✓ PASS: Return type is Unit (())")
}
veld_common::types::Type::TypeVar(id) => {
println!(" ✗ FAIL: Return type is TypeVar({}) instead of Unit", id);
}
other => {
println!(" ? Return type is: {:?}", other);
}
}
}
} else {
println!("\n✗ FAIL: no_params function not found in type environment");
}
// Check add function
if let Some(add_type) = tc.env().get("add") {
println!("\nadd: {:?}", add_type);
if let veld_common::types::Type::Function {
params,
return_type,
} = add_type
{
println!(" - Parameters: {} (expected: 2)", params.len());
println!(" - Return type: {:?}", return_type);
}
} else {
println!("\n✗ FAIL: add function not found in type environment");
}
println!("\n{}", "=".repeat(60));
}