salsa/examples-2022/calc/src/ir.rs

109 lines
1.9 KiB
Rust
Raw Normal View History

2022-08-24 16:43:29 +00:00
#![allow(clippy::needless_borrow)]
use derive_new::new;
2022-08-01 05:32:47 +00:00
use ordered_float::OrderedFloat;
// ANCHOR: input
#[salsa::input]
pub struct SourceProgram {
#[return_ref]
pub text: String,
}
// ANCHOR_END: input
2022-08-01 05:32:47 +00:00
// ANCHOR: interned_ids
#[salsa::interned]
pub struct VariableId {
#[return_ref]
pub text: String,
}
#[salsa::interned]
pub struct FunctionId {
#[return_ref]
pub text: String,
}
// ANCHOR_END: interned_ids
// ANCHOR: program
2022-08-19 09:53:33 +00:00
#[salsa::tracked]
pub struct Program {
#[return_ref]
pub statements: Vec<Statement>,
2022-08-19 09:53:33 +00:00
}
// ANCHOR_END: program
2022-08-19 09:53:33 +00:00
// ANCHOR: statements_and_expressions
#[derive(Eq, PartialEq, Debug, Hash, new)]
pub struct Statement {
pub span: Span,
pub data: StatementData,
}
#[derive(Eq, PartialEq, Debug, Hash)]
pub enum StatementData {
2022-08-01 05:32:47 +00:00
/// Defines `fn <name>(<args>) = <body>`
Function(Function),
/// Defines `print <expr>`
Print(Expression),
}
#[derive(Eq, PartialEq, Debug, Hash, new)]
pub struct Expression {
pub span: Span,
pub data: ExpressionData,
}
#[derive(Eq, PartialEq, Debug, Hash)]
pub enum ExpressionData {
Op(Box<Expression>, Op, Box<Expression>),
2022-08-01 05:32:47 +00:00
Number(OrderedFloat<f64>),
Variable(VariableId),
Call(FunctionId, Vec<Expression>),
}
#[derive(Eq, PartialEq, Copy, Clone, Hash, Debug)]
pub enum Op {
Add,
Subtract,
Multiply,
Divide,
}
// ANCHOR_END: statements_and_expressions
// ANCHOR: functions
#[salsa::tracked]
2022-08-01 05:32:47 +00:00
pub struct Function {
#[id]
pub name: FunctionId,
name_span: Span,
#[return_ref]
pub args: Vec<VariableId>,
#[return_ref]
pub body: Expression,
2022-08-01 05:32:47 +00:00
}
// ANCHOR_END: functions
#[salsa::tracked]
pub struct Span {
pub start: usize,
pub end: usize,
}
2022-08-01 05:32:47 +00:00
// ANCHOR: diagnostic
#[salsa::accumulator]
pub struct Diagnostics(Diagnostic);
#[derive(new, Clone, Debug)]
2022-08-01 05:32:47 +00:00
pub struct Diagnostic {
pub start: usize,
pub end: usize,
2022-08-01 05:32:47 +00:00
pub message: String,
}
// ANCHOR_END: diagnostic