Skip to content

Commit

Permalink
added error macro + added colors to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Miezekatze64 committed Aug 29, 2022
1 parent c89bd35 commit 2b109ff
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ enum Target {
Wasm,
}

const COLOR_RED: &str = "\x1b[31m";
const COLOR_GREEN: &str = "\x1b[32m";
const COLOR_YELLOW: &str = "\x1b[33m";
const COLOR_RESET: &str = "\x1b[0m";
pub const COLOR_RED: &str = "\x1b[31m";
pub const COLOR_GREEN: &str = "\x1b[32m";
pub const COLOR_YELLOW: &str = "\x1b[33m";
pub const COLOR_RESET: &str = "\x1b[0m";

/// Returns the usage of the compiler as a string
fn usage(prog_name: String) -> String {
Expand Down Expand Up @@ -175,7 +175,7 @@ fn main() {
if let Err(ref e) = a {
for a in e {
let (t, v) = a;
eprintln!("{}: {}", t, v);
eprintln!("{COLOR_RED}{}: {COLOR_RESET}{}", t, v);
}
error = true;
}
Expand All @@ -200,7 +200,7 @@ fn main() {
Err((e, a, g)) => {
for a in e.iter() {
let (t, v) = a;
eprintln!("{}: {}", t, v);
eprintln!("{COLOR_RED}{}: {COLOR_RESET}{}", t, v);
};
if e.into_iter().any(|(lvl, _)| lvl == util::ErrorLevel::Err) {
checked = false;
Expand Down
58 changes: 43 additions & 15 deletions src/preprocessor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::{COLOR_RED, COLOR_GREEN, COLOR_RESET};
use std::process::exit;

fn pos_to_line_char(source: Vec<char>, pos: usize) -> (usize, usize) {
assert!(pos < source.len());
let mut p: usize = 0;
Expand All @@ -16,6 +19,29 @@ fn pos_to_line_char(source: Vec<char>, pos: usize) -> (usize, usize) {
(line, ch)
}

macro_rules! parse_str {
($file:expr, $i:expr, $dir_start:expr, $filename:expr) => {
| | -> Option<String> {
while $file[$i] == ' ' || $file[$i] == '\t' { $i += 1; }
if $file[$i] != '"' {
let (l, c) = pos_to_line_char($file.clone(), $dir_start);
eprintln!("{file}:{line}:{ch}: Invalid character `{}`, `\"` expected",
$file[$i],
file = $filename,
line = l+1,
ch = c+1,
);
return None;
}
$i += 1;
let str_start = $i;
while $file[$i] != '"' { $i += 1; }
let string = $file.iter().skip(str_start).take($i - str_start).collect::<String>();
Some(string)
}()
};
}

pub fn preprocess(file: Vec<char>, filename: String) -> (Vec<char>, Vec<String>, Vec<String>) {
let mut i : usize = 0;
let mut to_skip : Vec<(usize, usize)> = vec![];
Expand Down Expand Up @@ -57,24 +83,26 @@ pub fn preprocess(file: Vec<char>, filename: String) -> (Vec<char>, Vec<String>,
match directive.as_str() {
"link"|"link_lib" => {
// expect string
while file[i] == ' ' || file[i] == '\t' { i += 1; }
if file[i] != '"' {
let (l, c) = pos_to_line_char(file.clone(), dir_start);
eprintln!("{file}:{line}:{ch}: Invalid character `{}`, `\"` expected",
file[i],
file = filename,
line = l+1,
ch = c+1,
);
continue;
}
i += 1;
let str_start = i;
while file[i] != '"' { i += 1; }
let file = file.iter().skip(str_start).take(i - str_start).collect::<String>();
let file = match parse_str!(file, i, dir_start, filename) {
Some(a) => a,
None => continue,
};

(if directive == *"link" {&mut links} else {&mut links_libs}).push(file);
},
"error" => {
let msg = match parse_str!(file, i, dir_start, filename) {
Some(a) => a,
None => continue,
};
let (l, c) = pos_to_line_char(file.clone(), dir_start);
eprintln!("{COLOR_RED}error: {COLOR_GREEN}{file}:{line}:{ch}: {COLOR_RESET}{msg}",
file = filename,
line = l+1,
ch = c+1,
);
exit(1);
},
_ => {
let (l, c) = pos_to_line_char(file.clone(), dir_start);
eprintln!("{file}:{line}:{ch}: Invalid preprocesser \
Expand Down
4 changes: 3 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ macro_rules! error_str {
($lexer:expr, $pos:expr) => {
| | -> String {
let (l, c) = $lexer.pos_to_line_char($pos);
return format!("{file}:{line}:{ch}: ",
return format!("{COLOR_GREEN}{file}:{line}:{ch}: {COLOR_RESET}",
COLOR_GREEN = "\x1b[32m",
COLOR_RESET = "\x1b[0m",
file = $lexer.filename,
line = l+1,
ch = c+1,
Expand Down
2 changes: 1 addition & 1 deletion swl/main.swl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// include "std.swl";
include "std.swl";
func main() -> int {
<- 42;
}
1 change: 1 addition & 0 deletions swl/std.swl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* SWL STANDARD LIBRARY */

#error "TODO"
include "linux.swl";

// used intrinsics
Expand Down
Binary file modified swlc
Binary file not shown.

0 comments on commit 2b109ff

Please sign in to comment.