crisp-lang 1.8.0

Crisp language toolchain — .crp to Rust to native (bins: crisp, reveal)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use clap::{Parser, Subcommand};
use crisp_diagnostics::{
    Severity, format_diagnostic_at, format_parse_error, format_unresolved_name,
};
use crisp_errors::ErrorPass;
use crisp_ownership::OwnershipPass;
use crisp_parser::{ParseError, Parser as CrispParser};
use crisp_regions::RegionPass;
use crisp_resolve::module::load_module_graph;
use crisp_resolve::{ResolveError, Resolver, find_crate_root};
use crisp_rust_emit::{
    PipelineError, TestHarnessError, build_emitted, emit_to_target, resolve_rustc_fallbacks,
    run_emitted, run_tests, verify_sealed_api,
};
use crisp_typeck::{TypeChecker, TypeError};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Parser)]
#[command(
    name = "crisp",
    version,
    about = "Crisp language toolchain — .crp to Rust to native"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Parse .crp source and print AST (debug)
    Parse { file: PathBuf },
    /// Resolve modules, imports, and names for a crate
    Resolve {
        #[arg(default_value = ".")]
        path: PathBuf,
    },
    /// Analyze, emit Rust, invoke rustc
    Build {
        #[arg(default_value = ".")]
        path: String,
    },
    /// Build and run
    Run {
        #[arg(default_value = ".")]
        path: String,
    },
    /// Run tests
    Test {
        /// Crate root(s). Several paths may be given; a pasted `and` is ignored.
        #[arg(default_value = ".")]
        paths: Vec<String>,
    },
    /// Resolve + typecheck (fast)
    Check {
        #[arg(default_value = ".")]
        path: String,
    },
    /// Emit Rust to target/rust/ and stop
    Emit {
        #[arg(default_value = ".")]
        path: String,
    },
}

fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt::init();

    let cli = Cli::parse();
    match cli.command {
        Commands::Parse { file } => {
            let src = fs::read_to_string(&file)?;
            match CrispParser::new(&src).and_then(|mut p| p.parse_file()) {
                Ok(module) => {
                    println!("{module:#?}");
                    Ok(())
                }
                Err(e) => {
                    print_parse_error(&file, &src, &e);
                    Err(e.into())
                }
            }
        }
        Commands::Resolve { path } => {
            let root = find_crate_root(&path).unwrap_or(path);
            match Resolver::resolve_crate(&root) {
                Ok(resolved) => {
                    print_resolve_warnings(&resolved.warnings);
                    println!("{resolved:#?}");
                    Ok(())
                }
                Err(e) => {
                    print_resolve_diagnostic(&root, &e);
                    Err(e.into())
                }
            }
        }
        Commands::Check { path } => {
            let root = find_crate_root(PathBuf::from(&path).as_path())
                .unwrap_or_else(|| PathBuf::from(&path));
            match Resolver::resolve_crate(&root) {
                Err(e) => {
                    print_resolve_diagnostic(&root, &e);
                    return Err(e.into());
                }
                Ok(resolved) => print_resolve_warnings(&resolved.warnings),
            }
            match TypeChecker::check_crate(&root) {
                Err(e) => {
                    print_type_diagnostic(&root, &e);
                    return Err(e.into());
                }
                Ok(typed) => print_type_warnings(&root, &typed.warnings),
            }
            match resolve_rustc_fallbacks(&root) {
                Ok(_) => {}
                Err(crisp_rust_emit::FallbackResolveError::RustcUnavailable) => {
                    OwnershipPass::analyze_crate(&root)?;
                }
                Err(e) => return Err(e.into()),
            }
            RegionPass::assign_crate(&root)?;
            ErrorPass::analyze_crate(&root)?;
            verify_sealed_api(&root)?;
            eprintln!("crisp check: ok ({})", root.display());
            Ok(())
        }
        Commands::Build { path } => {
            let root = find_crate_root(PathBuf::from(&path).as_path())
                .unwrap_or_else(|| PathBuf::from(&path));
            match build_emitted(&root) {
                Ok(out_dir) => {
                    eprintln!("crisp build: ok ({})", out_dir.display());
                    Ok(())
                }
                Err(PipelineError::ToolchainUnavailable) => {
                    eprintln!("crisp build: emitted to target/rust/ (cargo not on PATH)");
                    emit_to_target(&root)?;
                    std::process::exit(1);
                }
                Err(e) => Err(e.into()),
            }
        }
        Commands::Run { path } => {
            let root = find_crate_root(PathBuf::from(&path).as_path())
                .unwrap_or_else(|| PathBuf::from(&path));
            match run_emitted(&root) {
                Ok(stdout) => {
                    print!("{stdout}");
                    Ok(())
                }
                Err(PipelineError::ToolchainUnavailable) => {
                    eprintln!("crisp run: cargo not on PATH");
                    std::process::exit(1);
                }
                Err(e) => Err(e.into()),
            }
        }
        Commands::Test { paths } => {
            for path in normalize_crate_paths(paths) {
                let root = find_crate_root(PathBuf::from(&path).as_path())
                    .unwrap_or_else(|| PathBuf::from(&path));
                match run_tests(&root) {
                    Ok(report) => {
                        eprintln!(
                            "crisp test: ok {} ({} runtime, {} compile-fail)",
                            root.display(),
                            report.runtime_passed,
                            report.compile_fail_passed
                        );
                    }
                    Err(TestHarnessError::Other(e))
                        if e.to_string().contains("cargo not on PATH") =>
                    {
                        eprintln!("crisp test: cargo not on PATH");
                        std::process::exit(1);
                    }
                    Err(e) => return Err(e.into()),
                }
            }
            Ok(())
        }
        Commands::Emit { path } => {
            let root = find_crate_root(PathBuf::from(&path).as_path())
                .unwrap_or_else(|| PathBuf::from(&path));
            let out = emit_to_target(&root)?;
            eprintln!("crisp emit: ok ({})", out.out_dir.display());
            Ok(())
        }
    }
}

