2024-08-13 16:23:25 +00:00
|
|
|
//! 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-13 20:21:44 +00:00
|
|
|
abstract_tree::{BinaryOperator, StructInstantiation, UnaryOperator},
|
2024-08-13 18:21:31 +00:00
|
|
|
parse, AbstractSyntaxTree, Context, DustError, Identifier, Node, Span, Statement,
|
|
|
|
StructDefinition, StructType, Type,
|
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-12 12:54:21 +00:00
|
|
|
let context = Context::new();
|
|
|
|
let mut analyzer = Analyzer::new(&abstract_tree, &context);
|
2024-08-11 21:59:52 +00:00
|
|
|
|
|
|
|
analyzer
|
|
|
|
.analyze()
|
|
|
|
.map_err(|analyzer_error| DustError::AnalyzerError {
|
|
|
|
analyzer_error,
|
|
|
|
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-10 08:32:27 +00:00
|
|
|
/// let mut context = Context::new();
|
|
|
|
/// let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
|
2024-08-07 16:13:49 +00:00
|
|
|
/// let result = analyzer.analyze();
|
|
|
|
///
|
|
|
|
/// assert!(result.is_err());
|
2024-08-07 23:03:50 +00:00
|
|
|
pub struct Analyzer<'a> {
|
|
|
|
abstract_tree: &'a AbstractSyntaxTree,
|
2024-08-12 12:54:21 +00:00
|
|
|
context: &'a Context,
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 23:03:50 +00:00
|
|
|
impl<'a> Analyzer<'a> {
|
2024-08-12 12:54:21 +00:00
|
|
|
pub fn new(abstract_tree: &'a AbstractSyntaxTree, context: &'a Context) -> Self {
|
2024-08-07 15:57:15 +00:00
|
|
|
Self {
|
|
|
|
abstract_tree,
|
2024-08-10 01:12:36 +00:00
|
|
|
context,
|
2024-08-07 15:57:15 +00:00
|
|
|
}
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 01:12:36 +00:00
|
|
|
pub fn analyze(&mut self) -> Result<(), AnalyzerError> {
|
2024-08-07 15:38:08 +00:00
|
|
|
for node in &self.abstract_tree.nodes {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(node)?;
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
fn analyze_statement(&mut self, node: &Node<Statement>) -> Result<(), AnalyzerError> {
|
2024-08-09 07:00:48 +00:00
|
|
|
match &node.inner {
|
2024-08-13 21:34:45 +00:00
|
|
|
Statement::Assignment {
|
|
|
|
identifier, value, ..
|
2024-08-09 08:23:02 +00:00
|
|
|
} => {
|
2024-08-13 21:34:45 +00:00
|
|
|
self.analyze_statement(value)?;
|
2024-08-12 08:10:07 +00:00
|
|
|
|
2024-08-13 21:34:45 +00:00
|
|
|
let value_type = value.inner.expected_type(self.context);
|
2024-08-10 01:12:36 +00:00
|
|
|
|
2024-08-13 21:34:45 +00:00
|
|
|
if let Some(r#type) = value_type {
|
|
|
|
self.context
|
|
|
|
.set_type(identifier.inner.clone(), r#type, identifier.position);
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedValue {
|
|
|
|
actual: value.as_ref().clone(),
|
|
|
|
});
|
2024-08-09 22:14:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 21:34:45 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Statement::BinaryOperation {
|
|
|
|
left,
|
|
|
|
operator,
|
|
|
|
right,
|
|
|
|
} => {
|
2024-08-12 19:02:04 +00:00
|
|
|
if let BinaryOperator::FieldAccess = operator.inner {
|
|
|
|
self.analyze_statement(left)?;
|
|
|
|
|
|
|
|
if let Statement::Identifier(_) = right.inner {
|
|
|
|
// Do not expect a value for property accessors
|
|
|
|
} else {
|
|
|
|
self.analyze_statement(right)?;
|
|
|
|
}
|
|
|
|
|
2024-08-13 17:12:13 +00:00
|
|
|
let left_type = left.inner.expected_type(self.context);
|
|
|
|
let right_type = right.inner.expected_type(self.context);
|
|
|
|
|
|
|
|
if let Some(Type::Map { .. }) = left_type {
|
|
|
|
if let Some(Type::String) = right_type {
|
2024-08-12 19:02:04 +00:00
|
|
|
// Allow indexing maps with strings
|
|
|
|
} else if let Statement::Identifier(_) = right.inner {
|
|
|
|
// Allow indexing maps with identifiers
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedIdentifierOrString {
|
|
|
|
actual: right.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-12 20:57:10 +00:00
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedMap {
|
|
|
|
actual: left.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-13 17:12:13 +00:00
|
|
|
// If the accessor is an identifier, check if it is a valid field
|
|
|
|
if let Statement::Identifier(identifier) = &right.inner {
|
|
|
|
if let Some(Type::Map(fields)) = &left_type {
|
|
|
|
if !fields.contains_key(identifier) {
|
|
|
|
return Err(AnalyzerError::UndefinedField {
|
|
|
|
identifier: right.as_ref().clone(),
|
2024-08-13 20:46:54 +00:00
|
|
|
statement: left.as_ref().clone(),
|
2024-08-13 17:12:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the accessor is a constant, check if it is a valid field
|
|
|
|
if let Statement::Constant(value) = &right.inner {
|
|
|
|
if let Some(field_name) = value.as_string() {
|
|
|
|
if let Some(Type::Map(fields)) = left_type {
|
|
|
|
if !fields.contains_key(&Identifier::new(field_name)) {
|
|
|
|
return Err(AnalyzerError::UndefinedField {
|
|
|
|
identifier: right.as_ref().clone(),
|
2024-08-13 20:46:54 +00:00
|
|
|
statement: left.as_ref().clone(),
|
2024-08-13 17:12:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:57:10 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let BinaryOperator::ListIndex = operator.inner {
|
|
|
|
self.analyze_statement(left)?;
|
|
|
|
self.analyze_statement(right)?;
|
|
|
|
|
2024-08-13 17:12:13 +00:00
|
|
|
if let Some(Type::List { length, .. }) = left.inner.expected_type(self.context)
|
|
|
|
{
|
2024-08-12 20:57:10 +00:00
|
|
|
let index_type = right.inner.expected_type(self.context);
|
|
|
|
|
|
|
|
if let Some(Type::Integer | Type::Range) = index_type {
|
|
|
|
// List and index are valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedIntegerOrRange {
|
|
|
|
actual: right.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-13 17:12:13 +00:00
|
|
|
|
|
|
|
// If the index is a constant, check if it is out of bounds
|
|
|
|
if let Statement::Constant(value) = &right.inner {
|
|
|
|
if let Some(index_value) = value.as_integer() {
|
|
|
|
let index_value = index_value as usize;
|
|
|
|
|
|
|
|
if index_value >= length {
|
|
|
|
return Err(AnalyzerError::IndexOutOfBounds {
|
|
|
|
list: left.as_ref().clone(),
|
|
|
|
index: right.as_ref().clone(),
|
|
|
|
index_value,
|
|
|
|
length,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 20:57:10 +00:00
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedList {
|
|
|
|
actual: left.as_ref().clone(),
|
|
|
|
});
|
2024-08-12 19:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(left)?;
|
|
|
|
self.analyze_statement(right)?;
|
2024-08-10 00:24:18 +00:00
|
|
|
|
2024-08-10 01:12:36 +00:00
|
|
|
let left_type = left.inner.expected_type(self.context);
|
|
|
|
let right_type = right.inner.expected_type(self.context);
|
2024-08-09 08:56:24 +00:00
|
|
|
|
|
|
|
if let BinaryOperator::Add
|
|
|
|
| BinaryOperator::Subtract
|
|
|
|
| BinaryOperator::Multiply
|
|
|
|
| BinaryOperator::Divide
|
|
|
|
| BinaryOperator::Greater
|
|
|
|
| BinaryOperator::GreaterOrEqual
|
|
|
|
| BinaryOperator::Less
|
|
|
|
| BinaryOperator::LessOrEqual = operator.inner
|
|
|
|
{
|
2024-08-11 23:00:37 +00:00
|
|
|
if let Some(expected_type) = left_type {
|
|
|
|
if let Some(actual_type) = right_type {
|
|
|
|
expected_type.check(&actual_type).map_err(|conflict| {
|
|
|
|
AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: right.as_ref().clone(),
|
|
|
|
actual_type: conflict.actual,
|
|
|
|
expected: conflict.expected,
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedValue {
|
2024-08-09 08:56:24 +00:00
|
|
|
actual: right.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-12 12:35:08 +00:00
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedValue {
|
|
|
|
actual: left.as_ref().clone(),
|
|
|
|
});
|
2024-08-09 08:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
2024-08-09 15:41:23 +00:00
|
|
|
Statement::Block(statements) => {
|
|
|
|
for statement in statements {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(statement)?;
|
2024-08-09 15:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-11 22:11:59 +00:00
|
|
|
Statement::BuiltInFunctionCall {
|
|
|
|
function,
|
|
|
|
value_arguments,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let value_parameters = function.value_parameters();
|
|
|
|
|
|
|
|
if let Some(arguments) = value_arguments {
|
|
|
|
for argument in arguments {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(argument)?;
|
2024-08-11 22:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if arguments.len() != value_parameters.len() {
|
|
|
|
return Err(AnalyzerError::ExpectedValueArgumentCount {
|
|
|
|
expected: value_parameters.len(),
|
|
|
|
actual: arguments.len(),
|
|
|
|
position: node.position,
|
|
|
|
});
|
|
|
|
}
|
2024-08-11 23:00:37 +00:00
|
|
|
|
|
|
|
for ((_identifier, parameter_type), argument) in
|
|
|
|
value_parameters.iter().zip(arguments)
|
|
|
|
{
|
|
|
|
let argument_type_option = argument.inner.expected_type(self.context);
|
|
|
|
|
|
|
|
if let Some(argument_type) = argument_type_option {
|
|
|
|
parameter_type.check(&argument_type).map_err(|conflict| {
|
|
|
|
AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: argument.clone(),
|
|
|
|
actual_type: conflict.actual,
|
|
|
|
expected: parameter_type.clone(),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedValue {
|
|
|
|
actual: argument.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if arguments.is_empty() && !value_parameters.is_empty() {
|
|
|
|
return Err(AnalyzerError::ExpectedValueArgumentCount {
|
|
|
|
expected: value_parameters.len(),
|
|
|
|
actual: 0,
|
|
|
|
position: node.position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if !value_parameters.is_empty() {
|
|
|
|
return Err(AnalyzerError::ExpectedValueArgumentCount {
|
|
|
|
expected: value_parameters.len(),
|
|
|
|
actual: 0,
|
|
|
|
position: node.position,
|
|
|
|
});
|
2024-08-11 22:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-05 03:11:04 +00:00
|
|
|
Statement::Constant(_) => {}
|
2024-08-13 20:21:44 +00:00
|
|
|
Statement::Invokation {
|
2024-08-13 20:46:54 +00:00
|
|
|
invokee,
|
2024-08-12 01:37:44 +00:00
|
|
|
value_arguments,
|
|
|
|
..
|
|
|
|
} => {
|
2024-08-13 20:46:54 +00:00
|
|
|
self.analyze_statement(invokee)?;
|
|
|
|
|
|
|
|
let invokee_type = invokee.inner.expected_type(self.context);
|
2024-08-12 01:37:44 +00:00
|
|
|
|
|
|
|
if let Some(arguments) = value_arguments {
|
|
|
|
for argument in arguments {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(argument)?;
|
2024-08-12 01:37:44 +00:00
|
|
|
}
|
2024-08-13 20:46:54 +00:00
|
|
|
|
|
|
|
if let Some(Type::Struct(struct_type)) = invokee_type {
|
|
|
|
match struct_type {
|
2024-08-13 22:48:02 +00:00
|
|
|
StructType::Unit { .. } => todo!(),
|
2024-08-13 20:46:54 +00:00
|
|
|
StructType::Tuple { fields, .. } => {
|
|
|
|
for (expected_type, argument) in fields.iter().zip(arguments.iter())
|
|
|
|
{
|
|
|
|
let actual_type = argument.inner.expected_type(self.context);
|
|
|
|
|
|
|
|
if let Some(actual_type) = actual_type {
|
|
|
|
expected_type.check(&actual_type).map_err(|conflict| {
|
|
|
|
AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: argument.clone(),
|
|
|
|
actual_type: conflict.actual,
|
|
|
|
expected: expected_type.clone(),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedValue {
|
|
|
|
actual: argument.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 22:48:02 +00:00
|
|
|
StructType::Fields { .. } => todo!(),
|
2024-08-13 20:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-07 22:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 22:14:46 +00:00
|
|
|
Statement::Identifier(identifier) => {
|
2024-08-12 08:10:07 +00:00
|
|
|
let exists = self.context.update_last_position(identifier, node.position);
|
2024-08-10 08:45:30 +00:00
|
|
|
|
|
|
|
if !exists {
|
2024-08-09 22:14:46 +00:00
|
|
|
return Err(AnalyzerError::UndefinedVariable {
|
|
|
|
identifier: node.clone(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-05 04:40:51 +00:00
|
|
|
}
|
2024-08-11 18:35:33 +00:00
|
|
|
Statement::If { condition, body } => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
Statement::IfElse {
|
|
|
|
condition,
|
|
|
|
if_body,
|
|
|
|
else_body,
|
|
|
|
} => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(if_body)?;
|
|
|
|
self.analyze_statement(else_body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
Statement::IfElseIf {
|
|
|
|
condition,
|
|
|
|
if_body,
|
|
|
|
else_ifs,
|
|
|
|
} => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(if_body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
for (condition, body) in else_ifs {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Statement::IfElseIfElse {
|
|
|
|
condition,
|
|
|
|
if_body,
|
|
|
|
else_ifs,
|
|
|
|
else_body,
|
|
|
|
} => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(if_body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
for (condition, body) in else_ifs {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
// Condition is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(else_body)?;
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
2024-08-05 04:40:51 +00:00
|
|
|
Statement::List(statements) => {
|
|
|
|
for statement in statements {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(statement)?;
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 10:09:59 +00:00
|
|
|
Statement::Map(properties) => {
|
|
|
|
for (_key, value_node) in properties {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(value_node)?;
|
2024-08-09 10:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 09:23:43 +00:00
|
|
|
Statement::Nil(node) => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(node)?;
|
2024-08-10 09:23:43 +00:00
|
|
|
}
|
2024-08-13 18:21:31 +00:00
|
|
|
Statement::StructDefinition(struct_definition) => {
|
|
|
|
let (name, r#type) = match struct_definition {
|
|
|
|
StructDefinition::Unit { name } => (
|
|
|
|
name.inner.clone(),
|
|
|
|
Type::Struct(StructType::Unit {
|
|
|
|
name: name.inner.clone(),
|
|
|
|
}),
|
|
|
|
),
|
2024-08-13 22:48:02 +00:00
|
|
|
StructDefinition::Tuple {
|
|
|
|
name,
|
|
|
|
items: fields,
|
|
|
|
} => (
|
2024-08-13 20:21:44 +00:00
|
|
|
name.inner.clone(),
|
|
|
|
Type::Struct(StructType::Tuple {
|
|
|
|
name: name.inner.clone(),
|
|
|
|
fields: fields
|
|
|
|
.iter()
|
|
|
|
.map(|type_node| type_node.inner.clone())
|
|
|
|
.collect(),
|
|
|
|
}),
|
2024-08-13 22:48:02 +00:00
|
|
|
),
|
|
|
|
StructDefinition::Fields { name, fields } => (
|
|
|
|
name.inner.clone(),
|
|
|
|
Type::Struct(StructType::Fields {
|
|
|
|
name: name.inner.clone(),
|
|
|
|
fields: fields
|
|
|
|
.iter()
|
|
|
|
.map(|(identifier, r#type)| {
|
|
|
|
(identifier.inner.clone(), r#type.inner.clone())
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}),
|
2024-08-13 20:21:44 +00:00
|
|
|
),
|
2024-08-13 18:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.context.set_type(name, r#type, node.position);
|
|
|
|
}
|
2024-08-13 20:21:44 +00:00
|
|
|
Statement::StructInstantiation(struct_instantiation) => {
|
|
|
|
let name = match struct_instantiation {
|
|
|
|
StructInstantiation::Tuple { name, .. } => name,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.context.get_type(&name.inner).is_none() {
|
|
|
|
return Err(AnalyzerError::UndefinedType {
|
|
|
|
identifier: name.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 09:44:05 +00:00
|
|
|
Statement::UnaryOperation { operator, operand } => {
|
|
|
|
self.analyze_statement(operand)?;
|
|
|
|
|
|
|
|
if let UnaryOperator::Negate = operator.inner {
|
|
|
|
if let Some(Type::Integer | Type::Float | Type::Number) =
|
|
|
|
operand.inner.expected_type(self.context)
|
|
|
|
{
|
|
|
|
// Operand is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: operand.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let UnaryOperator::Not = operator.inner {
|
|
|
|
if let Some(Type::Boolean) = operand.inner.expected_type(self.context) {
|
|
|
|
// Operand is valid
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: operand.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 09:23:43 +00:00
|
|
|
Statement::While { condition, body } => {
|
2024-08-12 08:10:07 +00:00
|
|
|
self.analyze_statement(condition)?;
|
|
|
|
self.analyze_statement(body)?;
|
2024-08-10 09:23:43 +00:00
|
|
|
|
|
|
|
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
|
|
|
|
} else {
|
|
|
|
return Err(AnalyzerError::ExpectedBoolean {
|
|
|
|
actual: condition.as_ref().clone(),
|
|
|
|
});
|
|
|
|
}
|
2024-08-09 15:41:23 +00:00
|
|
|
}
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2024-08-07 23:03:50 +00:00
|
|
|
pub enum AnalyzerError {
|
2024-08-12 01:37:44 +00:00
|
|
|
ExpectedBoolean {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-09 07:00:48 +00:00
|
|
|
ExpectedIdentifier {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-12 01:37:44 +00:00
|
|
|
ExpectedIdentifierOrString {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-12 14:43:18 +00:00
|
|
|
ExpectedIntegerOrRange {
|
2024-08-09 07:00:48 +00:00
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-12 20:57:10 +00:00
|
|
|
ExpectedList {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
|
|
|
ExpectedMap {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-09 08:23:02 +00:00
|
|
|
ExpectedValue {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-11 22:11:59 +00:00
|
|
|
ExpectedValueArgumentCount {
|
|
|
|
expected: usize,
|
|
|
|
actual: usize,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-12 23:39:26 +00:00
|
|
|
IndexOutOfBounds {
|
|
|
|
list: Node<Statement>,
|
|
|
|
index: Node<Statement>,
|
|
|
|
index_value: usize,
|
|
|
|
length: usize,
|
|
|
|
},
|
2024-08-11 23:00:37 +00:00
|
|
|
TypeConflict {
|
|
|
|
actual_statement: Node<Statement>,
|
|
|
|
actual_type: Type,
|
|
|
|
expected: Type,
|
|
|
|
},
|
2024-08-13 17:12:13 +00:00
|
|
|
UndefinedField {
|
|
|
|
identifier: Node<Statement>,
|
2024-08-13 20:46:54 +00:00
|
|
|
statement: Node<Statement>,
|
2024-08-13 17:12:13 +00:00
|
|
|
},
|
2024-08-13 20:21:44 +00:00
|
|
|
UndefinedType {
|
|
|
|
identifier: Node<Identifier>,
|
|
|
|
},
|
2024-08-09 07:00:48 +00:00
|
|
|
UnexpectedIdentifier {
|
|
|
|
identifier: Node<Statement>,
|
|
|
|
},
|
2024-08-09 08:23:02 +00:00
|
|
|
UnexectedString {
|
|
|
|
actual: Node<Statement>,
|
|
|
|
},
|
2024-08-13 20:21:44 +00:00
|
|
|
UndefinedVariable {
|
|
|
|
identifier: Node<Statement>,
|
|
|
|
},
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 05:43:58 +00:00
|
|
|
impl AnalyzerError {
|
|
|
|
pub fn position(&self) -> Span {
|
|
|
|
match self {
|
2024-08-12 02:02:17 +00:00
|
|
|
AnalyzerError::ExpectedBoolean { actual, .. } => actual.position,
|
|
|
|
AnalyzerError::ExpectedIdentifier { actual, .. } => actual.position,
|
2024-08-12 01:37:44 +00:00
|
|
|
AnalyzerError::ExpectedIdentifierOrString { actual } => actual.position,
|
2024-08-12 14:43:18 +00:00
|
|
|
AnalyzerError::ExpectedIntegerOrRange { actual, .. } => actual.position,
|
2024-08-12 20:57:10 +00:00
|
|
|
AnalyzerError::ExpectedList { actual } => actual.position,
|
|
|
|
AnalyzerError::ExpectedMap { actual } => actual.position,
|
2024-08-11 23:00:37 +00:00
|
|
|
AnalyzerError::ExpectedValue { actual } => actual.position,
|
2024-08-11 22:11:59 +00:00
|
|
|
AnalyzerError::ExpectedValueArgumentCount { position, .. } => *position,
|
2024-08-12 23:39:26 +00:00
|
|
|
AnalyzerError::IndexOutOfBounds { list, index, .. } => {
|
|
|
|
(list.position.0, index.position.1)
|
|
|
|
}
|
2024-08-11 23:00:37 +00:00
|
|
|
AnalyzerError::TypeConflict {
|
|
|
|
actual_statement, ..
|
|
|
|
} => actual_statement.position,
|
2024-08-13 17:12:13 +00:00
|
|
|
AnalyzerError::UndefinedField { identifier, .. } => identifier.position,
|
2024-08-13 20:21:44 +00:00
|
|
|
AnalyzerError::UndefinedType { identifier } => identifier.position,
|
2024-08-09 22:14:46 +00:00
|
|
|
AnalyzerError::UndefinedVariable { identifier } => identifier.position,
|
2024-08-11 23:00:37 +00:00
|
|
|
AnalyzerError::UnexpectedIdentifier { identifier } => identifier.position,
|
|
|
|
AnalyzerError::UnexectedString { actual } => actual.position,
|
2024-08-09 05:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 00:58:56 +00:00
|
|
|
impl Error for AnalyzerError {}
|
|
|
|
|
|
|
|
impl Display for AnalyzerError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2024-08-09 01:47:49 +00:00
|
|
|
AnalyzerError::ExpectedBoolean { actual, .. } => {
|
2024-08-09 00:58:56 +00:00
|
|
|
write!(f, "Expected boolean, found {}", actual)
|
|
|
|
}
|
2024-08-09 01:47:49 +00:00
|
|
|
AnalyzerError::ExpectedIdentifier { actual, .. } => {
|
2024-08-09 00:58:56 +00:00
|
|
|
write!(f, "Expected identifier, found {}", actual)
|
|
|
|
}
|
2024-08-12 01:37:44 +00:00
|
|
|
AnalyzerError::ExpectedIdentifierOrString { actual } => {
|
|
|
|
write!(f, "Expected identifier or string, found {}", actual)
|
|
|
|
}
|
2024-08-12 14:43:18 +00:00
|
|
|
AnalyzerError::ExpectedIntegerOrRange { actual, .. } => {
|
|
|
|
write!(f, "Expected integer or range, found {}", actual)
|
2024-08-12 01:37:44 +00:00
|
|
|
}
|
2024-08-12 20:57:10 +00:00
|
|
|
AnalyzerError::ExpectedList { actual } => write!(f, "Expected list, found {}", actual),
|
|
|
|
AnalyzerError::ExpectedMap { actual } => write!(f, "Expected map, found {}", actual),
|
2024-08-09 08:23:02 +00:00
|
|
|
AnalyzerError::ExpectedValue { actual, .. } => {
|
|
|
|
write!(f, "Expected value, found {}", actual)
|
|
|
|
}
|
2024-08-11 22:11:59 +00:00
|
|
|
AnalyzerError::ExpectedValueArgumentCount {
|
|
|
|
expected, actual, ..
|
|
|
|
} => write!(f, "Expected {} value arguments, found {}", expected, actual),
|
2024-08-12 23:39:26 +00:00
|
|
|
AnalyzerError::IndexOutOfBounds {
|
|
|
|
list,
|
|
|
|
index_value,
|
|
|
|
length,
|
2024-08-13 17:12:13 +00:00
|
|
|
..
|
2024-08-12 23:39:26 +00:00
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Index {} out of bounds for list {} with length {}",
|
|
|
|
index_value, list, length
|
|
|
|
),
|
2024-08-11 23:00:37 +00:00
|
|
|
AnalyzerError::TypeConflict {
|
|
|
|
actual_statement,
|
|
|
|
actual_type,
|
|
|
|
expected,
|
|
|
|
} => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Expected type {}, found {}, which has type {}",
|
|
|
|
expected, actual_statement, actual_type
|
|
|
|
)
|
|
|
|
}
|
2024-08-13 20:46:54 +00:00
|
|
|
AnalyzerError::UndefinedField {
|
|
|
|
identifier,
|
|
|
|
statement: map,
|
|
|
|
} => {
|
2024-08-13 17:12:13 +00:00
|
|
|
write!(f, "Undefined field {} in map {}", identifier, map)
|
|
|
|
}
|
2024-08-13 20:21:44 +00:00
|
|
|
AnalyzerError::UndefinedType { identifier } => {
|
|
|
|
write!(f, "Undefined type {}", identifier)
|
|
|
|
}
|
2024-08-09 22:14:46 +00:00
|
|
|
AnalyzerError::UndefinedVariable { identifier } => {
|
|
|
|
write!(f, "Undefined variable {}", identifier)
|
|
|
|
}
|
2024-08-09 01:47:49 +00:00
|
|
|
AnalyzerError::UnexpectedIdentifier { identifier, .. } => {
|
2024-08-09 00:58:56 +00:00
|
|
|
write!(f, "Unexpected identifier {}", identifier)
|
|
|
|
}
|
2024-08-09 08:23:02 +00:00
|
|
|
AnalyzerError::UnexectedString { actual, .. } => {
|
|
|
|
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-11 23:18:13 +00:00
|
|
|
use crate::{Identifier, Value};
|
2024-08-05 03:11:04 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2024-08-13 20:46:54 +00:00
|
|
|
#[test]
|
|
|
|
fn tuple_struct_with_wrong_field_types() {
|
|
|
|
let source = "
|
|
|
|
struct Foo(int, float)
|
|
|
|
Foo(1, 2)
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: Node::new(Statement::Constant(Value::integer(2)), (55, 56)),
|
|
|
|
actual_type: Type::Integer,
|
|
|
|
expected: Type::Float
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-12 23:39:26 +00:00
|
|
|
#[test]
|
|
|
|
fn constant_list_index_out_of_bounds() {
|
|
|
|
let source = "[1, 2, 3][3]";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::IndexOutOfBounds {
|
|
|
|
list: Node::new(
|
|
|
|
Statement::List(vec![
|
|
|
|
Node::new(Statement::Constant(Value::integer(1)), (1, 2)),
|
|
|
|
Node::new(Statement::Constant(Value::integer(2)), (4, 5)),
|
|
|
|
Node::new(Statement::Constant(Value::integer(3)), (7, 8)),
|
|
|
|
]),
|
|
|
|
(0, 9)
|
|
|
|
),
|
|
|
|
index: Node::new(Statement::Constant(Value::integer(3)), (10, 11)),
|
|
|
|
index_value: 3,
|
|
|
|
length: 3
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-13 17:12:13 +00:00
|
|
|
fn nonexistant_field_identifier() {
|
2024-08-12 23:39:26 +00:00
|
|
|
let source = "{ x = 1 }.y";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
2024-08-13 17:12:13 +00:00
|
|
|
analyzer_error: AnalyzerError::UndefinedField {
|
2024-08-12 23:39:26 +00:00
|
|
|
identifier: Node::new(Statement::Identifier(Identifier::new("y")), (10, 11)),
|
2024-08-13 20:46:54 +00:00
|
|
|
statement: Node::new(
|
2024-08-13 17:12:13 +00:00
|
|
|
Statement::Map(vec![(
|
2024-08-13 21:34:45 +00:00
|
|
|
Node::new(Identifier::new("x"), (2, 3)),
|
2024-08-13 17:12:13 +00:00
|
|
|
Node::new(Statement::Constant(Value::integer(1)), (6, 7))
|
|
|
|
)]),
|
|
|
|
(0, 9)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonexistant_field_string() {
|
|
|
|
let source = "{ x = 1 }.'y'";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::UndefinedField {
|
|
|
|
identifier: Node::new(Statement::Constant(Value::string("y")), (10, 13)),
|
2024-08-13 20:46:54 +00:00
|
|
|
statement: Node::new(
|
2024-08-13 17:12:13 +00:00
|
|
|
Statement::Map(vec![(
|
2024-08-13 21:34:45 +00:00
|
|
|
Node::new(Identifier::new("x"), (2, 3)),
|
2024-08-13 17:12:13 +00:00
|
|
|
Node::new(Statement::Constant(Value::integer(1)), (6, 7))
|
|
|
|
)]),
|
|
|
|
(0, 9)
|
|
|
|
)
|
2024-08-12 23:39:26 +00:00
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-12 02:02:17 +00:00
|
|
|
#[test]
|
|
|
|
fn malformed_list_index() {
|
2024-08-12 20:57:10 +00:00
|
|
|
let source = "[1, 2, 3]['foo']";
|
2024-08-12 02:02:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
2024-08-12 14:43:18 +00:00
|
|
|
analyzer_error: AnalyzerError::ExpectedIntegerOrRange {
|
2024-08-12 20:57:10 +00:00
|
|
|
actual: Node::new(Statement::Constant(Value::string("foo")), (10, 15)),
|
2024-08-12 02:02:17 +00:00
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-12 23:39:26 +00:00
|
|
|
fn malformed_field_access() {
|
2024-08-12 02:02:17 +00:00
|
|
|
let source = "{ x = 1 }.0";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::ExpectedIdentifierOrString {
|
|
|
|
actual: Node::new(Statement::Constant(Value::integer(0)), (10, 11)),
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:00:37 +00:00
|
|
|
#[test]
|
|
|
|
fn length_no_arguments() {
|
|
|
|
let source = "length()";
|
2024-08-11 21:59:52 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:00:37 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::ExpectedValueArgumentCount {
|
|
|
|
expected: 1,
|
|
|
|
actual: 0,
|
|
|
|
position: (0, 6)
|
|
|
|
},
|
|
|
|
source
|
2024-08-11 21:59:52 +00:00
|
|
|
})
|
2024-08-11 23:00:37 +00:00
|
|
|
);
|
2024-08-11 21:59:52 +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-08 17:21:27 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:00:37 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: Node::new(Statement::Constant(Value::integer(2)), (7, 8)),
|
|
|
|
actual_type: Type::Integer,
|
|
|
|
expected: Type::Float,
|
|
|
|
},
|
|
|
|
source
|
2024-08-08 17:21:27 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:18:13 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: Node::new(Statement::Constant(Value::boolean(true)), (5, 9)),
|
|
|
|
actual_type: Type::Boolean,
|
|
|
|
expected: Type::Integer,
|
|
|
|
},
|
|
|
|
source
|
2024-08-08 17:01:25 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2024-08-08 17:21:27 +00:00
|
|
|
|
2024-08-08 17:01:25 +00:00
|
|
|
#[test]
|
2024-08-08 17:21:27 +00:00
|
|
|
fn is_even_expects_number() {
|
2024-08-11 23:18:13 +00:00
|
|
|
let source = "is_even('hello')";
|
2024-08-08 17:01:25 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:18:13 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: Node::new(
|
|
|
|
Statement::Constant(Value::string("hello")),
|
|
|
|
(8, 15)
|
|
|
|
),
|
|
|
|
actual_type: Type::String,
|
|
|
|
expected: Type::Number,
|
|
|
|
},
|
|
|
|
source
|
2024-08-08 17:01:25 +00:00
|
|
|
})
|
2024-08-11 23:18:13 +00:00
|
|
|
);
|
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-08 17:21:27 +00:00
|
|
|
fn is_odd_expects_number() {
|
2024-08-11 23:18:13 +00:00
|
|
|
let source = "is_odd('hello')";
|
2024-08-05 03:11:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:18:13 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::TypeConflict {
|
|
|
|
actual_statement: Node::new(
|
|
|
|
Statement::Constant(Value::string("hello")),
|
|
|
|
(7, 14)
|
|
|
|
),
|
|
|
|
actual_type: Type::String,
|
|
|
|
expected: Type::Number,
|
|
|
|
},
|
|
|
|
source
|
2024-08-05 03:11:04 +00:00
|
|
|
})
|
2024-08-11 23:18:13 +00:00
|
|
|
);
|
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
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-11 23:18:13 +00:00
|
|
|
analyze(source),
|
|
|
|
Err(DustError::AnalyzerError {
|
|
|
|
analyzer_error: AnalyzerError::UndefinedVariable {
|
|
|
|
identifier: Node::new(Statement::Identifier(Identifier::new("foo")), (0, 3)),
|
|
|
|
},
|
|
|
|
source
|
2024-08-05 04:40:51 +00:00
|
|
|
})
|
2024-08-11 23:18:13 +00:00
|
|
|
);
|
2024-08-05 04:40:51 +00:00
|
|
|
}
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|