1
0

Add error; Clean up struct values

This commit is contained in:
Jeff 2024-07-28 12:52:07 -04:00
parent 9640feb65b
commit 02dd33ab1a
5 changed files with 25 additions and 16 deletions

View File

@ -6,13 +6,16 @@ safety and familiar syntax.
```dust
io.write_line("Guess the number.")
secret_number = random.range(0..100)
secret_number = random.in_range(0..100)
loop {
io.write_line("Input your guess.")
input = io.read_line()
guess = int.parse(input)
input = io.read_line().expect("Failed to read line.")
guess = match input.trim().parse() {
Ok(value) => value,
Err(_) => continue,
}
if guess < secret_number {
io.write_line("Too low!")

View File

@ -418,7 +418,7 @@ impl AbstractNode for ValueNode {
fields.push((identifier.node, value));
}
Value::structure(name, fields)
Value::structure(name.node, fields)
}
ValueNode::BuiltInFunction(function) => Value::built_in_function(function),
};

View File

@ -186,6 +186,10 @@ pub enum ValidationError {
actual: usize,
position: SourcePosition,
},
StructDefinitionNotFound {
identifier: Identifier,
position: Option<SourcePosition>,
},
}
impl From<PoisonError> for ValidationError {

View File

@ -23,7 +23,6 @@ use std::{
};
pub use abstract_tree::Type;
use standard_library::core_context;
pub use value::Value;
use abstract_tree::AbstractTree;
@ -32,6 +31,7 @@ use context::Context;
use error::{DustError, RuntimeError, TypeConflict, ValidationError};
use lexer::{lex, Token};
use parser::{parse, parser};
use standard_library::core_context;
pub fn interpret(source_id: &str, source: &str) -> Result<Option<Value>, InterpreterError> {
let interpreter = Interpreter::new();
@ -493,6 +493,10 @@ impl InterpreterError {
),
),
),
ValidationError::StructDefinitionNotFound {
identifier,
position,
} => todo!(),
}
}

View File

@ -15,9 +15,7 @@ use serde::{
};
use crate::{
abstract_tree::{
AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type, WithPosition,
},
abstract_tree::{AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type},
context::Context,
error::{RuntimeError, ValidationError},
identifier::Identifier,
@ -91,7 +89,7 @@ impl Value {
))))
}
pub fn structure(name: WithPosition<Identifier>, fields: Vec<(Identifier, Value)>) -> Self {
pub fn structure(name: Identifier, fields: Vec<(Identifier, Value)>) -> Self {
Value(Arc::new(ValueInner::Structure { name, fields }))
}
@ -232,10 +230,10 @@ impl Display for Value {
write!(f, " {body}")
}
ValueInner::Structure { name, fields } => {
write!(f, "{}\n{{", name.node)?;
write!(f, "{name} {{")?;
for (key, value) in fields {
writeln!(f, "{key} = {value},")?;
write!(f, "{key} = {value},")?;
}
write!(f, "}}")
@ -599,7 +597,7 @@ pub enum ValueInner {
Range(Range<i64>),
String(String),
Structure {
name: WithPosition<Identifier>,
name: Identifier,
fields: Vec<(Identifier, Value)>,
},
}
@ -651,12 +649,12 @@ impl ValueInner {
}
}
ValueInner::Structure { name, .. } => {
if let Some(r#type) = context.get_type(&name.node)? {
if let Some(r#type) = context.get_type(&name)? {
r#type
} else {
return Err(ValidationError::VariableNotFound {
identifier: name.node.clone(),
position: name.position,
return Err(ValidationError::StructDefinitionNotFound {
identifier: name.clone(),
position: None,
});
}
}