Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/evaluator/expression/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ impl Evaluator {
self.environment.pop();
Ok(block_call_evaluation)
}
_ => anyhow::bail!("UncallableObject"),
object => anyhow::bail!(
"Cannot call an object of type {}, only functions are callable.",
object.type_name()
),
}
}
}
45 changes: 37 additions & 8 deletions src/evaluator/expression/infix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ impl Evaluator {
match operator {
InfixOperator::Equals => Ok(Object::True),
InfixOperator::NotEquals => Ok(Object::False),
_ => anyhow::bail!("UnknownOperator"),
_ => anyhow::bail!(
"Unknown operator, the {} infix operator is not supported for operands of type BOOLEAN.",
operator
),
}
}

fn evaluate_opposite_boolean(operator: InfixOperator) -> anyhow::Result<Object> {
match operator {
InfixOperator::Equals => Ok(Object::False),
InfixOperator::NotEquals => Ok(Object::True),
_ => anyhow::bail!("UnknownOperator"),
_ => anyhow::bail!(
"Unknown operator, the {} infix operator is not supported for operands of type BOOLEAN.",
operator
),
}
}

Expand Down Expand Up @@ -57,17 +63,29 @@ impl Evaluator {
false => Ok(Object::False),
},
},
_ => anyhow::bail!("TypeMismatch"),
right_hand_object => anyhow::bail!(
"Type mismatch, cannot apply the {} infix operator to operands of type INTEGER and {}.",
operator,
right_hand_object.type_name()
),
},
Object::True => match self.evaluate_expression(right_hand)? {
Object::True => evaluate_same_boolean(operator),
Object::False => evaluate_opposite_boolean(operator),
_ => anyhow::bail!("TypeMismatch"),
right_hand_object => anyhow::bail!(
"Type mismatch, cannot apply the {} infix operator to operands of type BOOLEAN and {}.",
operator,
right_hand_object.type_name()
),
},
Object::False => match self.evaluate_expression(right_hand)? {
Object::False => evaluate_same_boolean(operator),
Object::True => evaluate_opposite_boolean(operator),
_ => anyhow::bail!("TypeMismatch"),
right_hand_object => anyhow::bail!(
"Type mismatch, cannot apply the {} infix operator to operands of type BOOLEAN and {}.",
operator,
right_hand_object.type_name()
),
},
Object::String { value: left_value } => match self.evaluate_expression(right_hand)? {
Object::String { value: right_value } => match operator {
Expand All @@ -86,11 +104,22 @@ impl Evaluator {
value: concatenated,
})
}
_ => anyhow::bail!("UnknownOperator"),
_ => anyhow::bail!(
"Unknown operator, the {} infix operator is not supported for operands of type STRING.",
operator
),
},
_ => anyhow::bail!("TypeMismatch"),
right_hand_object => anyhow::bail!(
"Type mismatch, cannot apply the {} infix operator to operands of type STRING and {}.",
operator,
right_hand_object.type_name()
),
},
_ => anyhow::bail!("UnknownOperator"),
left_hand_object => anyhow::bail!(
"Unknown operator, the {} infix operator is not supported for operands of type {}.",
operator,
left_hand_object.type_name()
),
}
}
}
10 changes: 8 additions & 2 deletions src/evaluator/expression/prefix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ impl Evaluator {
match object {
Object::True => Ok(Object::False),
Object::False => Ok(Object::True),
_ => anyhow::bail!("TypeMismatch"),
object => anyhow::bail!(
"Type mismatch, cannot apply the ! prefix operator to an operand of type {}.",
object.type_name()
),
}
}

Expand All @@ -24,7 +27,10 @@ impl Evaluator {

match object {
Object::Integer { value } => Ok(Object::Integer { value: -value }),
_ => anyhow::bail!("TypeMismatch"),
object => anyhow::bail!(
"Type mismatch, cannot apply the - prefix operator to an operand of type {}.",
object.type_name()
),
}
}
}
13 changes: 13 additions & 0 deletions src/evaluator/model/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ impl fmt::Display for Object {
}
}
}

impl Object {
pub(crate) fn type_name(&self) -> &'static str {
match self {
Object::Return { .. } => "RETURN",
Object::Integer { .. } => "INTEGER",
Object::String { .. } => "STRING",
Object::True | Object::False => "BOOLEAN",
Object::Null => "NULL",
Object::Function { .. } => "FUNCTION",
}
}
}
5 changes: 4 additions & 1 deletion src/evaluator/statement/let_statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ impl Evaluator {
let expression = self.evaluate_expression(expression)?;

if let Object::Return { value: _ } = expression {
anyhow::bail!("UnassignableObject");
anyhow::bail!(
"Cannot assign an object of type RETURN to the identifier {}.",
identifier
);
}

self.environment.set(identifier, expression);
Expand Down
4 changes: 3 additions & 1 deletion src/syntax_analysis/expression/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ impl SyntaxAnalysis<'_> {
}
},
None => {
anyhow::bail!("A {context} ended abruptly.");
anyhow::bail!(
"A {context} ended abruptly, expecting either a Comma or ClosingRoundBracket token."
);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/syntax_analysis/model/syntax_tree_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ pub(crate) enum Expression {
arguments: Vec<Expression>,
},
}

impl std::fmt::Display for InfixOperator {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let operator = match self {
InfixOperator::Plus => "+",
InfixOperator::Minus => "-",
InfixOperator::Multiply => "*",
InfixOperator::Divide => "/",
InfixOperator::Equals => "==",
InfixOperator::NotEquals => "!=",
InfixOperator::LesserThan => "<",
InfixOperator::GreaterThan => ">",
};

formatter.write_str(operator)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 31
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the == infix operator to operands of type BOOLEAN and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 31
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the == infix operator to operands of type STRING and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 31
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the != infix operator to operands of type INTEGER and STRING.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 26
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - prefix operator to an operand of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 26
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - prefix operator to an operand of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 26
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - prefix operator to an operand of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 26
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - prefix operator to an operand of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - infix operator to operands of type STRING and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the + infix operator to operands of type STRING and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - infix operator to operands of type STRING and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the * infix operator to operands of type STRING and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the * infix operator to operands of type INTEGER and STRING.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the + infix operator to operands of type INTEGER and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - infix operator to operands of type INTEGER and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the * infix operator to operands of type BOOLEAN and INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the / infix operator to operands of type INTEGER and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the + infix operator to operands of type STRING and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the - infix operator to operands of type BOOLEAN and STRING.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the * infix operator to operands of type BOOLEAN and STRING.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the / infix operator to operands of type STRING and BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 48
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the + infix operator to operands of type INTEGER and STRING.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 23
expression: error
---
Err(
"TypeMismatch",
"Type mismatch, cannot apply the ! prefix operator to an operand of type INTEGER.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 32
expression: error
---
Err(
"UnknownOperator",
"Unknown operator, the + infix operator is not supported for operands of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 32
expression: error
---
Err(
"UnknownOperator",
"Unknown operator, the - infix operator is not supported for operands of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 32
expression: error
---
Err(
"UnknownOperator",
"Unknown operator, the * infix operator is not supported for operands of type BOOLEAN.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ assertion_line: 32
expression: error
---
Err(
"UnknownOperator",
"Unknown operator, the / infix operator is not supported for operands of type BOOLEAN.",
)