1
0

Improve errors and built-ins

This commit is contained in:
Jeff 2024-03-23 19:12:18 -04:00
parent 13c95dd12f
commit b392a4c7aa
7 changed files with 56 additions and 24 deletions

View File

@ -45,7 +45,7 @@ impl BuiltInFunction {
} }
} }
pub fn call(&self, arguments: Vec<Value>, context: &Context) -> Result<Action, RuntimeError> { pub fn call(&self, arguments: Vec<Value>, _context: &Context) -> Result<Action, RuntimeError> {
match self { match self {
// "INT_RANDOM_RANGE" => { // "INT_RANDOM_RANGE" => {
// let range = arguments.get(0).unwrap(); // let range = arguments.get(0).unwrap();
@ -77,9 +77,6 @@ impl BuiltInFunction {
Ok(Action::None) Ok(Action::None)
} }
_ => {
todo!()
}
} }
} }
} }

View File

@ -14,6 +14,9 @@ pub fn std_context() -> Context {
let mut interpreter = Interpreter::new(context.clone()); let mut interpreter = Interpreter::new(context.clone());
interpreter.run(include_str!("../../std/io.ds")).unwrap(); interpreter.run(include_str!("../../std/io.ds")).unwrap();
interpreter
.run(include_str!("../../std/thread.ds"))
.unwrap();
context context
} }

View File

@ -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 chumsky::{prelude::Rich, span::Span};
use crate::{ use crate::{

View File

@ -1,10 +1,4 @@
use std::{ use std::{borrow::Cow, io::stderr, path::PathBuf, process::Command, rc::Rc};
borrow::Cow,
io::{stderr, Write},
path::PathBuf,
process::Command,
rc::Rc,
};
use ariadne::sources; use ariadne::sources;
use dust_lang::{ use dust_lang::{
@ -20,7 +14,7 @@ use reedline::{
use crate::error::Error; 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 interpreter = Interpreter::new(context.clone());
let mut keybindings = default_emacs_keybindings(); let mut keybindings = default_emacs_keybindings();
@ -92,7 +86,7 @@ pub fn run_shell(context: Context) {
Err(errors) => { Err(errors) => {
let source_id = Rc::new("input".to_string()); let source_id = Rc::new("input".to_string());
let reports = Error::Dust { errors } let reports = Error::Dust { errors }
.build_reports(source_id.clone(), &buffer) .build_reports(source_id.clone())
.unwrap(); .unwrap();
for report in reports { for report in reports {
@ -114,6 +108,8 @@ pub fn run_shell(context: Context) {
} }
} }
} }
Ok(())
} }
struct StarshipPrompt { struct StarshipPrompt {

View File

@ -1,14 +1,12 @@
use ariadne::{Color, Fmt, Label, Report, ReportKind}; use ariadne::{Color, Fmt, Label, Report, ReportKind};
use clap::error;
use dust_lang::{ use dust_lang::{
abstract_tree::Type, abstract_tree::Type,
error::{Error as DustError, RuntimeError, TypeConflict, ValidationError}, error::{Error as DustError, RuntimeError, TypeConflict, ValidationError},
}; };
use std::{ use std::{
fmt::{self, Debug, Display, Formatter}, fmt::{self, Display, Formatter},
io, io,
ops::Range, ops::Range,
path::Path,
rc::Rc, rc::Rc,
}; };
@ -20,11 +18,16 @@ pub enum Error {
Io(io::Error), Io(io::Error),
} }
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}
impl Error { impl Error {
pub fn build_reports<'id>( pub fn build_reports<'id>(
self, self,
source_id: Rc<String>, source_id: Rc<String>,
source: &str,
) -> Result<Vec<Report<'id, (Rc<String>, Range<usize>)>>, io::Error> { ) -> Result<Vec<Report<'id, (Rc<String>, Range<usize>)>>, io::Error> {
if let Error::Dust { errors } = self { if let Error::Dust { errors } = self {
let mut reports = Vec::new(); 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}")
}
}
}
}

View File

@ -11,7 +11,6 @@ use error::Error;
use std::{ use std::{
fs::read_to_string, fs::read_to_string,
io::{stderr, Write}, io::{stderr, Write},
path::Path,
rc::Rc, rc::Rc,
}; };
@ -47,7 +46,12 @@ fn main() {
} else if let Some(command) = args.command { } else if let Some(command) = args.command {
(command, Rc::new("input".to_string())) (command, Rc::new("input".to_string()))
} else { } else {
return run_shell(context); match run_shell(context) {
Ok(_) => {}
Err(error) => eprintln!("{error}"),
}
return;
}; };
let eval_result = interpret(&source); let eval_result = interpret(&source);
@ -60,12 +64,25 @@ fn main() {
} }
Err(errors) => { Err(errors) => {
let reports = Error::Dust { errors } let reports = Error::Dust { errors }
.build_reports(source_id.clone(), &source) .build_reports(source_id.clone())
.unwrap(); .unwrap();
for report in reports { for report in reports {
report 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(); .unwrap();
} }
} }

View File

@ -1,5 +1,5 @@
thread = { thread = {
sleep = (milliseconds: int) : none { sleep = (milliseconds: int) : none {
__SLEEP__() __SLEEP__(milliseconds)
} }
} }