From 52c6c3a507700f58a9a3fa114e3f6d8a4569800b Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 24 Jan 2024 20:11:34 -0500 Subject: [PATCH] Implement Range value --- src/abstract_tree/value_node.rs | 22 ++++++++++++++++++---- src/value/mod.rs | 5 +++++ src/value/range.rs | 4 ++-- tests/value.rs | 7 +++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 9a4b679..59a3550 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize}; use crate::{ AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier, - List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, TypeSpecification, - Value, + List, Map, Range, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, + TypeSpecification, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -20,6 +20,7 @@ pub enum ValueNode { Map(BTreeMap)>), BuiltInValue(BuiltInValue), Structure(BTreeMap, Type)>), + Range(Range), } impl AbstractTree for ValueNode { @@ -162,10 +163,20 @@ impl AbstractTree for ValueNode { ValueNode::Structure(btree_map) } + "range" => { + let start_node = child.child(0).unwrap(); + let end_node = child.child(2).unwrap(); + + let start = source[start_node.byte_range()].parse().unwrap(); + let end = source[end_node.byte_range()].parse().unwrap(); + + ValueNode::Range(Range { start, end }) + } _ => { return Err(Error::UnexpectedSyntaxNode { - expected: "string, integer, float, boolean, list, map, option or structure" - .to_string(), + expected: + "string, integer, float, boolean, range, list, map, option or structure" + .to_string(), actual: child.kind().to_string(), location: child.start_position(), relevant_source: source[child.byte_range()].to_string(), @@ -245,6 +256,7 @@ impl AbstractTree for ValueNode { Value::TypeDefinition(TypeDefintion::Structure(Structure::new(value_map))) } + ValueNode::Range(range) => Value::Range(*range), }; Ok(value) @@ -296,6 +308,7 @@ impl AbstractTree for ValueNode { Type::Map(Some(Structure::new(value_map))) } + ValueNode::Range(_) => Type::Range, }; Ok(r#type) @@ -379,6 +392,7 @@ impl Format for ValueNode { output.push('}'); } + ValueNode::Range(_) => todo!(), } } } diff --git a/src/value/mod.rs b/src/value/mod.rs index 0dc0c16..cecde8d 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -60,6 +60,10 @@ impl Value { Value::String(string.into()) } + pub fn range(start: i64, end: i64) -> Self { + Value::Range(Range { start, end }) + } + pub fn r#type(&self) -> Type { match self { Value::List(list) => { @@ -440,6 +444,7 @@ impl PartialEq for Value { (Value::Function(left), Value::Function(right)) => left == right, (Value::Option(left), Value::Option(right)) => left == right, (Value::TypeDefinition(left), Value::TypeDefinition(right)) => left == right, + (Value::Range(left), Value::Range(right)) => left == right, _ => false, } } diff --git a/src/value/range.rs b/src/value/range.rs index cfe4adb..f4589cd 100644 --- a/src/value/range.rs +++ b/src/value/range.rs @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct Range { - start: i64, - end: i64, + pub start: i64, + pub end: i64, } impl Display for Range { diff --git a/tests/value.rs b/tests/value.rs index 9824c74..81fb5ce 100644 --- a/tests/value.rs +++ b/tests/value.rs @@ -116,3 +116,10 @@ fn option() { assert_eq!(Value::Option(Some(Box::new(Value::Integer(1)))), result); } + +#[test] +fn range() { + let result = interpret("0..100"); + + assert_eq!(Ok(Value::range(0, 100)), result); +}