1
0

Begin custom type implementation

This commit is contained in:
Jeff 2024-01-03 20:11:09 -05:00
parent ff6cc707d2
commit 531227ba2d
3 changed files with 21 additions and 15 deletions

View File

@ -1,22 +1,28 @@
cards = {
type Cards {
rooms <[str]>
suspects <[str]>
weapons <[str]>
}
cards = Cards {
rooms = ['Library' 'Kitchen' 'Conservatory']
suspects = ['White' 'Green' 'Scarlett']
weapons = ['Rope' 'Lead_Pipe' 'Knife']
}
is_ready_to_solve = (cards <map>) <bool> {
is_ready_to_solve = (cards <Cards>) <bool> {
(length(cards:suspects) == 1)
&& (length(cards:rooms) == 1)
&& (length(cards:weapons) == 1)
}
remove_card = (cards <map>, opponent_card <str>) <none> {
remove_card = (cards <Cards>, opponent_card <str>) <none> {
cards:rooms -= opponent_card
cards:suspects -= opponent_card
cards:weapons -= opponent_card
}
make_guess = (cards <map>, current_room <str>) <none> {
make_guess = (cards <Cards>, current_room <str>) <none> {
if is_ready_to_solve(cards) {
output(
'I accuse '
@ -40,7 +46,7 @@ make_guess = (cards <map>, current_room <str>) <none> {
}
}
take_turn = (cards <map>, opponent_card <str>, current_room <str>) <none> {
take_turn = (cards <Cards>, opponent_card <str>, current_room <str>) <none> {
remove_card(cards opponent_card)
make_guess(cards current_room)
}

View File

@ -52,8 +52,8 @@ impl AbstractTree for FunctionCall {
{
parameter_types
} else {
return Err(Error::ExpectedFunctionType {
actual: function_expression_type,
return Err(Error::ExpectedFunctionExpression {
actual: self.function_expression.clone(),
});
};

View File

@ -1,12 +1,10 @@
//! Error and Result types.
//!
//! To deal with errors from dependencies, either create a new error variant
//! or use the ToolFailure variant if the error can only occur inside a tool.
//! To deal with errors from dependencies, create a new Error variant.
use serde::{Deserialize, Serialize};
use tree_sitter::{LanguageError, Node, Point};
use crate::{value::Value, Type};
use crate::{value::Value, FunctionExpression, Type};
use std::{
fmt::{self, Formatter},
@ -83,8 +81,8 @@ pub enum Error {
actual: usize,
},
ExpectedFunctionType {
actual: Type,
ExpectedFunctionExpression {
actual: FunctionExpression,
},
ExpectedString {
@ -434,11 +432,13 @@ impl fmt::Display for Error {
f,
"Parsing was cancelled either manually or because it took too long."
),
ExpectedFunctionType { actual } => write!(f, "Expected a function but got {actual}."),
ExpectedFunctionExpression { actual } => {
write!(f, "Expected a function expression but got {actual:?}.")
}
}
}
}
fn get_position(position: &Point) -> String {
format!("column {}, row {}", position.row + 1, position.column)
format!("row {}, column {}", position.row + 1, position.column)
}