Begin implementing range value

This commit is contained in:
Jeff 2024-01-23 17:35:12 -05:00
parent 9299131024
commit 270c2fd1dc
10 changed files with 14839 additions and 17758 deletions

View File

@ -21,6 +21,7 @@ pub enum Type {
None, None,
Number, Number,
String, String,
Range,
Option(Box<Type>), Option(Box<Type>),
} }
@ -266,6 +267,7 @@ impl Format for Type {
optional_type.format(output, indent_level); optional_type.format(output, indent_level);
output.push(')'); output.push(')');
} }
Type::Range => todo!(),
} }
} }
} }
@ -304,6 +306,7 @@ impl Display for Type {
Type::Option(inner_type) => { Type::Option(inner_type) => {
write!(f, "option({})", inner_type) write!(f, "option({})", inner_type)
} }
Type::Range => todo!(),
} }
} }
} }

View File

@ -4,14 +4,7 @@
//! Using this library is simple and straightforward, see the [inferface] module for instructions on //! Using this library is simple and straightforward, see the [inferface] module for instructions on
//! interpreting Dust code. Most of the language's features are implemented in the [tools] module. //! interpreting Dust code. Most of the language's features are implemented in the [tools] module.
pub use crate::{ pub use crate::{
abstract_tree::*, abstract_tree::*, built_in_functions::BuiltInFunction, error::*, interpret::*, value::*,
built_in_functions::BuiltInFunction,
error::*,
interpret::*,
value::{
function::Function, list::List, map::Map, structure::Structure,
type_definition::TypeDefintion, Value,
},
}; };
pub use tree_sitter::Node as SyntaxNode; pub use tree_sitter::Node as SyntaxNode;

View File

@ -1,7 +1,7 @@
//! Types that represent runtime values. //! Types that represent runtime values.
use crate::{ use crate::{
error::{Error, Result}, error::{Error, Result},
Function, Identifier, List, Map, Type, TypeDefintion, TypeSpecification, Identifier, Type, TypeSpecification,
}; };
use serde::{ use serde::{
@ -18,9 +18,15 @@ use std::{
ops::{Add, AddAssign, Div, Mul, Rem, Sub, SubAssign}, ops::{Add, AddAssign, Div, Mul, Rem, Sub, SubAssign},
}; };
pub use self::{
function::Function, list::List, map::Map, range::Range, structure::Structure,
type_definition::TypeDefintion,
};
pub mod function; pub mod function;
pub mod list; pub mod list;
pub mod map; pub mod map;
pub mod range;
pub mod structure; pub mod structure;
pub mod type_definition; pub mod type_definition;
@ -38,6 +44,7 @@ pub enum Value {
Float(f64), Float(f64),
Integer(i64), Integer(i64),
Boolean(bool), Boolean(bool),
Range(Range),
Option(Option<Box<Value>>), Option(Option<Box<Value>>),
TypeDefinition(TypeDefintion), TypeDefinition(TypeDefintion),
} }
@ -54,8 +61,6 @@ impl Value {
} }
pub fn r#type(&self) -> Type { pub fn r#type(&self) -> Type {
match self { match self {
Value::List(list) => { Value::List(list) => {
let mut previous_type = None; let mut previous_type = None;
@ -103,6 +108,7 @@ impl Value {
} }
} }
Value::TypeDefinition(_) => todo!(), Value::TypeDefinition(_) => todo!(),
Value::Range(_) => todo!(),
} }
} }
@ -472,6 +478,8 @@ impl Ord for Value {
(Value::Function(_), _) => Ordering::Greater, (Value::Function(_), _) => Ordering::Greater,
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right), (Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right),
(Value::TypeDefinition(_), _) => Ordering::Greater, (Value::TypeDefinition(_), _) => Ordering::Greater,
(Value::Range(left), Value::Range(right)) => left.cmp(right),
(Value::Range(_), _) => Ordering::Greater,
(Value::Option(left), Value::Option(right)) => left.cmp(right), (Value::Option(left), Value::Option(right)) => left.cmp(right),
(Value::Option(_), _) => Ordering::Less, (Value::Option(_), _) => Ordering::Less,
} }
@ -502,6 +510,7 @@ impl Serialize for Value {
Value::Map(inner) => inner.serialize(serializer), Value::Map(inner) => inner.serialize(serializer),
Value::Function(inner) => inner.serialize(serializer), Value::Function(inner) => inner.serialize(serializer),
Value::TypeDefinition(inner) => inner.serialize(serializer), Value::TypeDefinition(inner) => inner.serialize(serializer),
Value::Range(range) => range.serialize(serializer),
} }
} }
} }
@ -524,6 +533,7 @@ impl Display for Value {
Value::Map(map) => write!(f, "{map}"), Value::Map(map) => write!(f, "{map}"),
Value::Function(function) => write!(f, "{function}"), Value::Function(function) => write!(f, "{function}"),
Value::TypeDefinition(structure) => write!(f, "{structure}"), Value::TypeDefinition(structure) => write!(f, "{structure}"),
Value::Range(range) => write!(f, "{range}"),
} }
} }
} }

