Skip to content

Commit

Permalink
Split warnings and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 17, 2024
1 parent bc7d25f commit 77bf4a7
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 88 deletions.
12 changes: 10 additions & 2 deletions crates/rue-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;

use clap::Parser;
use clvmr::{run_program, serde::node_to_bytes, Allocator, ChiaDialect, NodePtr};
use rue_compiler::compile;
use rue_compiler::{compile, DiagnosticKind};
use rue_parser::{line_col, parse, LineCol};

/// The Rue language compiler and toolchain.
Expand Down Expand Up @@ -35,7 +35,15 @@ fn main() {
let LineCol { line, col } = line_col(&source, error.span().start);
let line = line + 1;
let col = col + 1;
eprintln!("{} at {line}:{col}", error.info());

match error.kind() {
DiagnosticKind::Error(kind) => {
eprintln!("Error: {} at {line}:{col}", kind)
}
DiagnosticKind::Warning(kind) => {
eprintln!("Warning: {} at {line}:{col}", kind)
}
}
}
return;
}
Expand Down
44 changes: 25 additions & 19 deletions crates/rue-compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,48 @@ use thiserror::Error;
#[derive(Debug)]
pub struct Diagnostic {
kind: DiagnosticKind,
info: DiagnosticInfo,
span: Range<usize>,
}

impl Diagnostic {
pub fn new(kind: DiagnosticKind, info: DiagnosticInfo, span: Range<usize>) -> Self {
Self { kind, info, span }
pub fn new(kind: DiagnosticKind, span: Range<usize>) -> Self {
Self { kind, span }
}

pub fn kind(&self) -> DiagnosticKind {
self.kind
}

pub fn info(&self) -> &DiagnosticInfo {
&self.info
pub fn kind(&self) -> &DiagnosticKind {
&self.kind
}

pub fn span(&self) -> &Range<usize> {
&self.span
}

pub fn is_error(&self) -> bool {
matches!(self.kind, DiagnosticKind::Error(_))
}

pub fn is_warning(&self) -> bool {
matches!(self.kind, DiagnosticKind::Warning(_))
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DiagnosticKind {
Warning,
Error,
Warning(WarningKind),
Error(ErrorKind),
}

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
pub enum WarningKind {
#[error("redundant optional type")]
RedundantOptional,

#[error("redundant check against same type `{0}`")]
RedundantTypeGuard(String),
}

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
pub enum DiagnosticInfo {
pub enum ErrorKind {
#[error("missing `main` function")]
MissingMain,

Expand Down Expand Up @@ -116,9 +128,6 @@ pub enum DiagnosticInfo {
#[error("cannot check list against pair with types other than the list item type and the list itself")]
NonListPairTypeGuard,

#[error("redundant check against same type `{0}`")]
RedundantTypeGuard(String),

#[error("implicit return is not allowed in if statements, use an explicit return statement")]
ImplicitReturnInIf,

Expand All @@ -130,9 +139,6 @@ pub enum DiagnosticInfo {

#[error("cannot check equality on non-atom type `{0}`")]
NonAtomEquality(String),

#[error("redundant optional type")]
RedundantOptional,
}

/// Join a list of names into a string, wrapped in backticks.
Expand Down
9 changes: 2 additions & 7 deletions crates/rue-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ pub fn compile(allocator: &mut Allocator, root: Root, parsing_succeeded: bool) -

let Some(main_id) = db.scope_mut(scope_id).symbol("main") else {
diagnostics.push(Diagnostic::new(
DiagnosticKind::Error,
DiagnosticInfo::MissingMain,
DiagnosticKind::Error(ErrorKind::MissingMain),
0..0,
));

Expand All @@ -64,11 +63,7 @@ pub fn compile(allocator: &mut Allocator, root: Root, parsing_succeeded: bool) -
};
};

let node_ptr = if !diagnostics
.iter()
.any(|diagnostic| diagnostic.kind() == DiagnosticKind::Error)
&& parsing_succeeded
{
let node_ptr = if !diagnostics.iter().any(Diagnostic::is_error) && parsing_succeeded {
let mut optimizer = Optimizer::new(&mut db);
let lir_id = optimizer.opt_main(main_id);

Expand Down
Loading

0 comments on commit 77bf4a7

Please sign in to comment.