Skip to content

Commit

Permalink
diagnose functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Nov 11, 2023
1 parent 16b8b3b commit 8fc5e3e
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn compile_file(db: &dyn crate::Db, input_file: InputFile) -> CompiledFuncti
}

#[salsa::tracked]
pub fn compile(db: &dyn crate::Db, function: lox_ir::function::Function) -> CompiledFunction {
pub fn compile_fn(db: &dyn crate::Db, function: lox_ir::function::Function) -> CompiledFunction {
let stmts = function.parse(db);

// Whether the scope depth is 0 or not determines the variable type, global or local
Expand Down
4 changes: 2 additions & 2 deletions components/lox-compile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

pub mod compile;
pub use compile::compile_file;
pub mod prelude;
pub use compile::compile_fn;

#[salsa::jar(db = Db)]
pub struct Jar(compile::compile_file, compile::compile);
pub struct Jar(compile::compile_file, compile::compile_fn);

pub trait Db: salsa::DbWithJar<Jar> + lox_ir::Db + lox_parse::Db {}
impl<T> Db for T where T: salsa::DbWithJar<Jar> + lox_ir::Db + lox_parse::Db {}
13 changes: 0 additions & 13 deletions components/lox-compile/src/prelude.rs

This file was deleted.

1 change: 1 addition & 0 deletions components/lox-execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ salsa = { path="../salsa" }
lox-ir = { path="../lox-ir" }
lox-lex = { path="../lox-lex" }
lox-compile = { path="../lox-compile" }
lox-error-format = { path="../lox-error-format" }
tracing = "0.1.37"
generational-arena = "0.2.9"
5 changes: 3 additions & 2 deletions components/lox-execute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pub use vm::VM;
#[salsa::jar(db = Db)]
pub struct Jar(execute::main_function);

pub trait Db: salsa::DbWithJar<Jar> + lox_ir::Db + lox_compile::Db {}
impl<T> Db for T where T: salsa::DbWithJar<Jar> + lox_ir::Db + lox_compile::Db {}
pub trait Db: salsa::DbWithJar<Jar> + lox_ir::Db + lox_compile::Db + lox_error_format::Db {}
impl<T> Db for T where T: salsa::DbWithJar<Jar> + lox_ir::Db + lox_compile::Db + lox_error_format::Db
{}
21 changes: 17 additions & 4 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;

use lox_compile::prelude::FunctionCompileExt;
use lox_compile::compile_fn;
use lox_ir::{
bytecode::{self, CompiledFunction},
diagnostic::Diagnostics,
function::Function,
};

Expand Down Expand Up @@ -188,7 +189,7 @@ pub struct VM {

impl VM {
pub fn new(main: Function, db: &dyn crate::Db) -> Self {
let function = main.compile(db);
let function = compile_fn(db, main);
let frame = CallFrame {
function,
ip: 0,
Expand Down Expand Up @@ -422,8 +423,20 @@ impl VM {
let closure = self.peek_n_from_top(arity);
match closure {
Value::Function(function) => {
let compiled_function = function.compile(db);
self.push_frame(compiled_function);
let compiled_function = compile_fn(db, function.clone());
let diagnostics =
compile_fn::accumulated::<Diagnostics>(db, function.clone());
if diagnostics.is_empty() {
self.push_frame(compiled_function);
} else {
let output = lox_error_format::format_diagnostics_with_options(
db,
&diagnostics,
lox_error_format::FormatOptions::no_color(),
)
.unwrap();
kernel.print(&output);
}
}
_ => panic!("Cannot call {:?}", closure),
}
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions lox_tests/diagnostics/error_in_function/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Error: expected `;`
╭─[/home/zhoufan/workspace/rust/lox/lox_tests/diagnostics/error_in_function.lox:2:5]
2 │ print "hello"
│ ┬
│ ╰── here
───╯

File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion lox_tests/diagnostics/hello/output

This file was deleted.

5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,9 @@ fn main() {
lox_compile::compile_file(&db, input_file);
let diagnostics =
lox_compile::compile_file::accumulated::<Diagnostics>(&db, input_file);
dbg!(&diagnostics);
if !diagnostics.is_empty() {
for diagnostic in &diagnostics {
lox_error_format::print_diagnostic(&db, diagnostic).unwrap();
println!(
"{}",
lox_error_format::format_diagnostics(&db, &diagnostics).unwrap()
);
}
} else {
lox_execute::execute_file(
Expand Down

0 comments on commit 8fc5e3e

Please sign in to comment.