-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.nitro
105 lines (79 loc) · 3.2 KB
/
tests.nitro
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
include "./includes.nitro"
fun main() {
let test = FilePath::new("tests/current_test.txt").read_text()!!.trim()
Log::info("Compiling test: $test")
let file = FilePath::new("../../test/nitro/compiler/$test")
let src = file.read_text()!!
let start_code = src.index_of("/* --- BEGIN CODE --- */\n")
let end_code = src.index_of("\n/* --- END CODE --- */")
if start_code.is_none() || end_code.is_none() {
crash("Malformed test, must contain both 'BEGIN CODE' and 'END CODE' lines")
}
let code = src.substring(start_code!! + 25, end_code!!)
let start_output = src.index_of("/* --- BEGIN OUTPUT ---\n")
let end_output = src.index_of("--- END OUTPUT --- */")
if start_output.is_none() || end_output.is_none() {
crash("Malformed test, must contain both 'BEGIN OUTPUT' and 'END OUTPUT' lines")
}
let output = src.substring(start_output!! + 24, end_output!!)
compile_test(code, file.path, "tests/test.wasm")
// Actual vs expected output
FilePath::new("tests/output.txt").write_text("")!!
FilePath::new("tests/expected_output.txt").write_text(output)!!
}
fun compile_test(input_code: String, input_path: String, output_path: String) {
let unit = CompilationUnit::new()
// Include core library
let start_core = Instant::now()
unit.add_file("../nitro/core/core.nitro", "core")
unit.parse()
// Parse errors
if unit.reporter.has_reports() {
if !unit.reporter.immediate_output {
unit.reporter.report_errors()
}
ret
}
let time_core = start_core.elapsed().to_milliseconds()
Log::info("Core included (Time $time_core ms, Memory ${get_memory().get_stats()})")
// Include compiler
let start_compiler = Instant::now()
unit.add_source(input_code, input_path, "compiler")
unit.parse()
// Parse errors
if unit.reporter.has_reports() {
if !unit.reporter.immediate_output {
unit.reporter.report_errors()
}
ret
}
let time_compiler = start_compiler.elapsed().to_milliseconds()
Log::info("Program included (Time $time_compiler ms, Memory ${get_memory().get_stats()})")
// Perform type checking
let start_checking = Instant::now()
unit.check_types()
// Type errors
if unit.reporter.has_reports() {
if !unit.reporter.immediate_output {
unit.reporter.report_errors()
}
ret
}
let time_checking = start_checking.elapsed().to_milliseconds()
Log::info("Type checking finished (Time $time_checking ms, Memory ${get_memory().get_stats()})")
// Perform code generation
let start_compiling = Instant::now()
unit.compile(FilePath::new(output_path))
// Final errors
if unit.reporter.has_reports() {
if !unit.reporter.immediate_output {
unit.reporter.report_errors()
}
ret
}
let time_compiling = start_compiling.elapsed().to_milliseconds()
Log::info("Compilation finished (Time $time_compiling ms, Memory ${get_memory().get_stats()})")
FilePath::new("tests/program.debug").write_text(unit.program.to_string())!!
let total_time = start_core.elapsed().to_milliseconds()
Log::info("Total time: $total_time ms")
}