This implements an initial fancy display for warnings emitted by the tvix compiler, using the codemap_diagnostic crate. Each warning variant has an associated message, and optionally an associated annotation for the span displayed to the user. In theory we could get a lot more fancy with the display for specific variants if needed (e.g. re-parse the AST and actually add multiple semantic spans based on context), but this is already a good start. Example: tvix-repl> let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} warning[W004]: declared variable 'toString' shadows a built-in global! --> [tvix-repl]:1:5 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^^^^^^^ variable declared here warning[W001]: URL literal syntax is deprecated, use a quoted string instead --> [tvix-repl]:1:16 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^^^^^^^^^^^^^^ warning[W002]: inherited variable already exists with the same value --> [tvix-repl]:1:40 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^^^^^^^^^^^^^^^^ warning[W999]: feature not yet implemented in tvix: recursive attribute sets --> [tvix-repl]:1:70 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^^^^^ warning[W999]: feature not yet implemented in tvix: closed formals --> [tvix-repl]:1:62 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^ warning[W003]: variable 'toString' is declared, but never used: --> [tvix-repl]:1:5 | 1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {} | ^^^^^^^^ variable declared here => 42 :: int These are coloured when output to a terminal. Change-Id: If315648a07e333895db4ae1d0915ee2013806585 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6532 Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI
84 lines
2.3 KiB
Rust
84 lines
2.3 KiB
Rust
use std::{path::PathBuf, rc::Rc};
|
|
|
|
use crate::{
|
|
builtins::global_builtins,
|
|
errors::{Error, ErrorKind, EvalResult},
|
|
observer::{DisassemblingObserver, NoOpObserver, TracingObserver},
|
|
value::Value,
|
|
};
|
|
|
|
pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
|
|
let mut codemap = codemap::CodeMap::new();
|
|
let file = codemap.add_file(
|
|
location
|
|
.as_ref()
|
|
.map(|p| p.to_string_lossy().to_string())
|
|
.unwrap_or_else(|| "[tvix-repl]".into()),
|
|
code.into(),
|
|
);
|
|
let codemap = Rc::new(codemap);
|
|
|
|
let parsed = rnix::ast::Root::parse(code);
|
|
let errors = parsed.errors();
|
|
|
|
if !errors.is_empty() {
|
|
for err in errors {
|
|
eprintln!("parse error: {}", err);
|
|
}
|
|
return Err(Error {
|
|
kind: ErrorKind::ParseErrors(errors.to_vec()),
|
|
span: file.span,
|
|
});
|
|
}
|
|
|
|
// If we've reached this point, there are no errors.
|
|
let root_expr = parsed
|
|
.tree()
|
|
.expr()
|
|
.expect("expression should exist if no errors occured");
|
|
|
|
if std::env::var("TVIX_DISPLAY_AST").is_ok() {
|
|
println!("{:?}", root_expr);
|
|
}
|
|
|
|
let result = if std::env::var("TVIX_DUMP_BYTECODE").is_ok() {
|
|
crate::compiler::compile(
|
|
root_expr,
|
|
location,
|
|
&file,
|
|
global_builtins(),
|
|
&mut DisassemblingObserver::new(codemap.clone(), std::io::stderr()),
|
|
)
|
|
} else {
|
|
crate::compiler::compile(
|
|
root_expr,
|
|
location,
|
|
&file,
|
|
global_builtins(),
|
|
&mut NoOpObserver::default(),
|
|
)
|
|
}?;
|
|
|
|
for warning in result.warnings {
|
|
warning.fancy_format_stderr(&codemap);
|
|
}
|
|
|
|
for error in &result.errors {
|
|
eprintln!(
|
|
"compiler error: {:?} at `{}`[line {}]",
|
|
error.kind,
|
|
file.source_slice(error.span),
|
|
file.find_line(error.span.low()) + 1
|
|
);
|
|
}
|
|
|
|
if let Some(err) = result.errors.last() {
|
|
return Err(err.clone());
|
|
}
|
|
|
|
if std::env::var("TVIX_TRACE_RUNTIME").is_ok() {
|
|
crate::vm::run_lambda(&mut TracingObserver::new(std::io::stderr()), result.lambda)
|
|
} else {
|
|
crate::vm::run_lambda(&mut NoOpObserver::default(), result.lambda)
|
|
}
|
|
}
|