1
0

Implement Range value

This commit is contained in:
Jeff 2024-01-24 20:11:34 -05:00
parent f2e7badf4b
commit 52c6c3a507
4 changed files with 32 additions and 6 deletions

View File

@ -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<String, (Statement, Option<Type>)>),
BuiltInValue(BuiltInValue),
Structure(BTreeMap<String, (Option<Statement>, 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!(),
}
}
}

View File

@ -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,
}
}

View File

@ -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 {

View File

@ -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);
}