litex-lang 0.9.73-beta

A simple formal proof language and verifier, learnable in 2 hours
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#[cfg(test)]
mod lit_file_runner_tests {
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::time::Instant;

    use crate::pipeline::{render_run_source_code_output, run_source_code};
    use crate::prelude::*;

    /// Collect ```litex``` bodies. A block is omitted when the last non-empty line before its opening
    /// fence is exactly `<!-- litex:skip-test -->` (for snippets that are illustrative only).
    fn extract_litex_fenced_blocks(markdown: &str) -> Vec<String> {
        const SKIP_MARKER: &str = "<!-- litex:skip-test -->";
        let mut blocks: Vec<String> = Vec::new();
        let mut in_litex = false;
        let mut skip_this_block = false;
        let mut current = String::new();
        let mut prev_non_empty_outside_block: Option<&str> = None;

        for line in markdown.lines() {
            let trimmed_start = line.trim_start();
            if trimmed_start.starts_with("```") {
                let info = trimmed_start[3..].trim();
                if in_litex {
                    if !skip_this_block {
                        let trimmed = current.trim();
                        if !trimmed.is_empty() {
                            blocks.push(trimmed.to_string());
                        }
                    }
                    current.clear();
                    in_litex = false;
                    skip_this_block = false;
                    prev_non_empty_outside_block = None;
                } else if info == "litex" {
                    in_litex = true;
                    skip_this_block = prev_non_empty_outside_block == Some(SKIP_MARKER);
                    current.clear();
                }
            } else if in_litex {
                if !skip_this_block {
                    if !current.is_empty() {
                        current.push('\n');
                    }
                    current.push_str(line);
                }
            } else {
                let t = line.trim();
                if !t.is_empty() {
                    prev_non_empty_outside_block = Some(t);
                }
            }
        }
        blocks
    }

