From d83d3c44f8b7184c931fb7fe4604bf4a9d9d5d01 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 19 Aug 2022 05:53:33 -0400 Subject: [PATCH] package up the output into a `Program` --- calc-example/calc/src/ir.rs | 13 +++ calc-example/calc/src/main.rs | 1 + calc-example/calc/src/parser.rs | 172 +++++++++----------------------- 3 files changed, 62 insertions(+), 124 deletions(-) diff --git a/calc-example/calc/src/ir.rs b/calc-example/calc/src/ir.rs index a21c278f..87d7eb67 100644 --- a/calc-example/calc/src/ir.rs +++ b/calc-example/calc/src/ir.rs @@ -24,6 +24,11 @@ pub struct FunctionId { // ANCHOR_END: interned_ids // ANCHOR: statements_and_expressions +#[salsa::tracked] +pub struct Program { + statements: Vec, +} + #[salsa::interned] pub struct Statement { data: StatementData, @@ -105,6 +110,14 @@ impl DebugWithDb for Expression { } // ANCHOR_END: expression_debug_impl +impl DebugWithDb for Program { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db) -> std::fmt::Result { + f.debug_struct("Program") + .field("statements", &self.statements(db)) + .finish() + } +} + impl DebugWithDb for FunctionId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db) -> std::fmt::Result { write!(f, "{:?}", self.text(db)) diff --git a/calc-example/calc/src/main.rs b/calc-example/calc/src/main.rs index 905d3d5a..96a7c709 100644 --- a/calc-example/calc/src/main.rs +++ b/calc-example/calc/src/main.rs @@ -2,6 +2,7 @@ #[salsa::jar(db = Db)] pub struct Jar( crate::ir::SourceProgram, + crate::ir::Program, crate::ir::VariableId, crate::ir::FunctionId, crate::ir::Expression, diff --git a/calc-example/calc/src/parser.rs b/calc-example/calc/src/parser.rs index 21a4b261..2236cbb9 100644 --- a/calc-example/calc/src/parser.rs +++ b/calc-example/calc/src/parser.rs @@ -1,13 +1,13 @@ use ordered_float::OrderedFloat; use crate::ir::{ - Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, SourceProgram, - Statement, StatementData, VariableId, + Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, Program, + SourceProgram, Statement, StatementData, VariableId, }; // ANCHOR: parse_statements #[salsa::tracked(return_ref)] -pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Vec { +pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Program { // Get the source text from the database let source_text = source.text(db); @@ -41,7 +41,7 @@ pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Vec