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 ```dust
io.write_line("Guess the number.") io.write_line("Guess the number.")
secret_number = random.range(0..100) secret_number = random.in_range(0..100)
loop { loop {
io.write_line("Input your guess.") io.write_line("Input your guess.")
input = io.read_line() input = io.read_line().expect("Failed to read line.")
guess = int.parse(input) guess = match input.trim().parse() {
Ok(value) => value,
Err(_) => continue,
}
if guess < secret_number { if guess < secret_number {
io.write_line("Too low!") io.write_line("Too low!")

View File

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

View File

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

View File

@ -23,7 +23,6 @@ use std::{
}; };
pub use abstract_tree::Type; pub use abstract_tree::Type;
use standard_library::core_context;
pub use value::Value; pub use value::Value;
use abstract_tree::AbstractTree; use abstract_tree::AbstractTree;
@ -32,6 +31,7 @@ use context::Context;
use error::{DustError, RuntimeError, TypeConflict, ValidationError}; use error::{DustError, RuntimeError, TypeConflict, ValidationError};
use lexer::{lex, Token}; use lexer::{lex, Token};
use parser::{parse, parser}; use parser::{parse, parser};
use standard_library::core_context;
pub fn interpret(source_id: &str, source: &str) -> Result<Option<Value>, InterpreterError> { pub fn interpret(source_id: &str, source: &str) -> Result<Option<Value>, InterpreterError> {
let interpreter = Interpreter::new(); 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::{ use crate::{
abstract_tree::{ abstract_tree::{AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type},
AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type, WithPosition,
},
context::Context, context::Context,
error::{RuntimeError, ValidationError}, error::{RuntimeError, ValidationError},
identifier::Identifier, 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 })) Value(Arc::new(ValueInner::Structure { name, fields }))
} }
@ -232,10 +230,10 @@ impl Display for Value {
write!(f, " {body}") write!(f, " {body}")
} }
ValueInner::Structure { name, fields } => { ValueInner::Structure { name, fields } => {
write!(f, "{}\n{{", name.node)?; write!(f, "{name} {{")?;
for (key, value) in fields { for (key, value) in fields {
writeln!(f, "{key} = {value},")?; write!(f, "{key} = {value},")?;
} }
write!(f, "}}") write!(f, "}}")
@ -599,7 +597,7 @@ pub enum ValueInner {
Range(Range<i64>), Range(Range<i64>),
String(String), String(String),
Structure { Structure {
name: WithPosition<Identifier>, name: Identifier,
fields: Vec<(Identifier, Value)>, fields: Vec<(Identifier, Value)>,
}, },
} }
@ -651,12 +649,12 @@ impl ValueInner {
} }
} }
ValueInner::Structure { name, .. } => { ValueInner::Structure { name, .. } => {
if let Some(r#type) = context.get_type(&name.node)? { if let Some(r#type) = context.get_type(&name)? {
r#type r#type
} else { } else {
return Err(ValidationError::VariableNotFound { return Err(ValidationError::StructDefinitionNotFound {
identifier: name.node.clone(), identifier: name.clone(),
position: name.position, position: None,
}); });
} }
} }