1
0

Implement random tools

This commit is contained in:
Jeff 2023-10-22 14:48:34 -04:00
parent 3d143cc64c
commit b5e659f09f
6 changed files with 1928 additions and 1744 deletions

View File

@ -15,7 +15,7 @@ remove_card = function <opponent_card> {
} }
} }
if removed == 'empty' { if (type removed) == 'empty' {
(output 'Card not found.') (output 'Card not found.')
} }
} }

View File

@ -5,6 +5,7 @@ use std::{
process::Command, process::Command,
}; };
use rand::{random, thread_rng, Rng};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
@ -40,6 +41,12 @@ pub enum Tool {
Raw(Vec<Expression>), Raw(Vec<Expression>),
Sh(Vec<Expression>), Sh(Vec<Expression>),
Zsh(Vec<Expression>), Zsh(Vec<Expression>),
// Random
Random(Vec<Expression>),
RandomBoolean,
RandomInteger,
RandomFloat,
} }
impl AbstractTree for Tool { impl AbstractTree for Tool {
@ -188,6 +195,14 @@ impl AbstractTree for Tool {
Tool::Zsh(expressions) Tool::Zsh(expressions)
} }
"random" => {
let expressions = parse_expressions(source, node)?;
Tool::Random(expressions)
}
"random_boolean" => Tool::RandomBoolean,
"random_float" => Tool::RandomFloat,
"random_integer" => Tool::RandomInteger,
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "built-in tool", expected: "built-in tool",
@ -460,6 +475,17 @@ impl AbstractTree for Tool {
Ok(Value::String(String::from_utf8(output)?)) Ok(Value::String(String::from_utf8(output)?))
} }
Tool::Random(expressions) => {
let range = 0..expressions.len();
let random_index = thread_rng().gen_range(range);
let random_expression = expressions.get(random_index).unwrap();
let value = random_expression.run(source, context)?;
Ok(value)
}
Tool::RandomBoolean => Ok(Value::Boolean(random())),
Tool::RandomFloat => Ok(Value::Float(random())),
Tool::RandomInteger => Ok(Value::Integer(random())),
} }
} }
} }

View File

@ -287,6 +287,12 @@ module.exports = grammar({
'raw', 'raw',
'sh', 'sh',
'zsh', 'zsh',
// Random
'random',
'random_boolean',
'random_float',
'random_integer',
), ),
} }
}); });

View File

@ -1149,6 +1149,22 @@
{ {
"type": "STRING", "type": "STRING",
"value": "zsh" "value": "zsh"
},
{
"type": "STRING",
"value": "random"
},
{
"type": "STRING",
"value": "random_boolean"
},
{
"type": "STRING",
"value": "random_float"
},
{
"type": "STRING",
"value": "random_integer"
} }
] ]
} }

View File

@ -821,6 +821,22 @@
"type": "output_error", "type": "output_error",
"named": false "named": false
}, },
{
"type": "random",
"named": false
},
{
"type": "random_boolean",
"named": false
},
{
"type": "random_float",
"named": false
},
{
"type": "random_integer",
"named": false
},
{ {
"type": "raw", "type": "raw",
"named": false "named": false

File diff suppressed because it is too large Load Diff