1
0
dust/src/abstract_tree/while.rs

47 lines
1.3 KiB
Rust
Raw Normal View History

2023-10-07 02:45:36 +00:00
use serde::{Deserialize, Serialize};
2023-10-10 17:29:11 +00:00
use tree_sitter::Node;
2023-10-07 02:45:36 +00:00
2023-11-30 05:57:15 +00:00
use crate::{AbstractTree, Block, Expression, Map, Result, TypeDefinition, Value};
2023-10-07 02:45:36 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct While {
expression: Expression,
2023-10-31 17:04:22 +00:00
block: Block,
2023-10-07 02:45:36 +00:00
}
impl AbstractTree for While {
2023-11-30 03:54:46 +00:00
fn from_syntax_node(source: &str, node: Node, context: &Map) -> crate::Result<Self> {
2023-10-07 02:45:36 +00:00
debug_assert_eq!("while", node.kind());
let expression_node = node.child(1).unwrap();
2023-11-30 03:54:46 +00:00
let expression = Expression::from_syntax_node(source, expression_node, context)?;
2023-10-07 02:45:36 +00:00
2023-10-31 17:04:22 +00:00
let block_node = node.child(2).unwrap();
2023-11-30 03:54:46 +00:00
let block = Block::from_syntax_node(source, block_node, context)?;
2023-10-07 02:45:36 +00:00
2023-10-31 17:04:22 +00:00
Ok(While { expression, block })
2023-10-07 02:45:36 +00:00
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2023-10-10 17:29:11 +00:00
while self.expression.run(source, context)?.as_boolean()? {
2023-10-31 17:04:22 +00:00
self.block.run(source, context)?;
2023-10-07 02:45:36 +00:00
}
2023-10-31 13:31:10 +00:00
Ok(Value::Empty)
2023-10-07 02:45:36 +00:00
}
2023-11-30 00:23:42 +00:00
2023-11-30 05:57:15 +00:00
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
2023-11-30 00:23:42 +00:00
self.block.expected_type(context)
}
2023-10-07 02:45:36 +00:00
}
#[cfg(test)]
mod tests {
use crate::evaluate;
#[test]
fn evalualate_while_loop() {
2023-10-09 19:54:47 +00:00
assert_eq!(evaluate("while false { 'foo' }"), Ok(crate::Value::Empty))
2023-10-07 02:45:36 +00:00
}
}