Implement random tools
This commit is contained in:
parent
3d143cc64c
commit
b5e659f09f
@ -15,7 +15,7 @@ remove_card = function <opponent_card> {
|
||||
}
|
||||
}
|
||||
|
||||
if removed == 'empty' {
|
||||
if (type removed) == 'empty' {
|
||||
(output 'Card not found.')
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ use std::{
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use rand::{random, thread_rng, Rng};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
@ -40,6 +41,12 @@ pub enum Tool {
|
||||
Raw(Vec<Expression>),
|
||||
Sh(Vec<Expression>),
|
||||
Zsh(Vec<Expression>),
|
||||
|
||||
// Random
|
||||
Random(Vec<Expression>),
|
||||
RandomBoolean,
|
||||
RandomInteger,
|
||||
RandomFloat,
|
||||
}
|
||||
|
||||
impl AbstractTree for Tool {
|
||||
@ -188,6 +195,14 @@ impl AbstractTree for Tool {
|
||||
|
||||
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 {
|
||||
expected: "built-in tool",
|
||||
@ -460,6 +475,17 @@ impl AbstractTree for Tool {
|
||||
|
||||
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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -287,6 +287,12 @@ module.exports = grammar({
|
||||
'raw',
|
||||
'sh',
|
||||
'zsh',
|
||||
|
||||
// Random
|
||||
'random',
|
||||
'random_boolean',
|
||||
'random_float',
|
||||
'random_integer',
|
||||
),
|
||||
}
|
||||
});
|
@ -1149,6 +1149,22 @@
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "zsh"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "random"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "random_boolean"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "random_float"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "random_integer"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -821,6 +821,22 @@
|
||||
"type": "output_error",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "random",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "random_boolean",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "random_float",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "random_integer",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "raw",
|
||||
"named": false
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user