1
0
dust/dust-lang/src/analyzer.rs

1387 lines
51 KiB
Rust
Raw Normal View History

//! Tools for analyzing an abstract syntax tree and catching errors before running the virtual
2024-08-09 02:44:34 +00:00
//! machine.
//!
2024-08-12 12:54:21 +00:00
//! This module provides two anlysis options:
//! - `analyze` convenience function, which takes a string input
//! - `Analyzer` struct, which borrows an abstract syntax tree and a context
2024-08-09 00:58:56 +00:00
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
2024-08-07 15:57:15 +00:00
2024-08-09 08:23:02 +00:00
use crate::{
2024-08-17 02:43:29 +00:00
ast::{
2024-08-20 15:07:13 +00:00
AbstractSyntaxTree, AstError, BlockExpression, CallExpression, ElseExpression,
FieldAccessExpression, IfExpression, LetStatement, ListExpression, ListIndexExpression,
2024-08-20 22:48:25 +00:00
LiteralExpression, LoopExpression, MapExpression, Node, OperatorExpression,
PrimitiveValueExpression, RangeExpression, Span, Statement, StructDefinition,
StructExpression, TupleAccessExpression,
2024-08-17 02:43:29 +00:00
},
2024-08-31 11:24:45 +00:00
parse, Context, ContextData, ContextError, DustError, Expression, Identifier, RangeableType,
StructType, Type, TypeConflict, TypeEvaluation,
2024-08-09 08:23:02 +00:00
};
2024-08-07 15:57:15 +00:00
/// Analyzes the abstract syntax tree for errors.
///
/// # Examples
/// ```
/// # use std::collections::HashMap;
/// # use dust_lang::*;
/// let input = "x = 1 + false";
2024-08-11 23:00:37 +00:00
/// let result = analyze(input);
2024-08-07 15:57:15 +00:00
///
/// assert!(result.is_err());
/// ```
2024-08-11 23:00:37 +00:00
pub fn analyze(source: &str) -> Result<(), DustError> {
2024-08-11 21:59:52 +00:00
let abstract_tree = parse(source)?;
2024-08-30 22:06:58 +00:00
let mut analyzer = Analyzer::new(&abstract_tree);
2024-08-23 11:36:10 +00:00
2024-08-31 11:24:45 +00:00
analyzer.analyze()?;
2024-08-23 11:36:10 +00:00
if analyzer.errors.is_empty() {
Ok(())
} else {
Err(DustError::analysis(analyzer.errors, source))
}
2024-08-05 04:40:51 +00:00
}
2024-08-07 16:13:49 +00:00
/// Static analyzer that checks for potential runtime errors.
///
/// # Examples
/// ```
/// # use std::collections::HashMap;
/// # use dust_lang::*;
/// let input = "x = 1 + false";
/// let abstract_tree = parse(input).unwrap();
2024-08-23 21:27:11 +00:00
/// let context = Context::new();
2024-09-05 17:16:26 +00:00
/// let mut analyzer = Analyzer::new(&abstract_tree);
2024-08-07 16:13:49 +00:00
/// let result = analyzer.analyze();
///
2024-08-23 21:27:11 +00:00
/// assert!(!analyzer.errors.is_empty());
pub struct Analyzer<'a> {
abstract_tree: &'a AbstractSyntaxTree,
2024-08-23 11:36:10 +00:00
pub errors: Vec<AnalysisError>,
2024-08-05 03:11:04 +00:00
}
2024-08-23 11:36:10 +00:00
impl<'a> Analyzer<'a> {
2024-08-30 22:06:58 +00:00
pub fn new(abstract_tree: &'a AbstractSyntaxTree) -> Self {
2024-08-07 15:57:15 +00:00
Self {
abstract_tree,
2024-08-23 11:36:10 +00:00
errors: Vec::new(),
2024-08-07 15:57:15 +00:00
}
2024-08-05 03:11:04 +00:00
}
2024-08-31 11:24:45 +00:00
pub fn analyze(&mut self) -> Result<(), ContextError> {
for statement in &self.abstract_tree.statements {
2024-08-31 11:24:45 +00:00
self.analyze_statement(statement, &self.abstract_tree.context)?;
2024-08-05 03:11:04 +00:00
}
2024-08-31 11:24:45 +00:00
Ok(())
2024-08-05 03:11:04 +00:00
}
2024-08-31 11:24:45 +00:00
fn analyze_statement(
&mut self,
statement: &Statement,
context: &Context,
) -> Result<(), ContextError> {
2024-09-05 17:10:38 +00:00
log::trace!(
"Analyzing statement {statement} at {:?} with context {}",
statement.position(),
context.id()
);
match statement {
2024-08-31 11:24:45 +00:00
Statement::Expression(expression) => self.analyze_expression(expression, context)?,
Statement::ExpressionNullified(expression_node) => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(&expression_node.inner, context)?;
}
Statement::Let(let_statement) => match &let_statement.inner {
2024-08-20 04:15:19 +00:00
LetStatement::Let { identifier, value }
| LetStatement::LetMut { identifier, value } => {
2024-08-30 22:06:58 +00:00
let r#type = match value.type_evaluation(&self.abstract_tree.context) {
2024-08-23 11:36:10 +00:00
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
2024-08-23 20:33:38 +00:00
Ok(TypeEvaluation::Constructor(StructType::Unit { name })) => {
2024-08-31 11:24:45 +00:00
self.abstract_tree.context.set_variable_type(
2024-08-23 20:33:38 +00:00
identifier.inner.clone(),
Type::Struct(StructType::Unit { name }),
2024-08-31 11:24:45 +00:00
)?;
2024-08-23 20:33:38 +00:00
2024-08-31 11:24:45 +00:00
self.analyze_expression(value, context)?;
2024-08-23 20:33:38 +00:00
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
}
Ok(evaluation) => evaluation.r#type(),
2024-08-23 11:36:10 +00:00
};
2024-08-20 04:15:19 +00:00
if let Some(r#type) = r#type {
2024-09-05 17:10:38 +00:00
context.set_variable_type(identifier.inner.clone(), r#type.clone())?;
} else {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::LetExpectedValueFromStatement {
actual: value.clone(),
});
}
2024-08-20 06:25:22 +00:00
2024-08-31 11:24:45 +00:00
self.analyze_expression(value, context)?;
}
2024-08-20 04:15:19 +00:00
LetStatement::LetType { .. } => todo!(),
LetStatement::LetMutType { .. } => todo!(),
},
2024-08-23 11:36:10 +00:00
Statement::StructDefinition(struct_definition) => {
2024-08-31 11:24:45 +00:00
match &struct_definition.inner {
2024-08-30 22:06:58 +00:00
StructDefinition::Unit { name } => {
self.abstract_tree.context.set_constructor_type(
name.inner.clone(),
StructType::Unit {
name: name.inner.clone(),
},
2024-08-31 11:24:45 +00:00
)?;
2024-08-30 22:06:58 +00:00
}
2024-08-23 11:36:10 +00:00
StructDefinition::Tuple { name, items } => {
let fields = items.iter().map(|item| item.inner.clone()).collect();
2024-08-30 22:06:58 +00:00
self.abstract_tree.context.set_constructor_type(
2024-08-23 11:36:10 +00:00
name.inner.clone(),
StructType::Tuple {
name: name.inner.clone(),
fields,
},
2024-08-31 11:24:45 +00:00
)?;
2024-08-23 11:36:10 +00:00
}
StructDefinition::Fields { name, fields } => {
let fields = fields
.iter()
.map(|(identifier, r#type)| {
(identifier.inner.clone(), r#type.inner.clone())
})
.collect();
2024-08-30 22:06:58 +00:00
self.abstract_tree.context.set_constructor_type(
2024-08-23 11:36:10 +00:00
name.inner.clone(),
StructType::Fields {
name: name.inner.clone(),
fields,
},
2024-08-31 11:24:45 +00:00
)?;
2024-08-23 11:36:10 +00:00
}
};
2024-08-20 21:01:30 +00:00
}
}
2024-08-31 11:24:45 +00:00
Ok(())
}
2024-08-31 11:24:45 +00:00
fn analyze_expression(
&mut self,
expression: &Expression,
context: &Context,
) -> Result<(), ContextError> {
2024-09-05 15:32:31 +00:00
log::trace!(
2024-09-05 17:10:38 +00:00
"Analyzing expression {expression} at {:?} with context {}",
expression.position(),
context.id()
2024-09-05 15:32:31 +00:00
);
2024-09-03 04:09:32 +00:00
match expression {
2024-08-31 11:24:45 +00:00
Expression::Block(block_expression) => {
self.analyze_block(&block_expression.inner, context)?;
}
2024-08-20 18:45:43 +00:00
Expression::Break(break_node) => {
if let Some(expression) = &break_node.inner {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-20 18:45:43 +00:00
}
}
2024-08-17 03:18:05 +00:00
Expression::Call(call_expression) => {
let CallExpression { invoker, arguments } = call_expression.inner.as_ref();
2024-08-31 11:24:45 +00:00
self.analyze_expression(invoker, context)?;
2024-08-17 03:18:05 +00:00
2024-08-30 22:06:58 +00:00
let invoker_evaluation = match invoker.type_evaluation(&self.abstract_tree.context)
{
2024-08-23 20:33:38 +00:00
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
}
};
if let TypeEvaluation::Constructor(StructType::Tuple { fields, .. }) =
invoker_evaluation
{
for (expected_type, argument) in fields.iter().zip(arguments.iter()) {
2024-08-30 22:06:58 +00:00
let actual_type =
match argument.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation.r#type(),
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-23 20:33:38 +00:00
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
2024-08-23 20:33:38 +00:00
if let Some(r#type) = actual_type {
let check = expected_type.check(&r#type);
if let Err(type_conflict) = check {
self.errors.push(AnalysisError::TypeConflict {
actual_expression: argument.clone(),
type_conflict,
});
}
}
}
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
}
let invoked_type = if let Some(r#type) = invoker_evaluation.r#type() {
r#type
} else {
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: invoker.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
};
let function_type = if let Type::Function(function_type) = invoked_type {
function_type
} else {
self.errors.push(AnalysisError::ExpectedFunction {
actual: invoked_type,
actual_expression: invoker.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
};
let value_parameters =
if let Some(value_parameters) = &function_type.value_parameters {
value_parameters
} else {
if !arguments.is_empty() {
self.errors.push(AnalysisError::ExpectedValueArgumentCount {
expected: 0,
actual: arguments.len(),
position: invoker.position(),
});
}
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 20:33:38 +00:00
};
for ((_, expected_type), argument) in value_parameters.iter().zip(arguments) {
2024-08-31 11:24:45 +00:00
self.analyze_expression(argument, context)?;
2024-08-23 20:33:38 +00:00
2024-08-30 22:06:58 +00:00
let argument_evaluation =
match argument.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(error) => {
self.errors.push(AnalysisError::AstError(error));
2024-08-23 20:33:38 +00:00
2024-08-30 22:06:58 +00:00
continue;
}
};
2024-08-23 20:33:38 +00:00
let actual_type = if let Some(r#type) = argument_evaluation.r#type() {
r#type
} else {
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: argument.clone(),
});
continue;
};
if let Err(type_conflict) = expected_type.check(&actual_type) {
self.errors.push(AnalysisError::TypeConflict {
type_conflict,
actual_expression: argument.clone(),
});
}
}
2024-08-17 03:18:05 +00:00
for argument in arguments {
2024-08-31 11:24:45 +00:00
self.analyze_expression(argument, context)?;
2024-08-17 03:18:05 +00:00
}
}
2024-08-28 15:31:47 +00:00
Expression::Dereference(expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(&expression.inner, context)?;
2024-08-28 15:31:47 +00:00
}
2024-08-17 03:18:05 +00:00
Expression::FieldAccess(field_access_expression) => {
2024-08-23 15:44:47 +00:00
let FieldAccessExpression { container, field } =
2024-08-17 03:18:05 +00:00
field_access_expression.inner.as_ref();
2024-08-30 22:06:58 +00:00
let evaluation = match container.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
}
};
let container_type = match evaluation.r#type() {
Some(r#type) => r#type,
None => {
2024-08-23 15:44:47 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: container.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 15:44:47 +00:00
}
};
if !container_type.has_field(&field.inner) {
self.errors.push(AnalysisError::UndefinedFieldIdentifier {
identifier: field.clone(),
container: container.clone(),
});
}
2024-08-31 11:24:45 +00:00
self.analyze_expression(container, context)?;
2024-08-17 03:18:05 +00:00
}
Expression::Grouped(expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression.inner.as_ref(), context)?;
2024-08-17 03:18:05 +00:00
}
Expression::Identifier(identifier) => {
2024-08-31 11:24:45 +00:00
let context_data = context.get_data(&identifier.inner)?;
2024-08-20 16:24:41 +00:00
2024-08-31 11:24:45 +00:00
if let Some(ContextData::Reserved) | None = context_data {
2024-08-23 11:36:10 +00:00
self.errors.push(AnalysisError::UndefinedVariable {
2024-08-20 16:24:41 +00:00
identifier: identifier.clone(),
});
}
}
2024-08-31 11:24:45 +00:00
Expression::If(if_expression) => self.analyze_if(&if_expression.inner, context)?,
2024-08-17 03:18:05 +00:00
Expression::List(list_expression) => match list_expression.inner.as_ref() {
ListExpression::AutoFill {
repeat_operand,
length_operand,
} => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(repeat_operand, context)?;
self.analyze_expression(length_operand, context)?;
2024-08-17 03:18:05 +00:00
}
ListExpression::Ordered(expressions) => {
for expression in expressions {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-17 03:18:05 +00:00
}
}
},
Expression::ListIndex(list_index_expression) => {
let ListIndexExpression { list, index } = list_index_expression.inner.as_ref();
2024-08-31 11:24:45 +00:00
self.analyze_expression(list, context)?;
self.analyze_expression(index, context)?;
2024-08-20 23:20:38 +00:00
2024-08-30 22:06:58 +00:00
let list_type_evaluation = match list.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
}
};
let list_type = match list_type_evaluation.r#type() {
Some(r#type) => r#type,
None => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: list.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
};
2024-08-30 22:06:58 +00:00
let index_type_evaluation = match index.type_evaluation(&self.abstract_tree.context)
{
Ok(evaluation) => evaluation,
2024-08-23 11:36:10 +00:00
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
2024-08-20 23:20:38 +00:00
};
let index_type = match index_type_evaluation.r#type() {
Some(r#type) => r#type,
None => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: list.clone(),
});
2024-08-20 22:48:25 +00:00
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
};
2024-08-20 22:48:25 +00:00
let literal_type = if let Expression::Literal(Node { inner, .. }) = index {
Some(inner.as_ref().clone())
} else {
None
};
2024-08-20 23:20:38 +00:00
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
if integer < 0 {
2024-08-23 11:36:10 +00:00
self.errors.push(AnalysisError::NegativeIndex {
2024-08-20 23:20:38 +00:00
index: index.clone(),
index_value: integer,
list: list.clone(),
});
}
}
2024-08-23 11:36:10 +00:00
if let Type::List { length, .. } = list_type {
2024-08-20 22:48:25 +00:00
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
2024-08-20 23:20:38 +00:00
if integer >= length as i64 {
2024-08-23 16:16:56 +00:00
self.errors.push(AnalysisError::ListIndexOutOfBounds {
2024-08-20 22:48:25 +00:00
index: index.clone(),
length,
list: list.clone(),
index_value: integer,
});
}
2024-08-23 15:44:47 +00:00
} else if let Type::Integer
| Type::Range {
r#type: RangeableType::Integer,
} = index_type
{
} else {
self.errors.push(AnalysisError::ExpectedTypeMultiple {
expected: vec![
Type::Integer,
Type::Range {
r#type: RangeableType::Integer,
},
],
actual: index_type.clone(),
actual_expression: index.clone(),
});
2024-08-20 22:48:25 +00:00
}
}
2024-08-23 11:36:10 +00:00
if let Type::String {
2024-08-20 22:48:25 +00:00
length: Some(length),
2024-08-23 11:36:10 +00:00
} = list_type
2024-08-20 22:48:25 +00:00
{
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
2024-08-20 23:20:38 +00:00
if integer >= length as i64 {
2024-08-23 16:16:56 +00:00
self.errors.push(AnalysisError::ListIndexOutOfBounds {
2024-08-20 22:48:25 +00:00
index: index.clone(),
length,
list: list.clone(),
index_value: integer,
});
}
}
}
2024-08-17 03:18:05 +00:00
}
Expression::Literal(_) => {
// Literals don't need to be analyzed
}
Expression::Loop(loop_expression) => match loop_expression.inner.as_ref() {
2024-08-31 11:24:45 +00:00
LoopExpression::Infinite { block } => self.analyze_block(&block.inner, context)?,
2024-08-17 03:18:05 +00:00
LoopExpression::While { condition, block } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(condition, context)?;
self.analyze_block(&block.inner, context)?;
2024-08-17 03:18:05 +00:00
}
LoopExpression::For {
iterator, block, ..
} => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(iterator, context)?;
self.analyze_block(&block.inner, context)?;
2024-08-17 03:18:05 +00:00
}
},
2024-08-17 14:07:38 +00:00
Expression::Map(map_expression) => {
let MapExpression { pairs } = map_expression.inner.as_ref();
for (_, expression) in pairs {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-17 14:07:38 +00:00
}
}
Expression::Operator(operator_expression) => match operator_expression.inner.as_ref() {
OperatorExpression::Assignment { assignee, value } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(assignee, context)?;
self.analyze_expression(value, context)?;
}
OperatorExpression::Comparison { left, right, .. } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(left, context)?;
self.analyze_expression(right, context)?;
}
OperatorExpression::CompoundAssignment {
assignee, modifier, ..
} => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(assignee, context)?;
self.analyze_expression(modifier, context)?;
2024-08-23 11:36:10 +00:00
2024-08-30 22:06:58 +00:00
let assignee_type_evaluation =
match assignee.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
let modifier_type_evaluation =
match modifier.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
2024-08-23 11:36:10 +00:00
let (expected_type, actual_type) = match (
assignee_type_evaluation.r#type(),
modifier_type_evaluation.r#type(),
2024-08-23 11:36:10 +00:00
) {
(Some(expected_type), Some(actual_type)) => (expected_type, actual_type),
(None, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: assignee.clone(),
});
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: modifier.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(None, _) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: assignee.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(_, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: modifier.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
};
2024-08-20 04:15:19 +00:00
2024-08-23 11:36:10 +00:00
if actual_type != expected_type {
self.errors.push(AnalysisError::ExpectedType {
expected: expected_type,
actual: actual_type,
actual_expression: modifier.clone(),
2024-08-20 04:15:19 +00:00
});
}
}
OperatorExpression::ErrorPropagation(_) => todo!(),
2024-08-16 15:21:20 +00:00
OperatorExpression::Negation(expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-16 15:21:20 +00:00
}
OperatorExpression::Not(expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-16 15:21:20 +00:00
}
OperatorExpression::Math { left, right, .. } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(left, context)?;
self.analyze_expression(right, context)?;
2024-08-23 11:36:10 +00:00
2024-08-30 22:06:58 +00:00
let left_type_evaluation =
match left.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
let right_type_evaluation =
match right.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
2024-08-23 11:36:10 +00:00
let (left_type, right_type) = match (
left_type_evaluation.r#type(),
right_type_evaluation.r#type(),
2024-08-23 11:36:10 +00:00
) {
(Some(left_type), Some(right_type)) => (left_type, right_type),
(None, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
});
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(None, _) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(_, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
};
match left_type {
Type::Integer => {
if right_type != Type::Integer {
self.errors.push(AnalysisError::ExpectedType {
expected: Type::Integer,
actual: right_type,
actual_expression: right.clone(),
});
}
}
Type::Float => {
if right_type != Type::Float {
self.errors.push(AnalysisError::ExpectedType {
expected: Type::Float,
actual: right_type,
actual_expression: right.clone(),
});
}
}
2024-08-20 22:48:25 +00:00
2024-08-23 11:36:10 +00:00
Type::String { .. } => {
if let Type::String { .. } = right_type {
} else {
self.errors.push(AnalysisError::ExpectedType {
expected: Type::String { length: None },
actual: right_type,
actual_expression: right.clone(),
});
}
}
_ => {
self.errors.push(AnalysisError::ExpectedTypeMultiple {
expected: vec![
Type::Float,
Type::Integer,
Type::String { length: None },
],
actual: left_type,
actual_expression: left.clone(),
});
}
2024-08-20 22:48:25 +00:00
}
}
OperatorExpression::Logic { left, right, .. } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(left, context)?;
self.analyze_expression(right, context)?;
2024-08-23 11:36:10 +00:00
2024-08-30 22:06:58 +00:00
let left_type_evaluation =
match left.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
let right_type_evaluation =
match right.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-30 22:06:58 +00:00
}
};
2024-08-23 11:36:10 +00:00
let (left_type, right_type) = match (
left_type_evaluation.r#type(),
right_type_evaluation.r#type(),
2024-08-23 11:36:10 +00:00
) {
(Some(left_type), Some(right_type)) => (left_type, right_type),
(None, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
});
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(None, _) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
(_, None) => {
2024-08-23 11:36:10 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 11:36:10 +00:00
}
};
2024-08-20 22:48:25 +00:00
if left_type != right_type {
2024-08-23 11:36:10 +00:00
self.errors.push(AnalysisError::ExpectedType {
expected: left_type,
actual: right_type,
2024-08-20 22:48:25 +00:00
actual_expression: right.clone(),
});
}
}
},
2024-08-17 03:18:05 +00:00
Expression::Range(range_expression) => match range_expression.inner.as_ref() {
RangeExpression::Exclusive { start, end } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(start, context)?;
self.analyze_expression(end, context)?;
2024-08-17 03:18:05 +00:00
}
RangeExpression::Inclusive { start, end } => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(start, context)?;
self.analyze_expression(end, context)?;
2024-08-17 03:18:05 +00:00
}
},
Expression::Struct(struct_expression) => match struct_expression.inner.as_ref() {
2024-08-31 11:24:45 +00:00
StructExpression::Fields { fields, .. } => {
2024-08-20 06:25:22 +00:00
for (_, expression) in fields {
2024-08-31 11:24:45 +00:00
self.analyze_expression(expression, context)?;
2024-08-20 06:25:22 +00:00
}
2024-08-17 03:18:05 +00:00
}
},
Expression::TupleAccess(tuple_access) => {
2024-08-23 16:16:56 +00:00
let TupleAccessExpression { tuple, index } = tuple_access.inner.as_ref();
2024-08-30 22:06:58 +00:00
let type_evaluation = match tuple.type_evaluation(&self.abstract_tree.context) {
Ok(evaluation) => evaluation,
Err(ast_error) => {
self.errors.push(AnalysisError::AstError(ast_error));
2024-08-31 11:24:45 +00:00
return Ok(());
}
};
let tuple_type = match type_evaluation.r#type() {
Some(tuple_type) => tuple_type,
None => {
2024-08-23 16:16:56 +00:00
self.errors
.push(AnalysisError::ExpectedValueFromExpression {
expression: tuple.clone(),
});
2024-08-31 11:24:45 +00:00
return Ok(());
2024-08-23 16:16:56 +00:00
}
};
if let Type::Tuple {
fields: Some(fields),
} = tuple_type
{
if index.inner >= fields.len() {
self.errors.push(AnalysisError::TupleIndexOutOfBounds {
index: expression.clone(),
tuple: tuple.clone(),
index_value: index.inner as i64,
length: fields.len(),
});
}
} else {
self.errors.push(AnalysisError::ExpectedType {
expected: Type::Tuple { fields: None },
actual: tuple_type,
actual_expression: tuple.clone(),
});
}
2024-08-17 03:18:05 +00:00
2024-08-31 11:24:45 +00:00
self.analyze_expression(tuple, context)?;
2024-08-17 03:18:05 +00:00
}
}
2024-08-31 11:24:45 +00:00
Ok(())
2024-08-17 03:18:05 +00:00
}
2024-08-31 11:24:45 +00:00
fn analyze_block(
&mut self,
block_expression: &BlockExpression,
_context: &Context,
) -> Result<(), ContextError> {
2024-08-30 22:06:58 +00:00
let ast = match block_expression {
BlockExpression::Async(ast) => ast,
BlockExpression::Sync(ast) => ast,
};
2024-09-05 17:10:38 +00:00
log::trace!("Analyzing block with context {}", ast.context.id());
2024-08-31 11:24:45 +00:00
for statement in &ast.statements {
self.analyze_statement(statement, &ast.context)?;
}
2024-08-30 22:06:58 +00:00
2024-08-31 11:24:45 +00:00
Ok(())
2024-08-17 03:18:05 +00:00
}
2024-08-31 11:24:45 +00:00
fn analyze_if(
&mut self,
if_expression: &IfExpression,
context: &Context,
) -> Result<(), ContextError> {
2024-08-17 03:18:05 +00:00
match if_expression {
IfExpression::If {
condition,
if_block,
} => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(condition, context)?;
self.analyze_block(&if_block.inner, context)?;
2024-08-17 03:18:05 +00:00
}
IfExpression::IfElse {
condition,
if_block,
r#else,
} => {
2024-08-31 11:24:45 +00:00
self.analyze_expression(condition, context)?;
self.analyze_block(&if_block.inner, context)?;
2024-08-17 03:18:05 +00:00
match r#else {
ElseExpression::Block(block_expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_block(&block_expression.inner, context)?;
2024-08-17 03:18:05 +00:00
}
ElseExpression::If(if_expression) => {
2024-08-31 11:24:45 +00:00
self.analyze_if(&if_expression.inner, context)?;
2024-08-17 03:18:05 +00:00
}
}
}
}
2024-08-31 11:24:45 +00:00
Ok(())
2024-08-05 03:11:04 +00:00
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnalysisError {
2024-08-20 15:07:13 +00:00
AstError(AstError),
2024-08-23 20:33:38 +00:00
ExpectedFunction {
actual: Type,
actual_expression: Expression,
},
2024-08-20 22:48:25 +00:00
ExpectedType {
expected: Type,
actual: Type,
actual_expression: Expression,
},
ExpectedTypeMultiple {
expected: Vec<Type>,
actual: Type,
actual_expression: Expression,
},
ExpectedIdentifier {
2024-08-20 22:48:25 +00:00
actual: Expression,
},
ExpectedIdentifierOrString {
2024-08-20 22:48:25 +00:00
actual: Expression,
2024-08-12 20:57:10 +00:00
},
2024-08-23 09:54:58 +00:00
LetExpectedValueFromStatement {
actual: Expression,
2024-08-09 08:23:02 +00:00
},
2024-08-20 04:15:19 +00:00
ExpectedValueFromExpression {
2024-08-20 06:25:22 +00:00
expression: Expression,
2024-08-20 04:15:19 +00:00
},
2024-08-11 22:11:59 +00:00
ExpectedValueArgumentCount {
expected: usize,
actual: usize,
position: Span,
},
2024-08-23 16:16:56 +00:00
ListIndexOutOfBounds {
2024-08-20 22:48:25 +00:00
list: Expression,
index: Expression,
index_value: i64,
2024-08-12 23:39:26 +00:00
length: usize,
},
2024-08-23 16:16:56 +00:00
TupleIndexOutOfBounds {
tuple: Expression,
index: Expression,
index_value: i64,
length: usize,
},
2024-08-20 23:20:38 +00:00
NegativeIndex {
list: Expression,
index: Expression,
index_value: i64,
},
2024-08-11 23:00:37 +00:00
TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression: Expression,
2024-08-23 20:33:38 +00:00
type_conflict: TypeConflict,
},
UnexpectedArguments {
expected: Option<Vec<Type>>,
actual: Vec<Expression>,
2024-08-11 23:00:37 +00:00
},
2024-08-23 15:44:47 +00:00
UndefinedFieldIdentifier {
identifier: Node<Identifier>,
container: Expression,
},
2024-08-13 20:21:44 +00:00
UndefinedType {
identifier: Node<Identifier>,
},
UnexpectedIdentifier {
identifier: Node<Identifier>,
},
2024-08-09 08:23:02 +00:00
UnexectedString {
2024-08-20 22:48:25 +00:00
actual: Expression,
2024-08-09 08:23:02 +00:00
},
2024-08-13 20:21:44 +00:00
UndefinedVariable {
identifier: Node<Identifier>,
2024-08-13 20:21:44 +00:00
},
2024-08-05 03:11:04 +00:00
}
2024-08-20 15:07:13 +00:00
impl From<AstError> for AnalysisError {
2024-08-31 11:24:45 +00:00
fn from(error: AstError) -> Self {
Self::AstError(error)
2024-08-20 15:07:13 +00:00
}
}
impl AnalysisError {
2024-08-20 21:01:30 +00:00
pub fn position(&self) -> Span {
match self {
AnalysisError::AstError(ast_error) => ast_error.position(),
2024-08-23 20:33:38 +00:00
AnalysisError::ExpectedFunction {
actual_expression, ..
} => actual_expression.position(),
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedType {
actual_expression, ..
} => actual_expression.position(),
AnalysisError::ExpectedTypeMultiple {
actual_expression, ..
} => actual_expression.position(),
AnalysisError::ExpectedIdentifier { actual } => actual.position(),
AnalysisError::ExpectedIdentifierOrString { actual } => actual.position(),
2024-08-20 06:25:22 +00:00
AnalysisError::ExpectedValueFromExpression { expression, .. } => expression.position(),
AnalysisError::ExpectedValueArgumentCount { position, .. } => *position,
2024-08-23 16:16:56 +00:00
AnalysisError::ListIndexOutOfBounds { index, .. } => index.position(),
AnalysisError::TupleIndexOutOfBounds { index, .. } => index.position(),
2024-08-23 09:54:58 +00:00
AnalysisError::LetExpectedValueFromStatement { actual } => actual.position(),
2024-08-20 23:20:38 +00:00
AnalysisError::NegativeIndex { index, .. } => index.position(),
AnalysisError::TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression, ..
} => actual_expression.position(),
2024-08-23 15:44:47 +00:00
AnalysisError::UndefinedFieldIdentifier { identifier, .. } => identifier.position,
AnalysisError::UndefinedType { identifier } => identifier.position,
AnalysisError::UndefinedVariable { identifier } => identifier.position,
2024-08-23 20:33:38 +00:00
AnalysisError::UnexpectedArguments { actual, .. } => actual[0].position(),
AnalysisError::UnexpectedIdentifier { identifier } => identifier.position,
AnalysisError::UnexectedString { actual } => actual.position(),
2024-08-20 21:01:30 +00:00
}
2024-08-09 05:43:58 +00:00
}
}
impl Error for AnalysisError {}
2024-08-09 00:58:56 +00:00
impl Display for AnalysisError {
2024-08-09 00:58:56 +00:00
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
2024-08-20 15:07:13 +00:00
AnalysisError::AstError(ast_error) => write!(f, "{}", ast_error),
2024-08-23 20:33:38 +00:00
AnalysisError::ExpectedFunction {
actual,
actual_expression,
} => {
write!(
f,
"Expected function, found {} in {}",
actual, actual_expression
)
}
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedType {
expected,
actual,
actual_expression,
} => {
write!(
f,
2024-08-23 09:54:58 +00:00
"Expected type {}, found {} in {}",
2024-08-20 22:48:25 +00:00
expected, actual, actual_expression
)
2024-08-20 21:01:30 +00:00
}
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedTypeMultiple {
expected,
actual,
actual_expression,
} => {
2024-08-23 09:54:58 +00:00
write!(f, "Expected ")?;
for (i, expected_type) in expected.iter().enumerate() {
if i == expected.len() - 1 {
write!(f, "or ")?;
} else if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", expected_type)?;
}
write!(f, ", found {} in {}", actual, actual_expression)
2024-08-09 00:58:56 +00:00
}
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedIdentifier { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected identifier, found {}", actual)
}
AnalysisError::ExpectedIdentifierOrString { actual } => {
write!(f, "Expected identifier or string, found {}", actual)
}
2024-08-23 09:54:58 +00:00
AnalysisError::ExpectedValueFromExpression { expression } => {
write!(f, "Expected {} to produce a value", expression)
2024-08-09 08:23:02 +00:00
}
AnalysisError::ExpectedValueArgumentCount {
2024-08-11 22:11:59 +00:00
expected, actual, ..
} => write!(f, "Expected {} value arguments, found {}", expected, actual),
2024-08-23 16:16:56 +00:00
AnalysisError::ListIndexOutOfBounds {
2024-08-12 23:39:26 +00:00
list,
index_value,
length,
..
2024-08-12 23:39:26 +00:00
} => write!(
f,
"Index {} out of bounds for list {} with length {}",
index_value, list, length
),
2024-08-23 09:54:58 +00:00
AnalysisError::LetExpectedValueFromStatement { actual, .. } => {
write!(
f,
"Cannot assign to nothing. This expression should produce a value, but {} does not",
actual
)
}
2024-08-20 23:20:38 +00:00
AnalysisError::NegativeIndex {
list, index_value, ..
} => write!(f, "Negative index {} for list {}", index_value, list),
2024-08-23 16:16:56 +00:00
AnalysisError::TupleIndexOutOfBounds {
tuple,
index_value,
length,
..
} => write!(
f,
"Index {} out of bounds for tuple {} with length {}",
index_value, tuple, length
),
AnalysisError::TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression: actual_statement,
2024-08-23 20:33:38 +00:00
type_conflict: TypeConflict { expected, actual },
2024-08-11 23:00:37 +00:00
} => {
write!(
f,
"Expected type {}, found {}, which has type {}",
2024-08-23 20:33:38 +00:00
expected, actual_statement, actual
)
}
AnalysisError::UnexpectedArguments {
actual, expected, ..
} => {
write!(
f,
"Unexpected arguments {:?}, expected {:?}",
actual, expected
2024-08-11 23:00:37 +00:00
)
}
2024-08-23 15:44:47 +00:00
AnalysisError::UndefinedFieldIdentifier {
identifier,
2024-08-23 15:44:47 +00:00
container,
} => {
2024-08-23 15:44:47 +00:00
write!(
f,
"Undefined field {} in container {}",
identifier, container
)
}
AnalysisError::UndefinedType { identifier } => {
2024-08-13 20:21:44 +00:00
write!(f, "Undefined type {}", identifier)
}
AnalysisError::UndefinedVariable { identifier } => {
2024-08-09 22:14:46 +00:00
write!(f, "Undefined variable {}", identifier)
}
AnalysisError::UnexpectedIdentifier { identifier, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Unexpected identifier {}", identifier)
}
AnalysisError::UnexectedString { actual, .. } => {
2024-08-09 08:23:02 +00:00
write!(f, "Unexpected string {}", actual)
}
2024-08-09 00:58:56 +00:00
}
}
}
2024-08-05 03:11:04 +00:00
#[cfg(test)]
mod tests {
2024-08-23 16:16:56 +00:00
use std::collections::HashMap;
2024-08-23 15:44:47 +00:00
use crate::RangeableType;
2024-08-05 03:11:04 +00:00
use super::*;
2024-08-23 11:36:10 +00:00
#[test]
fn multiple_errors() {
let source = "1 + 1.0; 'a' + 1";
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_errors: vec![
AnalysisError::ExpectedType {
expected: Type::Integer,
actual: Type::Float,
actual_expression: Expression::literal(1.0, (4, 7)),
},
AnalysisError::ExpectedTypeMultiple {
expected: vec![Type::Float, Type::Integer, Type::String { length: None }],
actual: Type::Character,
actual_expression: Expression::literal('a', (9, 12)),
}
],
source,
})
);
}
2024-08-14 18:28:39 +00:00
#[test]
fn add_assign_wrong_type() {
let source = "
2024-08-20 04:15:19 +00:00
let mut a = 1;
2024-08-14 18:28:39 +00:00
a += 1.0
";
2024-08-20 04:15:19 +00:00
assert_eq!(
analyze(source),
2024-08-20 08:38:15 +00:00
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::ExpectedType {
2024-08-20 04:15:19 +00:00
expected: Type::Integer,
2024-08-23 11:36:10 +00:00
actual: Type::Float,
actual_expression: Expression::literal(1.0, (45, 48)),
}],
2024-08-20 04:15:19 +00:00
source,
})
);
2024-08-14 18:28:39 +00:00
}
#[test]
fn subtract_assign_wrong_type() {
let source = "
2024-08-20 16:24:41 +00:00
let mut a = 1;
2024-08-14 18:28:39 +00:00
a -= 1.0
";
2024-08-20 04:15:19 +00:00
assert_eq!(
analyze(source),
2024-08-20 08:38:15 +00:00
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::ExpectedType {
2024-08-20 04:15:19 +00:00
expected: Type::Integer,
2024-08-23 11:36:10 +00:00
actual: Type::Float,
actual_expression: Expression::literal(1.0, (45, 48)),
}],
2024-08-20 04:15:19 +00:00
source,
})
);
2024-08-14 18:28:39 +00:00
}
#[test]
fn tuple_struct_with_wrong_field_types() {
let source = "
2024-08-20 22:48:25 +00:00
struct Foo(int, float);
Foo(1, 2)
";
2024-08-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 20:33:38 +00:00
analysis_errors: vec![AnalysisError::TypeConflict {
actual_expression: Expression::literal(2, (56, 57)),
type_conflict: TypeConflict {
expected: Type::Float,
actual: Type::Integer,
},
2024-08-23 11:36:10 +00:00
}],
2024-08-20 22:48:25 +00:00
source,
})
);
}
2024-08-12 23:39:26 +00:00
#[test]
fn constant_list_index_out_of_bounds() {
let source = "[1, 2, 3][3]";
2024-08-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 16:16:56 +00:00
analysis_errors: vec![AnalysisError::ListIndexOutOfBounds {
2024-08-20 22:48:25 +00:00
list: Expression::list(
vec![
Expression::literal(1, (1, 2)),
Expression::literal(2, (4, 5)),
Expression::literal(3, (7, 8)),
],
(0, 9)
),
index: Expression::literal(3, (10, 11)),
index_value: 3,
length: 3,
2024-08-23 11:36:10 +00:00
}],
2024-08-20 22:48:25 +00:00
source,
})
);
2024-08-12 23:39:26 +00:00
}
#[test]
2024-08-23 15:44:47 +00:00
fn nonexistant_map_field_identifier() {
let source = "map { x = 1 }.y";
2024-08-23 11:36:10 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 15:44:47 +00:00
analysis_errors: vec![AnalysisError::UndefinedFieldIdentifier {
container: Expression::map(
2024-08-23 11:36:10 +00:00
[(
2024-08-23 15:44:47 +00:00
Node::new(Identifier::new("x"), (6, 7)),
Expression::literal(1, (10, 11))
2024-08-23 11:36:10 +00:00
)],
2024-08-23 15:44:47 +00:00
(0, 13)
2024-08-23 11:36:10 +00:00
),
2024-08-23 15:44:47 +00:00
identifier: Node::new(Identifier::new("y"), (14, 15)),
2024-08-23 11:36:10 +00:00
}],
source,
})
);
2024-08-12 23:39:26 +00:00
}
2024-08-12 02:02:17 +00:00
#[test]
fn malformed_list_index() {
2024-08-23 11:36:10 +00:00
let source = "[1, 2, 3][\"foo\"]";
2024-08-12 02:02:17 +00:00
2024-08-20 23:20:38 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::ExpectedTypeMultiple {
expected: vec![
Type::Integer,
Type::Range {
r#type: RangeableType::Integer
}
],
2024-08-20 23:20:38 +00:00
actual: Type::String { length: Some(3) },
actual_expression: Expression::literal("foo", (10, 15)),
2024-08-23 11:36:10 +00:00
}],
2024-08-20 23:20:38 +00:00
source,
})
);
2024-08-12 02:02:17 +00:00
}
#[test]
2024-08-12 23:39:26 +00:00
fn malformed_field_access() {
2024-08-23 15:44:47 +00:00
let source = "struct Foo { x: int } Foo { x: 1 }.0";
2024-08-12 02:02:17 +00:00
2024-08-23 11:36:10 +00:00
assert_eq!(
analyze(source),
2024-08-23 16:16:56 +00:00
Err(DustError::analysis(
[AnalysisError::ExpectedType {
expected: Type::Tuple { fields: None },
actual: Type::Struct(StructType::Fields {
name: Identifier::new("Foo"),
fields: HashMap::from([(Identifier::new("x"), Type::Integer)])
}),
actual_expression: Expression::r#struct(
StructExpression::Fields {
name: Node::new(Identifier::new("Foo"), (22, 25)),
fields: vec![(
Node::new(Identifier::new("x"), (28, 29)),
Expression::literal(1, (31, 32))
)],
},
(22, 35)
),
2024-08-23 11:36:10 +00:00
}],
2024-08-23 16:16:56 +00:00
source
))
2024-08-23 11:36:10 +00:00
);
2024-08-12 02:02:17 +00:00
}
2024-08-08 17:01:25 +00:00
#[test]
2024-08-09 08:56:24 +00:00
fn float_plus_integer() {
2024-08-11 23:00:37 +00:00
let source = "42.0 + 2";
2024-08-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::ExpectedType {
2024-08-20 22:48:25 +00:00
expected: Type::Float,
actual: Type::Integer,
actual_expression: Expression::literal(2, (7, 8)),
2024-08-23 11:36:10 +00:00
}],
2024-08-20 22:48:25 +00:00
source,
})
);
}
#[test]
2024-08-09 08:56:24 +00:00
fn integer_plus_boolean() {
2024-08-11 23:18:13 +00:00
let source = "42 + true";
2024-08-08 17:01:25 +00:00
2024-08-20 23:20:38 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::ExpectedType {
2024-08-20 23:20:38 +00:00
expected: Type::Integer,
actual: Type::Boolean,
actual_expression: Expression::literal(true, (5, 9)),
2024-08-23 11:36:10 +00:00
}],
2024-08-20 23:20:38 +00:00
source,
})
);
2024-08-08 17:01:25 +00:00
}
2024-08-11 23:18:13 +00:00
2024-08-05 03:11:04 +00:00
#[test]
2024-08-20 23:20:38 +00:00
fn nonexistant_field() {
2024-08-23 15:44:47 +00:00
let source = "\"hello\".foo";
2024-08-05 03:11:04 +00:00
2024-08-23 11:36:10 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 15:44:47 +00:00
analysis_errors: vec![AnalysisError::UndefinedFieldIdentifier {
container: Expression::literal("hello", (0, 7)),
identifier: Node::new(Identifier::new("foo"), (8, 11)),
2024-08-23 11:36:10 +00:00
}],
source,
})
);
2024-08-05 03:11:04 +00:00
}
2024-08-05 04:40:51 +00:00
2024-08-07 15:57:15 +00:00
#[test]
2024-08-09 22:14:46 +00:00
fn undefined_variable() {
2024-08-11 23:18:13 +00:00
let source = "foo";
2024-08-05 04:40:51 +00:00
2024-08-20 16:24:41 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
2024-08-23 11:36:10 +00:00
analysis_errors: vec![AnalysisError::UndefinedVariable {
2024-08-20 16:24:41 +00:00
identifier: Node::new(Identifier::new("foo"), (0, 3))
2024-08-23 11:36:10 +00:00
}],
2024-08-20 16:24:41 +00:00
source,
})
);
2024-08-05 04:40:51 +00:00
}
2024-08-05 03:11:04 +00:00
}