2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::{
|
2024-01-06 13:11:09 +00:00
|
|
|
AbstractTree, Assignment, Block, Error, Expression, For, Format, IfElse, IndexAssignment, Map,
|
2024-01-10 20:03:52 +00:00
|
|
|
Match, Result, SyntaxNode, Type, Value, While,
|
2023-10-06 17:32:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Abstract representation of a statement.
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub enum Statement {
|
|
|
|
Assignment(Box<Assignment>),
|
|
|
|
Expression(Expression),
|
|
|
|
IfElse(Box<IfElse>),
|
|
|
|
Match(Match),
|
2023-10-07 02:45:36 +00:00
|
|
|
While(Box<While>),
|
2023-11-11 01:44:03 +00:00
|
|
|
Block(Box<Block>),
|
2023-12-31 19:04:10 +00:00
|
|
|
Return(Box<Statement>),
|
2023-10-17 18:06:02 +00:00
|
|
|
For(Box<For>),
|
2023-11-15 01:41:57 +00:00
|
|
|
IndexAssignment(Box<IndexAssignment>),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for Statement {
|
2024-01-10 20:03:52 +00:00
|
|
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
|
2023-11-15 02:03:52 +00:00
|
|
|
Error::expect_syntax_node(source, "statement", node)?;
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
let child = node.child(0).unwrap();
|
|
|
|
|
|
|
|
match child.kind() {
|
|
|
|
"assignment" => Ok(Statement::Assignment(Box::new(
|
2024-01-10 20:03:52 +00:00
|
|
|
Assignment::from_syntax(child, source, context)?,
|
2023-10-06 17:32:58 +00:00
|
|
|
))),
|
2024-01-10 20:03:52 +00:00
|
|
|
"expression" => Ok(Statement::Expression(Expression::from_syntax(
|
|
|
|
child, source, context,
|
2023-11-30 14:30:25 +00:00
|
|
|
)?)),
|
2024-01-10 20:03:52 +00:00
|
|
|
"if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax(
|
|
|
|
child, source, context,
|
2023-10-06 17:32:58 +00:00
|
|
|
)?))),
|
2024-01-10 20:03:52 +00:00
|
|
|
"while" => Ok(Statement::While(Box::new(While::from_syntax(
|
|
|
|
child, source, context,
|
2023-10-07 02:45:36 +00:00
|
|
|
)?))),
|
2024-01-10 20:03:52 +00:00
|
|
|
"block" => Ok(Statement::Block(Box::new(Block::from_syntax(
|
|
|
|
child, source, context,
|
2023-10-17 18:06:02 +00:00
|
|
|
)?))),
|
2024-01-10 20:03:52 +00:00
|
|
|
"for" => Ok(Statement::For(Box::new(For::from_syntax(
|
|
|
|
child, source, context,
|
2023-11-15 01:41:57 +00:00
|
|
|
)?))),
|
2023-12-06 18:48:38 +00:00
|
|
|
"index_assignment" => Ok(Statement::IndexAssignment(Box::new(
|
2024-01-10 20:03:52 +00:00
|
|
|
IndexAssignment::from_syntax(child, source, context)?,
|
2023-12-06 18:48:38 +00:00
|
|
|
))),
|
2024-01-10 20:03:52 +00:00
|
|
|
"match" => Ok(Statement::Match(Match::from_syntax(
|
|
|
|
child, source, context,
|
2023-12-06 18:48:38 +00:00
|
|
|
)?)),
|
2023-12-31 19:04:10 +00:00
|
|
|
"return" => {
|
|
|
|
let statement_node = child.child(1).unwrap();
|
|
|
|
|
2024-01-10 20:03:52 +00:00
|
|
|
Ok(Statement::Return(Box::new(Statement::from_syntax(statement_node, source, context)?)))
|
2023-12-31 19:04:10 +00:00
|
|
|
},
|
2023-11-15 02:03:52 +00:00
|
|
|
_ => Err(Error::UnexpectedSyntaxNode {
|
2023-12-06 18:48:38 +00:00
|
|
|
expected:
|
2023-12-31 19:04:10 +00:00
|
|
|
"assignment, index assignment, expression, block, return, if...else, while, for or match".to_string(),
|
2023-12-30 07:04:39 +00:00
|
|
|
actual: child.kind().to_string(),
|
2023-11-15 02:03:52 +00:00
|
|
|
location: child.start_position(),
|
|
|
|
relevant_source: source[child.byte_range()].to_string(),
|
|
|
|
}),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
fn check_type(&self, _source: &str, _context: &Map) -> Result<()> {
|
2024-01-04 00:57:06 +00:00
|
|
|
match self {
|
2024-01-06 03:26:37 +00:00
|
|
|
Statement::Assignment(assignment) => assignment.check_type(_source, _context),
|
|
|
|
Statement::Expression(expression) => expression.check_type(_source, _context),
|
|
|
|
Statement::IfElse(if_else) => if_else.check_type(_source, _context),
|
|
|
|
Statement::Match(r#match) => r#match.check_type(_source, _context),
|
|
|
|
Statement::While(r#while) => r#while.check_type(_source, _context),
|
|
|
|
Statement::Block(block) => block.check_type(_source, _context),
|
|
|
|
Statement::For(r#for) => r#for.check_type(_source, _context),
|
|
|
|
Statement::IndexAssignment(index_assignment) => {
|
|
|
|
index_assignment.check_type(_source, _context)
|
|
|
|
}
|
|
|
|
Statement::Return(statement) => statement.check_type(_source, _context),
|
2024-01-04 00:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
2023-10-06 17:32:58 +00:00
|
|
|
match self {
|
2023-10-10 17:29:11 +00:00
|
|
|
Statement::Assignment(assignment) => assignment.run(source, context),
|
|
|
|
Statement::Expression(expression) => expression.run(source, context),
|
|
|
|
Statement::IfElse(if_else) => if_else.run(source, context),
|
|
|
|
Statement::Match(r#match) => r#match.run(source, context),
|
|
|
|
Statement::While(r#while) => r#while.run(source, context),
|
2023-11-11 01:44:03 +00:00
|
|
|
Statement::Block(block) => block.run(source, context),
|
2023-10-17 18:06:02 +00:00
|
|
|
Statement::For(r#for) => r#for.run(source, context),
|
2023-11-15 01:41:57 +00:00
|
|
|
Statement::IndexAssignment(index_assignment) => index_assignment.run(source, context),
|
2023-12-31 19:04:10 +00:00
|
|
|
Statement::Return(statement) => statement.run(source, context),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 00:23:42 +00:00
|
|
|
|
2023-12-05 22:08:22 +00:00
|
|
|
fn expected_type(&self, context: &Map) -> Result<Type> {
|
2023-11-30 00:23:42 +00:00
|
|
|
match self {
|
|
|
|
Statement::Assignment(assignment) => assignment.expected_type(context),
|
|
|
|
Statement::Expression(expression) => expression.expected_type(context),
|
|
|
|
Statement::IfElse(if_else) => if_else.expected_type(context),
|
|
|
|
Statement::Match(r#match) => r#match.expected_type(context),
|
|
|
|
Statement::While(r#while) => r#while.expected_type(context),
|
|
|
|
Statement::Block(block) => block.expected_type(context),
|
|
|
|
Statement::For(r#for) => r#for.expected_type(context),
|
|
|
|
Statement::IndexAssignment(index_assignment) => index_assignment.expected_type(context),
|
2023-12-31 19:04:10 +00:00
|
|
|
Statement::Return(statement) => statement.expected_type(context),
|
2023-11-30 00:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
2024-01-06 10:00:36 +00:00
|
|
|
|
2024-01-06 13:11:09 +00:00
|
|
|
impl Format for Statement {
|
|
|
|
fn format(&self, output: &mut String, indent_level: u8) {
|
2024-01-06 13:53:31 +00:00
|
|
|
Statement::indent(output, indent_level);
|
|
|
|
|
2024-01-06 10:00:36 +00:00
|
|
|
match self {
|
2024-01-06 13:11:09 +00:00
|
|
|
Statement::Assignment(assignment) => assignment.format(output, indent_level),
|
|
|
|
Statement::Expression(expression) => expression.format(output, indent_level),
|
|
|
|
Statement::IfElse(if_else) => if_else.format(output, indent_level),
|
|
|
|
Statement::Match(r#match) => r#match.format(output, indent_level),
|
|
|
|
Statement::While(r#while) => r#while.format(output, indent_level),
|
|
|
|
Statement::Block(block) => block.format(output, indent_level),
|
|
|
|
Statement::For(r#for) => r#for.format(output, indent_level),
|
|
|
|
Statement::IndexAssignment(index_assignment) => {
|
|
|
|
index_assignment.format(output, indent_level)
|
|
|
|
}
|
|
|
|
Statement::Return(statement) => statement.format(output, indent_level),
|
2024-01-06 10:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|