Skip to content

Commit cc67861

Browse files
committed
feat: better errors
1 parent 4ff2f7c commit cc67861

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

stack-control/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ unicode-segmentation = "1.10.1"
99
indoc = "2"
1010
strum = "0.27"
1111
strum_macros = "0.27"
12+
thiserror = "2.0.12"

stack-control/src/bytecode/commands.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::sync::Arc;
1+
use std::{error, sync::Arc};
2+
3+
use thiserror::Error;
24

35
use crate::runtime::stack::Stack;
46

@@ -8,13 +10,18 @@ pub mod math;
810
pub mod iters;
911
pub mod conditionals;
1012

11-
#[derive(strum_macros::IntoStaticStr)]
13+
#[derive(Error, Debug)]
1214
pub enum RuntimeException {
15+
#[error("No elements ahead of stack")]
1316
NoElementsAheadOfStack,
17+
#[error("No elements on stack")]
1418
NoElementsOnStack,
19+
#[error("Wrong element type")]
1520
WrongElementType,
21+
#[error("Not implemented")]
1622
NotImplemented
1723
}
24+
1825
pub trait CommandExecutable {
1926
fn execute(&self, stack: &mut Stack) -> Result<(), RuntimeException>;
2027
fn to_string(&self) -> String;

stack-control/src/compiletime/compiler.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use indoc::indoc;
4+
use thiserror::Error;
45

56
use crate::{bytecode::commands::{core::{ListGeneratorCommand, StackPusherCommand}, CommandMeta, DescribedCommand}, runtime::value::Value};
67

@@ -13,27 +14,16 @@ pub struct Scope {
1314
stack_pusher_meta: Arc<CommandMeta>
1415
}
1516

17+
#[derive(Error, Debug)]
1618
pub enum CompilationException {
19+
#[error("Unexcpected End Token {0}")]
1720
UnexcpectedEndToken(String),
21+
#[error("Function token required")]
1822
FunctionTokenRequired,
23+
#[error("Command {0} not found")]
1924
CommandNotFound(String),
2025
}
2126

22-
// TODO: Try macro?
23-
24-
impl ToString for CompilationException {
25-
fn to_string(&self) -> String {
26-
match self {
27-
CompilationException::CommandNotFound(cmd) =>
28-
format!("Command '{cmd}' not found"),
29-
CompilationException::FunctionTokenRequired =>
30-
format!("Function token required"),
31-
CompilationException::UnexcpectedEndToken(t) =>
32-
format!("Unexcpected END token: {t}")
33-
}
34-
}
35-
}
36-
3727
impl Scope {
3828
pub fn new() -> Self {
3929
Scope {
@@ -90,8 +80,6 @@ impl Scope {
9080
&'a self,
9181
token: CommandToken,
9282
tokens: &mut impl Iterator<Item = Token>) -> Result<Arc<DescribedCommand>, CompilationException> {
93-
94-
9583
Ok(match token {
9684
CommandToken::Number(num) => {
9785
Arc::new(DescribedCommand {

0 commit comments

Comments
 (0)