2022-08-19 10:46:54 +00:00
|
|
|
use derive_new::new;
|
2022-08-01 05:32:47 +00:00
|
|
|
use ordered_float::OrderedFloat;
|
|
|
|
use salsa::debug::DebugWithDb;
|
|
|
|
|
2022-08-03 04:41:36 +00:00
|
|
|
// ANCHOR: input
|
|
|
|
#[salsa::input]
|
|
|
|
pub struct SourceProgram {
|
|
|
|
#[return_ref]
|
|
|
|
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
|
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
// ANCHOR: program
|
2022-08-19 09:53:33 +00:00
|
|
|
#[salsa::tracked]
|
|
|
|
pub struct Program {
|
2022-08-19 10:46:54 +00:00
|
|
|
#[return_ref]
|
2022-08-19 09:53:33 +00:00
|
|
|
statements: Vec<Statement>,
|
|
|
|
}
|
2022-08-19 10:46:54 +00:00
|
|
|
// ANCHOR_END: program
|
2022-08-19 09:53:33 +00:00
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
// ANCHOR: statements_and_expressions
|
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, new)]
|
2022-08-03 07:43:39 +00:00
|
|
|
pub struct Statement {
|
2022-08-19 12:41:58 +00:00
|
|
|
pub span: Span,
|
2022-08-19 10:46:54 +00:00
|
|
|
|
2022-08-19 12:41:58 +00:00
|
|
|
pub data: StatementData,
|
2022-08-03 07:43:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Hash)]
|
2022-08-03 07:43:39 +00:00
|
|
|
pub enum StatementData {
|
2022-08-01 05:32:47 +00:00
|
|
|
/// Defines `fn <name>(<args>) = <body>`
|
|
|
|
Function(Function),
|
|
|
|
/// Defines `print <expr>`
|
|
|
|
Print(Expression),
|
|
|
|
}
|
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, new)]
|
2022-08-03 07:43:39 +00:00
|
|
|
pub struct Expression {
|
2022-08-19 12:41:58 +00:00
|
|
|
pub span: Span,
|
2022-08-19 10:46:54 +00:00
|
|
|
|
2022-08-19 12:41:58 +00:00
|
|
|
pub data: ExpressionData,
|
2022-08-03 07:43:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Hash)]
|
2022-08-03 07:43:39 +00:00
|
|
|
pub enum ExpressionData {
|
2022-08-19 10:46:54 +00:00
|
|
|
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
|
2022-08-03 13:09:22 +00:00
|
|
|
#[salsa::tracked]
|
2022-08-01 05:32:47 +00:00
|
|
|
pub struct Function {
|
|
|
|
#[id]
|
|
|
|
name: FunctionId,
|
2022-08-19 10:46:54 +00:00
|
|
|
|
|
|
|
name_span: Span,
|
|
|
|
|
2022-08-19 12:41:58 +00:00
|
|
|
#[return_ref]
|
2022-08-01 05:32:47 +00:00
|
|
|
args: Vec<VariableId>,
|
2022-08-19 10:46:54 +00:00
|
|
|
|
|
|
|
#[return_ref]
|
2022-08-01 05:32:47 +00:00
|
|
|
body: Expression,
|
|
|
|
}
|
|
|
|
// ANCHOR_END: functions
|
|
|
|
|
2022-08-19 10:46:54 +00:00
|
|
|
#[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);
|
|
|
|
|
2022-08-19 12:41:58 +00:00
|
|
|
#[derive(new, Clone, Debug)]
|
2022-08-01 05:32:47 +00:00
|
|
|
pub struct Diagnostic {
|
2022-08-19 12:41:58 +00:00
|
|
|
pub start: usize,
|
|
|
|
pub end: usize,
|
2022-08-01 05:32:47 +00:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
// ANCHOR_END: diagnostic
|