Skip to content

Commit

Permalink
print diagnositics without colors in testing context
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Nov 12, 2023
1 parent 9daa17e commit a158018
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion components/lox-error-format/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lox_ir::input_file::InputFile;
pub struct FormatOptions {
/// Whether or not errors should use rich formatting with colors. This is generally turned on,
/// except in tests, where the escape codes obscure the error messages.
with_color: bool,
pub with_color: bool,
}

impl FormatOptions {
Expand Down
3 changes: 2 additions & 1 deletion components/lox-execute/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ pub fn execute_file(
db: &impl crate::Db,
input_file: InputFile,
kernel: &mut impl Kernel,
diagnostic_with_color: bool,
step_inspect: Option<impl FnMut(Option<bytecode::Code>, &VM) + Clone>,
) {
let main = main_function(db, input_file);
let mut vm = VM::new(main, db);
let mut vm = VM::new(db, main, diagnostic_with_color);

while let ControlFlow::Next = vm.step(db, kernel, step_inspect.clone()) {}
}
16 changes: 13 additions & 3 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use lox_compile::compile_fn;
use lox_error_format::FormatOptions;
use lox_ir::{
bytecode::{self, CompiledFunction},
diagnostic::Diagnostics,
Expand Down Expand Up @@ -183,12 +184,14 @@ pub struct VM {

pub stack: Vec<generational_arena::Index>,

diagnostic_with_color: bool,

// global variables
globals: HashMap<String, Value>,
}

impl VM {
pub fn new(main: Function, db: &dyn crate::Db) -> Self {
pub fn new(db: &dyn crate::Db, main: Function, diagnostic_with_color: bool) -> Self {
let function = compile_fn(db, main);
let frame = CallFrame {
function,
Expand All @@ -206,6 +209,7 @@ impl VM {
frames: vec![frame],
heap,
stack,
diagnostic_with_color,
globals: HashMap::new(),
}
}
Expand Down Expand Up @@ -428,8 +432,14 @@ impl VM {
if diagnostics.is_empty() {
self.push_frame(compiled_function);
} else {
let output =
lox_error_format::format_diagnostics(db, &diagnostics).unwrap();
let output = lox_error_format::format_diagnostics_with_options(
db,
&diagnostics,
FormatOptions {
with_color: self.diagnostic_with_color,
},
)
.unwrap();
kernel.print(&output);
}
}
Expand Down
1 change: 1 addition & 0 deletions components/lox-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Compiler {
&self.db,
self.input_file,
&mut kernel,
true,
None::<fn(_, &lox_execute::VM)>,
);
kernel.take_buffer()
Expand Down
14 changes: 7 additions & 7 deletions lox_tests/diagnostics/error_in_function/output
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: expected `;`
╭─[/home/zhoufan/workspace/rust/lox/lox_tests/diagnostics/error_in_function.lox:2:18]
│
2 │     print "hello"

 ╰── here
───╯
Error: expected `;`
╭─[/home/zhoufan/workspace/rust/lox/lox_tests/diagnostics/error_in_function.lox:2:18]
2 │ print "hello"
│ ┬
│ ╰── here
───╯

24 changes: 8 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use clap::{Parser, Subcommand};
use expect_test::expect_file;
use lox_db::Database;
use lox_error_format::FormatOptions;
use lox_execute::kernel::{BufferKernel, StdoutKernel};
use lox_ir::{diagnostic::Diagnostics, input_file::InputFile, word::Word};
use salsa::DebugWithDb;
Expand All @@ -20,7 +19,6 @@ struct TestCase {
syntax: PathBuf,
bytecode: PathBuf,
execute: PathBuf,
diagnostic: PathBuf,
stdout: PathBuf,
text: String,
}
Expand All @@ -44,7 +42,6 @@ impl TestCase {
let syntax = lox_dir.join("syntax");
let bytecode = lox_dir.join("bytecode");
let execute = lox_dir.join("execute");
let diagnostic = lox_dir.join("diagnostic");
let output = lox_dir.join("output");
let text = fs::read_to_string(&lox).unwrap();
TestCase {
Expand All @@ -53,7 +50,6 @@ impl TestCase {
syntax,
bytecode,
execute,
diagnostic,
stdout: output,
text,
}
Expand Down Expand Up @@ -116,17 +112,6 @@ impl TestCase {
}
expect_file![self.syntax].assert_eq(&buf);

// test diagnostics
let diagnostics = lox_compile::compile_file::accumulated::<Diagnostics>(db, input_file);
let output = lox_error_format::format_diagnostics_with_options(
db,
&diagnostics,
FormatOptions::no_color(),
);
if let Ok(output) = output {
expect_file![self.diagnostic].assert_eq(&output);
}

// test bytecode
let chunk = lox_compile::compile_file(db, input_file);
expect_file![self.bytecode].assert_eq(&format!("{:#?}", chunk));
Expand All @@ -145,7 +130,13 @@ impl TestCase {

// test stdout
let mut kernel = BufferKernel::new();
lox_execute::execute_file(db, input_file, &mut kernel, None::<fn(_, &lox_execute::VM)>);
lox_execute::execute_file(
db,
input_file,
&mut kernel,
false,
None::<fn(_, &lox_execute::VM)>,
);
expect_file![self.stdout].assert_eq(kernel.buffer());

println!("ok");
Expand Down Expand Up @@ -226,6 +217,7 @@ fn main() {
&db,
input_file,
&mut StdoutKernel {},
true,
None::<fn(_, &lox_execute::VM)>,
);
}
Expand Down

0 comments on commit a158018

Please sign in to comment.