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 {
// "INT_RANDOM_RANGE" => {
// let range = arguments.get(0).unwrap();
@ -77,9 +77,6 @@ impl BuiltInFunction {
Ok(Action::None)
}
_ => {
todo!()
}
}
}
}

View File

@ -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
}

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 crate::{

View File

@ -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 {

View File

@ -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<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}
impl Error {
pub fn build_reports<'id>(
self,
source_id: Rc<String>,
source: &str,
) -> Result<Vec<Report<'id, (Rc<String>, Range<usize>)>>, 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}")
}
}
}
}

View File

@ -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();
}
}

View File

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