    fn collect_markdown_files_sorted(docs_dir: &Path) -> Vec<PathBuf> {
        let mut out: Vec<PathBuf> = Vec::new();
        fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
            let read_dir = match fs::read_dir(dir) {
                Ok(entries) => entries,
                Err(_) => return,
            };
            for entry in read_dir.flatten() {
                let path = entry.path();
                let Ok(file_type) = entry.file_type() else {
                    continue;
                };
                if file_type.is_dir() {
                    walk(&path, out);
                } else if path.extension().is_some_and(|e| e == "md") {
                    out.push(path);
                }
            }
        }
        walk(docs_dir, &mut out);
        out.sort();
        out
    }

    #[test]
    fn run_tmp() {
        let tmp_lit_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("examples")
            .join("tmp.lit");

        assert!(
            tmp_lit_path.is_file(),
            "examples/tmp.lit must exist at {:?}",
            tmp_lit_path
        );

        let tmp_lit_content = match fs::read_to_string(&tmp_lit_path) {
            Ok(content) => content,
            Err(read_error) => panic!("failed to read {:?}: {}", tmp_lit_path, read_error),
        };
        if tmp_lit_content.trim().is_empty() {
            println!("examples/tmp.lit is empty; skip run_tmp");
            return;
        }

        let path_str = match tmp_lit_path.to_str() {
            Some(path_string) => path_string,
            None => panic!("{:?} must be valid UTF-8", tmp_lit_path),
        };

        let mut runtime = Runtime::new_with_builtin_code();
        runtime.new_file_path_new_env_new_name_scope(path_str);
        let normalized_source = remove_windows_carriage_return(tmp_lit_content.as_str());

        let start_time = Instant::now();
        let (stmt_results, runtime_error) =
            run_source_code(normalized_source.as_str(), &mut runtime);
        let duration_ms = start_time.elapsed().as_secs_f64() * 1000.0;

        let (run_succeeded, run_output) =
            render_run_source_code_output(&runtime, &stmt_results, &runtime_error, false);

        let status_label = if run_succeeded { "OK" } else { "FAILED" };
        println!(
            "{}\n=== [{}] {:?} ({:.2} ms user file only) ===\n",
            run_output, path_str, status_label, duration_ms
        );
    }

    #[test]
    fn run_file_from_path() {
        let path: String = "./examples/chapter_6_induction.lit".to_string();
        let file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path);
        assert!(
            file_path.is_absolute(),
            "path must be an absolute path: {:?}",
            file_path
        );
        assert!(
            file_path.is_file(),
            "path must point to a file: {:?}",
            file_path
        );

        let source_code = match fs::read_to_string(&file_path) {
            Ok(content) => content,
            Err(read_error) => panic!("failed to read {:?}: {}", file_path, read_error),
        };
        let path_str = match file_path.to_str() {
            Some(path_string) => path_string,
            None => panic!("{:?} must be valid UTF-8", file_path),
        };

        let mut runtime = Runtime::new_with_builtin_code();
        runtime.new_file_path_new_env_new_name_scope(path_str);
        let normalized_source = remove_windows_carriage_return(source_code.as_str());

        let start_time = Instant::now();
        let (stmt_results, runtime_error) =
            run_source_code(normalized_source.as_str(), &mut runtime);
        let duration_ms = start_time.elapsed().as_secs_f64() * 1000.0;

        let (run_succeeded, run_output) =
            render_run_source_code_output(&runtime, &stmt_results, &runtime_error, false);
        let status_label = if run_succeeded { "OK" } else { "FAILED" };
        println!(
            "{}\n=== [{}] {:?} ({:.2} ms user file only) ===\n",
            run_output, path_str, status_label, duration_ms
        );
        assert!(run_succeeded, "Litex file failed: {}", path_str);
    }

    /// All `*.lit` files under `manifest_dir/subdir`, recursively (e.g. `examples/subdir/foo.lit`).
    /// Sorted by full path after collection. Empty if `subdir` is missing or has no `.lit` files.
    fn collect_lit_files_recursive_under(manifest_dir: &Path, subdir: &str) -> Vec<PathBuf> {
        let dir_path = manifest_dir.join(subdir);
        if !dir_path.is_dir() {
            println!("--- {} {:?}: directory missing; skip ---", subdir, dir_path);
            return Vec::new();
        }
        fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
            let read_directory = match fs::read_dir(dir) {
                Ok(entries) => entries,
                Err(read_error) => panic!("failed to read {:?}: {}", dir, read_error),
            };
            for directory_entry_result in read_directory {
                let directory_entry = match directory_entry_result {
                    Ok(entry) => entry,
                    Err(read_error) => panic!("failed to read directory entry: {}", read_error),
                };
                let path = directory_entry.path();
                let Ok(file_type) = directory_entry.file_type() else {
                    continue;
                };
                if file_type.is_dir() {
                    walk(&path, out);
                } else if path.extension().is_some_and(|ext| ext == "lit") {
                    out.push(path);
                }
            }
        }
        let mut lit_file_paths = Vec::new();
        walk(&dir_path, &mut lit_file_paths);
        lit_file_paths.sort();
        lit_file_paths
    }

    /// Single footer: builtin + per-phase sums/walls + `phase timing` line.
    fn print_run_examples_timing_summary(
        builtin_duration_ms: f64,
        examples_ran: bool,
        example_runs_ms: &[(String, f64)],
        examples_phase_wall_ms: f64,
        doc_runs_ms: &[(String, f64)],
        docs_phase_wall_ms: f64,
    ) {
        let examples_sum_ms: f64 = example_runs_ms.iter().map(|(_, ms)| *ms).sum();
        let docs_sum_ms: f64 = doc_runs_ms.iter().map(|(_, ms)| *ms).sum();
        println!("--- timing (summary) ---");
        println!("  builtin init (once): {:.2} ms", builtin_duration_ms);
        if examples_ran {
            println!(
                "  examples: sum of user-file runs: {:.2} ms  |  wall: {:.2} ms",
                examples_sum_ms, examples_phase_wall_ms
            );
        }
        println!(
            "  docs ```litex``` snippets: sum of runs: {:.2} ms  |  wall: {:.2} ms",
            docs_sum_ms, docs_phase_wall_ms
        );
        println!(
            "--- phase timing: examples {:.2} ms | docs {:.2} ms ---",
            examples_phase_wall_ms, docs_phase_wall_ms
        );
    }

    #[test]
    fn run_examples() {
        let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let lit_file_paths = collect_lit_files_recursive_under(&manifest_dir, "examples");

        let builtin_start = Instant::now();
        let mut runtime = Runtime::new_with_builtin_code();
        let builtin_duration_ms = builtin_start.elapsed().as_secs_f64() * 1000.0;

        let mut file_label_and_duration_ms_list: Vec<(String, f64)> = Vec::new();
        let mut every_file_run_ok = true;
        let mut examples_ran = false;
        let mut examples_phase_wall_ms: f64 = 0.0;

        if lit_file_paths.is_empty() {
            println!("--- examples/ (recursive): no .lit files found ---");
        } else {
            examples_ran = true;
            let examples_wall_start = Instant::now();
            let first_lit_path_str = match lit_file_paths[0].to_str() {
                Some(path_string) => path_string,
                None => panic!("{:?} must be valid UTF-8", lit_file_paths[0]),
            };
            runtime.new_file_path_new_env_new_name_scope(first_lit_path_str);

            for (file_index, lit_file_path) in lit_file_paths.iter().enumerate() {
                let lit_file_path_str = match lit_file_path.to_str() {
                    Some(path_string) => path_string,
                    None => panic!("{:?} must be valid UTF-8", lit_file_path),
                };

                let file_label_for_report = match lit_file_path.strip_prefix(&manifest_dir) {
                    Ok(rel) => rel.display().to_string(),
                    Err(_) => match lit_file_path.file_name() {
                        Some(os_file_name) => match os_file_name.to_str() {
                            Some(name_string) => String::from(name_string),
                            None => format!("{:?}", lit_file_path),
                        },
                        None => format!("{:?}", lit_file_path),
                    },
                };

                if file_index > 0 {
                    runtime.clear_current_env_and_parse_name_scope();
                    runtime.set_current_user_lit_file_path(lit_file_path_str);
                }

                let source_code = match fs::read_to_string(lit_file_path) {
                    Ok(content) => content,
                    Err(read_error) => panic!("failed to read {:?}: {}", lit_file_path, read_error),
                };
                let normalized_source = remove_windows_carriage_return(&source_code);

                let start_time_for_one_file = Instant::now();
                let (stmt_results, runtime_error) =
                    run_source_code(normalized_source.as_str(), &mut runtime);
                let duration_ms_for_one_file =
                    start_time_for_one_file.elapsed().as_secs_f64() * 1000.0;

                let (run_succeeded, run_output) =
                    render_run_source_code_output(&runtime, &stmt_results, &runtime_error, false);

                if !run_succeeded {
                    every_file_run_ok = false;
                    println!(
                        "=== [{}] {:?} ===\n{}\n",
                        "FAILED", lit_file_path, run_output
                    );
                    break;
                }

                file_label_and_duration_ms_list
                    .push((file_label_for_report, duration_ms_for_one_file));
            }
            examples_phase_wall_ms = examples_wall_start.elapsed().as_secs_f64() * 1000.0;
        }

        if every_file_run_ok && examples_ran {
            let number_of_lit_files = file_label_and_duration_ms_list.len();

            println!(
                "--- examples/ (recursive): {} .lit files, all OK ---",
                number_of_lit_files
            );
            for (file_label, duration_ms) in file_label_and_duration_ms_list.iter() {
                println!("  {}  {:.2} ms", file_label, duration_ms);
            }
        }

        if !every_file_run_ok {
            return;
        }

        let docs_dir = manifest_dir.join("docs");
        if !docs_dir.is_dir() {
            println!(
                "--- docs folder missing at {:?}; skip markdown litex blocks ---",
                docs_dir
            );
            print_run_examples_timing_summary(
                builtin_duration_ms,
                examples_ran,
                file_label_and_duration_ms_list.as_slice(),
                examples_phase_wall_ms,
                &[],
                0.0,
            );
            return;
        }

        let md_paths = collect_markdown_files_sorted(&docs_dir);
        // (test report label, fenced litex body, current markdown path string for relative run_file resolution)
        let mut doc_snippets: Vec<(String, String, String)> = Vec::new();
        for md_path in md_paths.iter() {
            let rel_label = md_path
                .strip_prefix(&manifest_dir)
                .unwrap_or(md_path)
                .display()
                .to_string();
            let md_current_path_str = md_path.to_string_lossy().into_owned();
            let md_content = match fs::read_to_string(md_path) {
                Ok(content) => content,
                Err(read_error) => panic!("failed to read {:?}: {}", md_path, read_error),
            };
            for (block_index, block) in extract_litex_fenced_blocks(&md_content)
                .into_iter()
                .enumerate()
            {
                doc_snippets.push((
                    format!("{} ```litex```#{}", rel_label, block_index),
                    block,
                    md_current_path_str.clone(),
                ));
            }
        }

        if doc_snippets.is_empty() {
            println!("--- docs: no ```litex``` fenced blocks ---");
            print_run_examples_timing_summary(
                builtin_duration_ms,
                examples_ran,
                file_label_and_duration_ms_list.as_slice(),
                examples_phase_wall_ms,
                &[],
                0.0,
            );
            return;
        }

        if !examples_ran {
            runtime.new_file_path_new_env_new_name_scope("docs/```litex``` snippets");
        }

        println!(
            "--- docs: {} ```litex``` block(s) in {} markdown file(s) ---",
            doc_snippets.len(),
            md_paths.len()
        );

        let docs_wall_start = Instant::now();
        let mut doc_durations_ms: Vec<(String, f64)> = Vec::new();
        for (snippet_index, (label, source_code, md_path_for_run_file)) in
            doc_snippets.iter().enumerate()
        {
            if examples_ran || snippet_index > 0 {
                runtime.clear_current_env_and_parse_name_scope();
            }
            runtime.set_current_user_lit_file_path(md_path_for_run_file.as_str());

            let normalized_source = remove_windows_carriage_return(source_code);
            let start_snippet = Instant::now();
            let (stmt_results, runtime_error) =
                run_source_code(normalized_source.as_str(), &mut runtime);
            let duration_ms = start_snippet.elapsed().as_secs_f64() * 1000.0;

            let (run_succeeded, run_output) =
                render_run_source_code_output(&runtime, &stmt_results, &runtime_error, false);

            if !run_succeeded {
                panic!("docs litex snippet FAILED: {}\n{}\n", label, run_output);
            }

            doc_durations_ms.push((label.clone(), duration_ms));
        }
        let docs_phase_wall_ms = docs_wall_start.elapsed().as_secs_f64() * 1000.0;

        for (label, duration_ms) in doc_durations_ms.iter() {
            println!("  OK  {:.2} ms  {}", duration_ms, label);
        }
        print_run_examples_timing_summary(
            builtin_duration_ms,
            examples_ran,
            file_label_and_duration_ms_list.as_slice(),
            examples_phase_wall_ms,
            doc_durations_ms.as_slice(),
            docs_phase_wall_ms,
        );
    }
}