Begin implementing range value
This commit is contained in:
parent
9299131024
commit
270c2fd1dc
@ -21,6 +21,7 @@ pub enum Type {
|
||||
None,
|
||||
Number,
|
||||
String,
|
||||
Range,
|
||||
Option(Box<Type>),
|
||||
}
|
||||
|
||||
@ -266,6 +267,7 @@ impl Format for Type {
|
||||
optional_type.format(output, indent_level);
|
||||
output.push(')');
|
||||
}
|
||||
Type::Range => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -304,6 +306,7 @@ impl Display for Type {
|
||||
Type::Option(inner_type) => {
|
||||
write!(f, "option({})", inner_type)
|
||||
}
|
||||
Type::Range => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,14 +4,7 @@
|
||||
//! 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.
|
||||
pub use crate::{
|
||||
abstract_tree::*,
|
||||
built_in_functions::BuiltInFunction,
|
||||
error::*,
|
||||
interpret::*,
|
||||
value::{
|
||||
function::Function, list::List, map::Map, structure::Structure,
|
||||
type_definition::TypeDefintion, Value,
|
||||
},
|
||||
abstract_tree::*, built_in_functions::BuiltInFunction, error::*, interpret::*, value::*,
|
||||
};
|
||||
|
||||
pub use tree_sitter::Node as SyntaxNode;
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Types that represent runtime values.
|
||||
use crate::{
|
||||
error::{Error, Result},
|
||||
Function, Identifier, List, Map, Type, TypeDefintion, TypeSpecification,
|
||||
Identifier, Type, TypeSpecification,
|
||||
};
|
||||
|
||||
use serde::{
|
||||
@ -18,9 +18,15 @@ use std::{
|
||||
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 list;
|
||||
pub mod map;
|
||||
pub mod range;
|
||||
pub mod structure;
|
||||
pub mod type_definition;
|
||||
|
||||
@ -38,6 +44,7 @@ pub enum Value {
|
||||
Float(f64),
|
||||
Integer(i64),
|
||||
Boolean(bool),
|
||||
Range(Range),
|
||||
Option(Option<Box<Value>>),
|
||||
TypeDefinition(TypeDefintion),
|
||||
}
|
||||
@ -54,8 +61,6 @@ impl Value {
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> Type {
|
||||
|
||||
|
||||
match self {
|
||||
Value::List(list) => {
|
||||
let mut previous_type = None;
|
||||
@ -103,6 +108,7 @@ impl Value {
|
||||
}
|
||||
}
|
||||
Value::TypeDefinition(_) => todo!(),
|
||||
Value::Range(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -472,6 +478,8 @@ impl Ord for Value {
|
||||
(Value::Function(_), _) => Ordering::Greater,
|
||||
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right),
|
||||
(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(_), _) => Ordering::Less,
|
||||
}
|
||||
@ -502,6 +510,7 @@ impl Serialize for Value {
|
||||
Value::Map(inner) => inner.serialize(serializer),
|
||||
Value::Function(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::Function(function) => write!(f, "{function}"),
|
||||
Value::TypeDefinition(structure) => write!(f, "{structure}"),
|
||||
Value::Range(range) => write!(f, "{range}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
src/value/range.rs
Normal file
15
src/value/range.rs
Normal 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)
|
||||
}
|
||||
}
|
@ -85,10 +85,8 @@ Nested Indexes
|
||||
(value
|
||||
(integer)))))
|
||||
(index_expression
|
||||
(value
|
||||
(integer)))
|
||||
(expression
|
||||
(value
|
||||
(range
|
||||
(integer)
|
||||
(integer)))))))
|
||||
|
||||
================================================================================
|
||||
|
15
tree-sitter-dust/corpus/range.txt
Normal file
15
tree-sitter-dust/corpus/range.txt
Normal file
@ -0,0 +1,15 @@
|
||||
================================================================================
|
||||
Simple Range
|
||||
================================================================================
|
||||
|
||||
0..10
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(root
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(range
|
||||
(integer)
|
||||
(integer))))))
|
@ -86,6 +86,14 @@ module.exports = grammar({
|
||||
$.option,
|
||||
$.built_in_value,
|
||||
$.structure,
|
||||
$.range,
|
||||
),
|
||||
|
||||
range: $ =>
|
||||
seq(
|
||||
$.integer,
|
||||
token.immediate('..'),
|
||||
$.integer,
|
||||
),
|
||||
|
||||
structure: $ =>
|
||||
@ -253,9 +261,6 @@ module.exports = grammar({
|
||||
$.index_expression,
|
||||
':',
|
||||
$.index_expression,
|
||||
optional(
|
||||
seq('..', $.expression),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@ -271,6 +276,7 @@ module.exports = grammar({
|
||||
$.identifier,
|
||||
$.index,
|
||||
$.value,
|
||||
$.range,
|
||||
),
|
||||
),
|
||||
|
||||
|
@ -252,6 +252,30 @@
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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",
|
||||
"name": "index_expression"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ".."
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -815,6 +818,10 @@
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "range"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -279,10 +279,6 @@
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "expression",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "index_expression",
|
||||
"named": true
|
||||
@ -333,6 +329,10 @@
|
||||
"type": "index",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "range",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "value",
|
||||
"named": true
|
||||
@ -483,6 +483,21 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "range",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "integer",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "return",
|
||||
"named": true,
|
||||
@ -657,6 +672,10 @@
|
||||
"type": "option",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "range",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"named": true
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user