package up the output into a Program

This commit is contained in:
Niko Matsakis 2022-08-19 05:53:33 -04:00
parent 80d0d14194
commit d83d3c44f8
3 changed files with 62 additions and 124 deletions

View file

@ -24,6 +24,11 @@ pub struct FunctionId {
// ANCHOR_END: interned_ids // ANCHOR_END: interned_ids
// ANCHOR: statements_and_expressions // ANCHOR: statements_and_expressions
#[salsa::tracked]
pub struct Program {
statements: Vec<Statement>,
}
#[salsa::interned] #[salsa::interned]
pub struct Statement { pub struct Statement {
data: StatementData, data: StatementData,
@ -105,6 +110,14 @@ impl DebugWithDb<dyn crate::Db + '_> for Expression {
} }
// ANCHOR_END: expression_debug_impl // ANCHOR_END: expression_debug_impl
impl DebugWithDb<dyn crate::Db + '_> 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<dyn crate::Db + '_> for FunctionId { impl DebugWithDb<dyn crate::Db + '_> for FunctionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &dyn crate::Db) -> std::fmt::Result {
write!(f, "{:?}", self.text(db)) write!(f, "{:?}", self.text(db))

View file

@ -2,6 +2,7 @@
#[salsa::jar(db = Db)] #[salsa::jar(db = Db)]
pub struct Jar( pub struct Jar(
crate::ir::SourceProgram, crate::ir::SourceProgram,
crate::ir::Program,
crate::ir::VariableId, crate::ir::VariableId,
crate::ir::FunctionId, crate::ir::FunctionId,
crate::ir::Expression, crate::ir::Expression,

View file

@ -1,13 +1,13 @@
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use crate::ir::{ use crate::ir::{
Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, SourceProgram, Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, Program,
Statement, StatementData, VariableId, SourceProgram, Statement, StatementData, VariableId,
}; };
// ANCHOR: parse_statements // ANCHOR: parse_statements
#[salsa::tracked(return_ref)] #[salsa::tracked(return_ref)]
pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Vec<Statement> { pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Program {
// Get the source text from the database // Get the source text from the database
let source_text = source.text(db); let source_text = source.text(db);
@ -41,7 +41,7 @@ pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Vec<Statem
} }
} }
result Program::new(db, result)
} }
// ANCHOR_END: parse_statements // ANCHOR_END: parse_statements
@ -352,21 +352,15 @@ fn parse_print() {
let actual = parse_string("print 1 + 2"); let actual = parse_string("print 1 + 2");
let expected = expect_test::expect![[r#" let expected = expect_test::expect![[r#"
( (
[ Program {
ExpressionData::Op( statements: [
Number( Statement(
OrderedFloat( Id {
1.0, value: 1,
), },
),
Add,
Number(
OrderedFloat(
2.0,
),
),
), ),
], ],
},
[], [],
)"#]]; )"#]];
expected.assert_eq(&actual); expected.assert_eq(&actual);
@ -386,85 +380,35 @@ fn parse_example() {
); );
let expected = expect_test::expect![[r#" let expected = expect_test::expect![[r#"
( (
[ Program {
Function { statements: [
name: "area_rectangle", Statement(
args: [ Id {
"w", value: 1,
"h",
],
body: ExpressionData::Op(
Variable(
"w",
),
Multiply,
Variable(
"h",
),
),
}, },
Function {
name: "area_circle",
args: [
"r",
],
body: ExpressionData::Op(
ExpressionData::Op(
Number(
OrderedFloat(
3.14,
),
),
Multiply,
Variable(
"r",
),
),
Multiply,
Variable(
"r",
),
), ),
Statement(
Id {
value: 2,
}, },
Call(
"area_rectangle",
[
Number(
OrderedFloat(
3.0,
), ),
Statement(
Id {
value: 3,
},
), ),
Number( Statement(
OrderedFloat( Id {
4.0, value: 4,
), },
),
],
),
Call(
"area_circle",
[
Number(
OrderedFloat(
1.0,
),
),
],
),
ExpressionData::Op(
Number(
OrderedFloat(
11.0,
),
),
Multiply,
Number(
OrderedFloat(
2.0,
),
), ),
Statement(
Id {
value: 5,
},
), ),
], ],
},
[], [],
)"#]]; )"#]];
expected.assert_eq(&actual); expected.assert_eq(&actual);
@ -477,7 +421,9 @@ fn parse_error() {
let actual = parse_string(source_text); let actual = parse_string(source_text);
let expected = expect_test::expect![[r#" let expected = expect_test::expect![[r#"
( (
[], Program {
statements: [],
},
[ [
Diagnostic { position: 10, message: "unexpected character" }, Diagnostic { position: 10, message: "unexpected character" },
], ],
@ -492,37 +438,15 @@ fn parse_precedence() {
let actual = parse_string(source_text); let actual = parse_string(source_text);
let expected = expect_test::expect![[r#" let expected = expect_test::expect![[r#"
( (
[ Program {
ExpressionData::Op( statements: [
ExpressionData::Op( Statement(
Number( Id {
OrderedFloat( value: 1,
1.0, },
),
),
Add,
ExpressionData::Op(
Number(
OrderedFloat(
2.0,
),
),
Multiply,
Number(
OrderedFloat(
3.0,
),
),
),
),
Add,
Number(
OrderedFloat(
4.0,
),
),
), ),
], ],
},
[], [],
)"#]]; )"#]];
expected.assert_eq(&actual); expected.assert_eq(&actual);