From 02dd33ab1af30c6365d3dcff1124df15a080f9e5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 28 Jul 2024 12:52:07 -0400 Subject: [PATCH] Add error; Clean up struct values --- README.md | 9 ++++++--- dust-lang/src/abstract_tree/value_node.rs | 2 +- dust-lang/src/error.rs | 4 ++++ dust-lang/src/lib.rs | 6 +++++- dust-lang/src/value.rs | 20 +++++++++----------- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e495ac9..ebbd6e3 100644 --- a/README.md +++ b/README.md @@ -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!") diff --git a/dust-lang/src/abstract_tree/value_node.rs b/dust-lang/src/abstract_tree/value_node.rs index f818067..a5e17a8 100644 --- a/dust-lang/src/abstract_tree/value_node.rs +++ b/dust-lang/src/abstract_tree/value_node.rs @@ -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), }; diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index 29002ca..3591d6b 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -186,6 +186,10 @@ pub enum ValidationError { actual: usize, position: SourcePosition, }, + StructDefinitionNotFound { + identifier: Identifier, + position: Option, + }, } impl From for ValidationError { diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 7ecbd8b..d151694 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -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, InterpreterError> { let interpreter = Interpreter::new(); @@ -493,6 +493,10 @@ impl InterpreterError { ), ), ), + ValidationError::StructDefinitionNotFound { + identifier, + position, + } => todo!(), } } diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 093b7d0..e08831d 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -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, 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), String(String), Structure { - name: WithPosition, + 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, }); } }