1
0

Modify return/break syntax; Change Value::String

This commit is contained in:
Jeff 2024-02-19 22:32:06 -05:00
parent 25e3941315
commit cc76ca89cc
19 changed files with 24466 additions and 25924 deletions

View File

@ -90,8 +90,9 @@ impl AbstractTree for As {
Value::List(list) => Value::List(list),
Value::String(string) => {
let chars = string
.read()?
.chars()
.map(|char| Value::String(char.to_string()))
.map(|char| Value::string(char))
.collect();
Value::List(List::with_items(chars))

View File

@ -65,7 +65,7 @@ impl AbstractTree for Command {
.wait_with_output()?
.stdout;
Ok(Value::String(String::from_utf8(output)?))
Ok(Value::string(String::from_utf8(output)?))
}
}

View File

@ -99,7 +99,7 @@ impl AbstractTree for Index {
}
} else {
let index_value = self.index.run(source, context)?;
let identifier = Identifier::new(index_value.as_string()?);
let identifier = Identifier::new(index_value.as_string()?.as_str());
if let Some(value) = map.get(&identifier) {
value
@ -114,7 +114,7 @@ impl AbstractTree for Index {
}
Value::String(string) => {
let index = self.index.run(source, context)?.as_integer()? as usize;
let item = string.chars().nth(index).unwrap_or_default();
let item = string.read()?.chars().nth(index).unwrap_or_default();
Ok(Value::string(item.to_string()))
}

View File

@ -50,7 +50,7 @@ impl AbstractTree for IndexAssignment {
identifier
} else {
let index_run = self.index.index.run(source, context)?;
let expected_identifier = Identifier::new(index_run.as_string()?);
let expected_identifier = Identifier::new(index_run.as_string()?.as_str());
return Err(RuntimeError::ValidationFailure(
ValidationError::VariableIdentifierNotFound(expected_identifier),

View File

@ -46,7 +46,7 @@ impl Callable for Fs {
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
let path = arguments.first().unwrap().as_string()?;
let mut file = File::open(path)?;
let mut file = File::open(path.as_str())?;
let file_size = file.metadata()?.len() as usize;
let mut file_content = String::with_capacity(file_size);

View File

@ -54,7 +54,7 @@ impl Callable for Json {
let value = arguments.first().unwrap();
let json_string = serde_json::to_string(value)?;
Ok(Value::String(json_string))
Ok(Value::string(json_string))
}
Json::CreatePretty => {
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
@ -62,13 +62,13 @@ impl Callable for Json {
let value = arguments.first().unwrap();
let json_string = serde_json::to_string_pretty(value)?;
Ok(Value::String(json_string))
Ok(Value::string(json_string))
}
Json::Parse => {
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
let json_string = arguments.first().unwrap().as_string()?;
let value = serde_json::from_str(json_string)?;
let value = serde_json::from_str(json_string.as_str())?;
Ok(value)
}

View File

@ -221,19 +221,17 @@ impl Callable for StrFunction {
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
let string = arguments.first().unwrap().as_string()?;
let pattern_string = arguments.get(1).unwrap().as_string()?;
let pattern = pattern_string.as_str();
let pattern = arguments.get(1).unwrap().as_string()?;
Value::Boolean(string.ends_with(pattern))
Value::Boolean(string.ends_with(pattern.as_str()))
}
StrFunction::Find => {
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
let string = arguments.first().unwrap().as_string()?;
let pattern_string = arguments.get(1).unwrap().as_string()?;
let pattern = pattern_string.as_str();
let pattern = arguments.get(1).unwrap().as_string()?;
let find = string
.find(pattern)
.find(pattern.as_str())
.map(|index| Value::Integer(index as i64));
if let Some(index) = find {
@ -271,9 +269,9 @@ impl Callable for StrFunction {
let index = arguments.get(1).unwrap().as_integer()? as usize;
let insertion = arguments.get(2).unwrap().as_string()?;
string.insert_str(index, insertion);
string.insert_str(index, insertion.as_str());
Value::String(string)
Value::none()
}
StrFunction::Lines => {
RuntimeError::expect_argument_amount(self.name(), 1, arguments.len())?;
@ -339,9 +337,9 @@ impl Callable for StrFunction {
let end = range[1].as_integer()? as usize;
let pattern = arguments.get(2).unwrap().as_string()?;
string.replace_range(start..end, pattern);
string.replace_range(start..end, pattern.as_str());
Value::String(string)
Value::string(string)
}
StrFunction::Retain => {
RuntimeError::expect_argument_amount(self.name(), 2, arguments.len())?;
@ -576,13 +574,9 @@ impl Callable for StrFunction {
let input_string = arguments.first().unwrap().as_string()?;
let new_length = arguments.get(1).unwrap().as_integer()? as usize;
let new_string = input_string
.chars()
.take(new_length)
.map(|char| char.to_string())
.collect();
let new_string = input_string.chars().take(new_length).collect::<String>();
Value::String(new_string)
Value::string(new_string)
}
};

View File

@ -1,4 +1,7 @@
use std::fmt::{self, Display, Formatter};
use std::{
fmt::{self, Display, Formatter},
sync::PoisonError,
};
use colored::Colorize;
use lyneate::Report;
@ -261,6 +264,12 @@ impl ValidationError {
}
}
impl<T> From<PoisonError<T>> for ValidationError {
fn from(_: PoisonError<T>) -> Self {
ValidationError::RwLock(RwLockError)
}
}
impl From<RwLockError> for ValidationError {
fn from(_error: RwLockError) -> Self {
ValidationError::RwLock(RwLockError)

View File

@ -18,6 +18,7 @@ use std::{
fmt::{self, Display, Formatter},
marker::PhantomData,
ops::RangeInclusive,
sync::{Arc, RwLock, RwLockReadGuard},
};
pub use self::{
@ -36,7 +37,7 @@ pub mod struct_instance;
/// Every dust variable has a key and a Value. Variables are represented by
/// storing them in a VariableMap. This means the map of variables is itself a
/// value that can be treated as any other.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Value {
Boolean(bool),
Enum(EnumInstance),
@ -46,7 +47,7 @@ pub enum Value {
List(List),
Map(Map),
Range(RangeInclusive<i64>),
String(String),
String(Arc<RwLock<String>>),
Struct(StructInstance),
}
@ -64,7 +65,7 @@ impl Value {
}
pub fn string<T: Into<String>>(string: T) -> Self {
Value::String(string.into())
Value::String(Arc::new(RwLock::new(string.into())))
}
pub fn range(start: i64, end: i64) -> Self {
@ -157,11 +158,11 @@ impl Value {
self == &Value::none()
}
/// Borrows the value stored in `self` as `&String`, or returns `Err` if
/// Borrows the value stored in `self` as `&str`, or returns `Err` if
/// `self` is not a `Value::String`.
pub fn as_string(&self) -> Result<&String, ValidationError> {
pub fn as_string(&self) -> Result<RwLockReadGuard<String>, ValidationError> {
match self {
Value::String(string) => Ok(string),
Value::String(string) => Ok(string.read()?),
value => Err(ValidationError::ExpectedString {
actual: value.clone(),
}),
@ -274,7 +275,12 @@ impl Value {
Ok(Value::List(list))
}
(Value::String(left), Value::String(right)) => Ok(Value::String(left + &right)),
(Value::String(left), Value::String(right)) => {
let left = left.read()?.to_string();
let right = right.read()?;
Ok(Value::string(left + right.as_str()))
}
(left, right) => Err(ValidationError::CannotAdd {
left,
right,
@ -360,7 +366,13 @@ impl PartialEq for Value {
(Value::Integer(left), Value::Integer(right)) => left == right,
(Value::Float(left), Value::Float(right)) => left == right,
(Value::Boolean(left), Value::Boolean(right)) => left == right,
(Value::String(left), Value::String(right)) => left == right,
(Value::String(left), Value::String(right)) => {
if let (Ok(left), Ok(right)) = (left.read(), right.read()) {
left.as_str() == right.as_str()
} else {
false
}
}
(Value::List(left), Value::List(right)) => left == right,
(Value::Map(left), Value::Map(right)) => left == right,
(Value::Function(left), Value::Function(right)) => left == right,
@ -381,7 +393,13 @@ impl PartialOrd for Value {
impl Ord for Value {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Value::String(left), Value::String(right)) => left.cmp(right),
(Value::String(left), Value::String(right)) => {
if let (Ok(left), Ok(right)) = (left.read(), right.read()) {
left.cmp(&right)
} else {
Ordering::Equal
}
}
(Value::String(_), _) => Ordering::Greater,
(Value::Float(left), Value::Float(right)) => left.total_cmp(right),
(Value::Integer(left), Value::Integer(right)) => left.cmp(right),
@ -424,7 +442,13 @@ impl Serialize for Value {
S: Serializer,
{
match self {
Value::String(inner) => serializer.serialize_str(inner),
Value::String(inner) => {
let inner = inner
.read()
.map_err(|_| serde::ser::Error::custom("failed to get read lock on string"))?;
serializer.serialize_str(inner.as_str())
}
Value::Float(inner) => serializer.serialize_f64(*inner),
Value::Integer(inner) => serializer.serialize_i64(*inner),
Value::Boolean(inner) => serializer.serialize_bool(*inner),
@ -461,10 +485,33 @@ impl Serialize for Value {
}
}
impl Clone for Value {
fn clone(&self) -> Self {
log::trace!("Cloning value {self}");
match self {
Value::Boolean(boolean) => Value::Boolean(*boolean),
Value::Enum(r#enum) => Value::Enum(r#enum.clone()),
Value::Float(float) => Value::Float(*float),
Value::Function(function) => Value::Function(function.clone()),
Value::Integer(integer) => Value::Integer(*integer),
Value::List(list) => Value::List(list.clone()),
Value::Map(map) => Value::Map(map.clone()),
Value::Range(range) => Value::Range(range.clone()),
Value::String(string) => Value::String(string.clone()),
Value::Struct(r#struct) => Value::Struct(r#struct.clone()),
}
}
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Value::String(string) => write!(f, "{string}"),
Value::String(string) => {
let string = string.read().map_err(|_| fmt::Error)?;
write!(f, "{}", string.as_str())
}
Value::Float(float) => write!(f, "{float}"),
Value::Integer(int) => write!(f, "{int}"),
Value::Boolean(boolean) => write!(f, "{boolean}"),
@ -531,7 +578,7 @@ impl TryFrom<Value> for String {
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
if let Value::String(string) = value {
Ok(string)
Ok(string.read()?.clone())
} else {
Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedString { actual: value },

View File

@ -8,12 +8,12 @@ fn string_as_string_list() {
assert_eq!(
interpret("'foobar' as [str]"),
Ok(Value::List(List::with_items(vec![
Value::String("f".to_string()),
Value::String("o".to_string()),
Value::String("o".to_string()),
Value::String("b".to_string()),
Value::String("a".to_string()),
Value::String("r".to_string()),
Value::string("f"),
Value::string("o"),
Value::string("o"),
Value::string("b"),
Value::string("a"),
Value::string("r"),
])))
)
}

View File

@ -40,10 +40,10 @@ fn find() {
fn insert() {
assert_eq!(
interpret("str:insert('ac', 1, 'b')"),
Ok(Value::String("abc".to_string()))
Ok(Value::string("abc"))
);
assert_eq!(
interpret("str:insert('foo', 3, 'bar')"),
Ok(Value::String("foobar".to_string()))
Ok(Value::string("foobar"))
);
}

View File

@ -17,7 +17,7 @@ fn override_built_ins() {
Ok(Value::Enum(EnumInstance::new(
Identifier::new("Option"),
Identifier::new("Some"),
Some(Value::String("foo".to_string())),
Some(Value::string("foo")),
)))
);
}
@ -65,7 +65,7 @@ fn result() {
Ok(Value::Enum(EnumInstance::new(
Identifier::new("Result"),
Identifier::new("Error"),
Some(Value::String("uh-oh!".to_string())),
Some(Value::string("uh-oh!")),
)))
);
}

View File

@ -2,7 +2,7 @@ use dust_lang::{interpret, Value};
#[test]
fn simple_command() {
assert_eq!(interpret("^echo hi"), Ok(Value::String("hi\n".to_string())))
assert_eq!(interpret("^echo hi"), Ok(Value::string("hi\n")))
}
#[test]

View File

@ -18,7 +18,7 @@ fn simple_struct() {
let mut map = Map::new();
map.set(Identifier::new("bar"), Value::Integer(0));
map.set(Identifier::new("baz"), Value::String("hiya".to_string()));
map.set(Identifier::new("baz"), Value::string("hiya"));
let expected = Ok(Value::Struct(StructInstance::new(
Identifier::new("Foo"),

View File

@ -43,6 +43,9 @@ Block with Return
(expression
(value
(integer)))))
(statement
(statement_kind
(return)))
(statement
(statement_kind
(expression

View File

@ -14,19 +14,33 @@ module.exports = grammar({
statement: $ =>
prec.left(
seq(
optional(
choice('return', 'break'),
),
$.statement_kind,
optional(';'),
),
),
break: $ =>
prec.left(
seq(
'break',
optional($.statement),
),
),
return: $ =>
prec.left(
seq(
'return',
optional($.statement),
),
),
statement_kind: $ =>
prec.left(
choice(
$.assignment,
$.block,
$.break,
$.expression,
$.for,
$.if_else,
@ -34,6 +48,7 @@ module.exports = grammar({
$.loop_node,
$.match,
$.pipe,
$.return,
$.while,
$.type_definition,
),
@ -329,10 +344,7 @@ module.exports = grammar({
),
loop_node: $ =>
seq(
'loop',
$.block,
),
seq('loop', $.block),
while: $ =>
seq(
@ -372,7 +384,7 @@ module.exports = grammar({
// Custom type with arguments
seq(
$.identifier,
$.type_arguments
$.type_arguments,
),
// Map with exact fields
@ -473,10 +485,7 @@ module.exports = grammar({
seq(
'<',
repeat1(
seq(
$.type,
optional(','),
),
seq($.type, optional(',')),
),
'>',
),
@ -493,7 +502,9 @@ module.exports = grammar({
repeat1(
seq(
$.identifier,
optional($.type_arguments),
optional(
$.type_arguments,
),
optional(','),
),
),

View File

@ -23,27 +23,6 @@
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "return"
},
{
"type": "STRING",
"value": "break"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "statement_kind"
@ -63,6 +42,56 @@
]
}
},
"break": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "break"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "BLANK"
}
]
}
]
}
},
"return": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "return"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "BLANK"
}
]
}
]
}
},
"statement_kind": {
"type": "PREC_LEFT",
"value": 0,
@ -77,6 +106,10 @@
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "break"
},
{
"type": "SYMBOL",
"name": "expression"
@ -105,6 +138,10 @@
"type": "SYMBOL",
"name": "pipe"
},
{
"type": "SYMBOL",
"name": "return"
},
{
"type": "SYMBOL",
"name": "while"

View File

@ -70,6 +70,21 @@
"named": true,
"fields": {}
},
{
"type": "break",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "command",
"named": true,
@ -598,6 +613,21 @@
]
}
},
{
"type": "return",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "root",
"named": true,
@ -644,6 +674,10 @@
"type": "block",
"named": true
},
{
"type": "break",
"named": true
},
{
"type": "expression",
"named": true
@ -672,6 +706,10 @@
"type": "pipe",
"named": true
},
{
"type": "return",
"named": true
},
{
"type": "type_definition",
"named": true

File diff suppressed because it is too large Load Diff