15
src/value/range.rs Normal file
View File

@ -0,0 +1,15 @@
use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Range {
start: i64,
end: i64,
}
impl Display for Range {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}

View File

@ -85,10 +85,8 @@ Nested Indexes
(value (value
(integer))))) (integer)))))
(index_expression (index_expression
(value (range
(integer))) (integer)
(expression
(value
(integer))))))) (integer)))))))
================================================================================ ================================================================================

View File

@ -0,0 +1,15 @@
================================================================================
Simple Range
================================================================================
0..10
--------------------------------------------------------------------------------
(root
(statement
(expression
(value
(range
(integer)
(integer))))))

View File

@ -86,6 +86,14 @@ module.exports = grammar({
$.option, $.option,
$.built_in_value, $.built_in_value,
$.structure, $.structure,
$.range,
),
range: $ =>
seq(
$.integer,
token.immediate('..'),
$.integer,
), ),
structure: $ => structure: $ =>
@ -253,9 +261,6 @@ module.exports = grammar({
$.index_expression, $.index_expression,
':', ':',
$.index_expression, $.index_expression,
optional(
seq('..', $.expression),
),
), ),
), ),
@ -271,6 +276,7 @@ module.exports = grammar({
$.identifier, $.identifier,
$.index, $.index,
$.value, $.value,
$.range,
), ),
), ),

View File

@ -252,6 +252,30 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "structure" "name": "structure"
},
{
"type": "SYMBOL",
"name": "range"
}
]
},
"range": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "integer"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": ".."
}
},
{
"type": "SYMBOL",
"name": "integer"
} }
] ]
}, },
@ -756,27 +780,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "index_expression" "name": "index_expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ".."
},
{
"type": "SYMBOL",
"name": "expression"
}
]
},
{
"type": "BLANK"
}
]
} }
] ]
} }
@ -815,6 +818,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "value" "name": "value"
},
{
"type": "SYMBOL",
"name": "range"
} }
] ]
} }

View File

@ -279,10 +279,6 @@
"multiple": true, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "expression",
"named": true
},
{ {
"type": "index_expression", "type": "index_expression",
"named": true "named": true
@ -333,6 +329,10 @@
"type": "index", "type": "index",
"named": true "named": true
}, },
{
"type": "range",
"named": true
},
{ {
"type": "value", "type": "value",
"named": true "named": true
@ -483,6 +483,21 @@
] ]
} }
}, },
{
"type": "range",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "integer",
"named": true
}
]
}
},
{ {
"type": "return", "type": "return",
"named": true, "named": true,
@ -657,6 +672,10 @@
"type": "option", "type": "option",
"named": true "named": true
}, },
{
"type": "range",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true

File diff suppressed because it is too large Load Diff