Skip to content

Commit

Permalink
make fmt happy
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 24, 2023
1 parent 37351bc commit fc3479b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn compile_file(db: &dyn crate::Db, input_file: InputFile) -> Chunk {
syntax::Stmt::Print(expr) => {
compile_expr(db, expr, &mut chunk);
chunk.emit_byte(Code::Print)
},
}
syntax::Stmt::Var { name, initializer } => {
if let Some(initializer) = initializer {
compile_expr(db, initializer, &mut chunk);
Expand Down Expand Up @@ -79,6 +79,6 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) {
syntax::Expr::Variable(word) => {
let word_str = word.as_str(db);
chunk.emit_byte(Code::Variable(word_str.to_string()))
},
}
}
}
8 changes: 4 additions & 4 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,18 @@ impl VM {
let value = self.pop();
// FIXME: This should be a call to a intrinsic function.
println!("{:?}", value);
},
}
bytecode::Code::VarDeclaration(name) => {
let value = self.pop();
self.globals.insert(name, value);
},
}
bytecode::Code::Nil => {
self.push(Value::Nil);
},
}
bytecode::Code::Variable(name) => {
let value = self.globals.get(&name).expect("variable not found");
self.push(value.clone());
},
}
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
4 changes: 2 additions & 2 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum Code {
LessEqual,
String(String),
Print,
VarDeclaration(String),
Variable(String),
VarDeclaration(String),
Variable(String),
Nil,
}

Expand Down
2 changes: 1 addition & 1 deletion components/lox-ir/src/kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ pub struct Keywords {}
#[salsa::tracked(return_ref)]
pub(crate) fn keywords_map(db: &dyn crate::Db, _k: Keywords) -> HashMap<Word, Keyword> {
Keyword::all().map(|kw| (kw.word(db), kw)).collect()
}
}
2 changes: 1 addition & 1 deletion components/lox-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Jar(
token_tree::TokenTree,
diagnostic::Diagnostics,
kw::Keywords,
kw::keywords_map
kw::keywords_map,
);

pub trait Db: salsa::DbWithJar<Jar> {}
33 changes: 21 additions & 12 deletions components/lox-ir/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl<'db> salsa::DebugWithDb<dyn crate::Db + 'db> for Expr {
}
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Stmt {
// expression statement, like `1 + 2;`
Expand All @@ -75,20 +74,30 @@ pub enum Stmt {
Var {
name: Word,
initializer: Option<Expr>,
}
},
}

impl salsa::DebugWithDb<dyn crate::Db> for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db, _include_all_fields: bool) -> std::fmt::Result {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
db: &dyn crate::Db,
_include_all_fields: bool,
) -> std::fmt::Result {
match self {
Stmt::Expr(expr) => f.debug_struct("Expr").field("expr", &expr.debug(db)).finish(),
Stmt::Print(expr) => f.debug_struct("Print").field("expr", &expr.debug(db)).finish(),
Stmt::Var { name, initializer } => {
f.debug_struct("Var")
.field("name", &name.as_str(db))
.field("initializer", &initializer.debug(db))
.finish()
}
Stmt::Expr(expr) => f
.debug_struct("Expr")
.field("expr", &expr.debug(db))
.finish(),
Stmt::Print(expr) => f
.debug_struct("Print")
.field("expr", &expr.debug(db))
.finish(),
Stmt::Var { name, initializer } => f
.debug_struct("Var")
.field("name", &name.as_str(db))
.field("initializer", &initializer.debug(db))
.finish(),
}
}
}
}
2 changes: 1 addition & 1 deletion components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lox_ir::{
};

use crate::{
token_test::{AnyTree, Number, StringLiteral, TokenTest, Identifier},
token_test::{AnyTree, Identifier, Number, StringLiteral, TokenTest},
tokens::Tokens,
};

Expand Down
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::{
path::{Path, PathBuf},
};

use clap::{Parser, Subcommand};
use expect_test::expect_file;
use lox_ir::{bytecode, diagnostic::Diagnostics, input_file::InputFile, word::Word};
use salsa::DebugWithDb;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use clap::{Parser, Subcommand};


#[salsa::db(
lox_parse::Jar,
Expand Down Expand Up @@ -43,7 +42,7 @@ impl TestCase {
let lox = TestCase::absolute_path(lox);
if lox.extension().unwrap_or_default() != "lox" {
panic!("expected lox file, got {}", lox.display());
}
}

// if the lox file is `foo/bar.lox`, then the generated files will be
// `foo/bar/{token,syntax,bytecode,execute}`
Expand Down Expand Up @@ -164,7 +163,6 @@ fn main() {
.with(EnvFilter::from_default_env())
.init();


let cli = Cli::parse();

let db = Database::default();
Expand All @@ -184,6 +182,6 @@ fn main() {
let test_case = TestCase::new(&path);
test_case.test(&db);
}
},
}
}
}

0 comments on commit fc3479b

Please sign in to comment.