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'] rooms = ['Library' 'Kitchen' 'Conservatory']
suspects = ['White' 'Green' 'Scarlett'] suspects = ['White' 'Green' 'Scarlett']
weapons = ['Rope' 'Lead_Pipe' 'Knife'] 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:suspects) == 1)
&& (length(cards:rooms) == 1) && (length(cards:rooms) == 1)
&& (length(cards:weapons) == 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:rooms -= opponent_card
cards:suspects -= opponent_card cards:suspects -= opponent_card
cards:weapons -= 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) { if is_ready_to_solve(cards) {
output( output(
'I accuse ' '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) remove_card(cards opponent_card)
make_guess(cards current_room) make_guess(cards current_room)
} }

View File

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

View File

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