fn print_parse_error(file: &Path, source: &str, err: &ParseError) {
    let mut extras = Vec::new();
    if let Some(h) = err.help() {
        extras.push(format!("help: {h}"));
    }
    let name = file.display().to_string();
    let rendered = format_parse_error(
        &name,
        source,
        err.diagnostic_code(),
        &err.primary_message(),
        err.byte_pos(),
        &extras,
    )
    .rendered;
    eprintln!("{rendered}");
}

fn print_resolve_warnings(warnings: &[crisp_resolve::ResolveWarning]) {
    for w in warnings {
        eprintln!("warning: {w}");
    }
}

fn print_type_warnings(root: &Path, warnings: &[crisp_typeck::TypeWarning]) {
    for w in warnings {
        if let Some((file, source)) = source_for_span(root, w.span()) {
            let rendered = format_diagnostic_at(
                &file,
                &source,
                w.code(),
                &w.to_string().replacen(&format!("[{}] ", w.code()), "", 1),
                w.span(),
                Severity::Warning,
                &[],
            )
            .rendered;
            eprintln!("{rendered}");
        } else {
            eprintln!("warning: {w}");
        }
    }
}

fn print_resolve_diagnostic(root: &Path, err: &ResolveError) {
    match err {
        ResolveError::Parse { path, message, pos } => {
            if let Ok(source) = fs::read_to_string(path) {
                let rel = Path::new(path)
                    .strip_prefix(root)
                    .unwrap_or(Path::new(path))
                    .display()
                    .to_string();
                let code = if message.starts_with("lex error:") {
                    "E0011"
                } else {
                    "E0010"
                };
                let rendered = format_parse_error(&rel, &source, code, message, *pos, &[]).rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        ResolveError::UnresolvedName {
            name, span, hint, ..
        } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered =
                    format_unresolved_name(&file, &source, name, *span, hint.as_deref()).rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        ResolveError::ShapesUnsupported { name, span } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0039",
                    &format!("shapes are not yet supported (`{name}`)"),
                    *span,
                    Severity::Error,
                    &["help: remove the `shape` definition or bound".into()],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        _ => {}
    }
    eprintln!("{err}");
}

fn print_type_diagnostic(root: &Path, err: &TypeError) {
    match err {
        TypeError::UnknownName { name, span } | TypeError::UnknownType { name, span } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let code = if matches!(err, TypeError::UnknownType { .. }) {
                    "E0040"
                } else {
                    "E0041"
                };
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    code,
                    &err.to_string().replacen(&format!("[{code}] "), "", 1),
                    *span,
                    Severity::Error,
                    &[],
                )
                .rendered;
                eprintln!("{rendered}");
                let _ = name;
                return;
            }
        }
        TypeError::AmbiguousField {
            field,
            candidates,
            span,
        } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0043",
                    &format!(
                        "ambiguous field `{field}` on unresolved type; annotate the parameter (candidates: {candidates})"
                    ),
                    *span,
                    Severity::Error,
                    &["help: write `param: StructName` on the function parameter".into()],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        TypeError::Resolve(inner) => {
            print_resolve_diagnostic(root, inner);
            return;
        }
        TypeError::UnifyAt { message, span } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0041",
                    message,
                    *span,
                    Severity::Error,
                    &[],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        TypeError::InvalidCast { span, .. } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0087",
                    &err.to_string().replacen("[E0087] ", "", 1),
                    *span,
                    Severity::Error,
                    &[],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        TypeError::UndeclaredRustImport { span, .. } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0089",
                    &err.to_string().replacen("[E0089] ", "", 1),
                    *span,
                    Severity::Error,
                    &["help: declare it in `extern rust <crate> { item(…) -> … }` or a `.crpi` sidecar".into()],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        TypeError::InvalidExternRustTy { span, .. } => {
            if let Some((file, source)) = source_for_span(root, *span) {
                let rendered = format_diagnostic_at(
                    &file,
                    &source,
                    "E0090",
                    &err.to_string().replacen("[E0090] ", "", 1),
                    *span,
                    Severity::Error,
                    &[],
                )
                .rendered;
                eprintln!("{rendered}");
                return;
            }
        }
        _ => {}
    }
    eprintln!("{err}");
}

/// Drop copy-pasted command glue (`and`, `crisp test`, …) when those tokens are not paths.
fn normalize_crate_paths(raw: Vec<String>) -> Vec<String> {
    const GLUE: &[&str] = &[
        "and", "&&", "crisp", "test", "check", "run", "build", "emit",
    ];
    let out: Vec<String> = raw
        .into_iter()
        .filter(|p| !GLUE.contains(&p.as_str()) || Path::new(p).exists())
        .collect();
    if out.is_empty() {
        vec![".".into()]
    } else {
        out
    }
}

/// Best-effort: find a module source whose length covers `span.end`.
fn source_for_span(root: &Path, span: crisp_ast::Span) -> Option<(String, String)> {
    let graph = load_module_graph(root).ok()?;
    let mut best: Option<(String, String)> = None;
    for node in graph.modules.values() {
        let Ok(source) = fs::read_to_string(&node.path) else {
            continue;
        };
        if (source.len() as u32) < span.end {
            continue;
        }
        let rel = node
            .path
            .strip_prefix(root)
            .unwrap_or(node.path.as_path())
            .display()
            .to_string();
        best = Some((rel, source));
        // Prefer main when multiple match.
        if node.module_path == "main" {
            break;
        }
    }
    best
}