From b392a4c7aa5b58c58e62f22e2fb01243a3b76144 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 23 Mar 2024 19:12:18 -0400 Subject: [PATCH] Improve errors and built-ins --- dust-lang/src/built_in_functions.rs | 5 +---- dust-lang/src/context.rs | 3 +++ dust-lang/src/error.rs | 3 +-- dust-shell/src/cli.rs | 14 +++++--------- dust-shell/src/error.rs | 28 ++++++++++++++++++++++++---- dust-shell/src/main.rs | 25 +++++++++++++++++++++---- std/thread.ds | 2 +- 7 files changed, 56 insertions(+), 24 deletions(-) diff --git a/dust-lang/src/built_in_functions.rs b/dust-lang/src/built_in_functions.rs index 08dbf92..1d227d2 100644 --- a/dust-lang/src/built_in_functions.rs +++ b/dust-lang/src/built_in_functions.rs @@ -45,7 +45,7 @@ impl BuiltInFunction { } } - pub fn call(&self, arguments: Vec, context: &Context) -> Result { + pub fn call(&self, arguments: Vec, _context: &Context) -> Result { match self { // "INT_RANDOM_RANGE" => { // let range = arguments.get(0).unwrap(); @@ -77,9 +77,6 @@ impl BuiltInFunction { Ok(Action::None) } - _ => { - todo!() - } } } } diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index c23c984..979429b 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -14,6 +14,9 @@ pub fn std_context() -> Context { let mut interpreter = Interpreter::new(context.clone()); interpreter.run(include_str!("../../std/io.ds")).unwrap(); + interpreter + .run(include_str!("../../std/thread.ds")) + .unwrap(); context } diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index d5fafc4..e13e085 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -1,6 +1,5 @@ -use std::{fmt::Debug, hash::Hash, io, ops::Range, sync::PoisonError}; +use std::{io, sync::PoisonError}; -use ariadne::{Color, Fmt, Label, Report, ReportKind}; use chumsky::{prelude::Rich, span::Span}; use crate::{ diff --git a/dust-shell/src/cli.rs b/dust-shell/src/cli.rs index dd11449..450c3a0 100644 --- a/dust-shell/src/cli.rs +++ b/dust-shell/src/cli.rs @@ -1,10 +1,4 @@ -use std::{ - borrow::Cow, - io::{stderr, Write}, - path::PathBuf, - process::Command, - rc::Rc, -}; +use std::{borrow::Cow, io::stderr, path::PathBuf, process::Command, rc::Rc}; use ariadne::sources; use dust_lang::{ @@ -20,7 +14,7 @@ use reedline::{ use crate::error::Error; -pub fn run_shell(context: Context) { +pub fn run_shell(context: Context) -> Result<(), Error> { let mut interpreter = Interpreter::new(context.clone()); let mut keybindings = default_emacs_keybindings(); @@ -92,7 +86,7 @@ pub fn run_shell(context: Context) { Err(errors) => { let source_id = Rc::new("input".to_string()); let reports = Error::Dust { errors } - .build_reports(source_id.clone(), &buffer) + .build_reports(source_id.clone()) .unwrap(); for report in reports { @@ -114,6 +108,8 @@ pub fn run_shell(context: Context) { } } } + + Ok(()) } struct StarshipPrompt { diff --git a/dust-shell/src/error.rs b/dust-shell/src/error.rs index eb3447b..4d74f17 100644 --- a/dust-shell/src/error.rs +++ b/dust-shell/src/error.rs @@ -1,14 +1,12 @@ use ariadne::{Color, Fmt, Label, Report, ReportKind}; -use clap::error; use dust_lang::{ abstract_tree::Type, error::{Error as DustError, RuntimeError, TypeConflict, ValidationError}, }; use std::{ - fmt::{self, Debug, Display, Formatter}, + fmt::{self, Display, Formatter}, io, ops::Range, - path::Path, rc::Rc, }; @@ -20,11 +18,16 @@ pub enum Error { Io(io::Error), } +impl From for Error { + fn from(error: io::Error) -> Self { + Error::Io(error) + } +} + impl Error { pub fn build_reports<'id>( self, source_id: Rc, - source: &str, ) -> Result, Range)>>, io::Error> { if let Error::Dust { errors } = self { let mut reports = Vec::new(); @@ -241,3 +244,20 @@ impl Error { }; } } + +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Error::Dust { errors } => { + for error in errors { + writeln!(f, "{error:?}")?; + } + + Ok(()) + } + Error::Io(io_error) => { + write!(f, "{io_error}") + } + } + } +} diff --git a/dust-shell/src/main.rs b/dust-shell/src/main.rs index 4555440..33f7a73 100644 --- a/dust-shell/src/main.rs +++ b/dust-shell/src/main.rs @@ -11,7 +11,6 @@ use error::Error; use std::{ fs::read_to_string, io::{stderr, Write}, - path::Path, rc::Rc, }; @@ -47,7 +46,12 @@ fn main() { } else if let Some(command) = args.command { (command, Rc::new("input".to_string())) } else { - return run_shell(context); + match run_shell(context) { + Ok(_) => {} + Err(error) => eprintln!("{error}"), + } + + return; }; let eval_result = interpret(&source); @@ -60,12 +64,25 @@ fn main() { } Err(errors) => { let reports = Error::Dust { errors } - .build_reports(source_id.clone(), &source) + .build_reports(source_id.clone()) .unwrap(); for report in reports { report - .write_for_stdout(sources([(source_id.clone(), source.clone())]), stderr()) + .write_for_stdout( + sources([ + (source_id.clone(), source.as_str()), + ( + Rc::new("std/io.ds".to_string()), + include_str!("../../std/io.ds"), + ), + ( + Rc::new("std/thread.ds".to_string()), + include_str!("../../std/thread.ds"), + ), + ]), + stderr(), + ) .unwrap(); } } diff --git a/std/thread.ds b/std/thread.ds index 2ef663e..1b1f80a 100644 --- a/std/thread.ds +++ b/std/thread.ds @@ -1,5 +1,5 @@ thread = { sleep = (milliseconds: int) : none { - __SLEEP__() + __SLEEP__(milliseconds) } }