Skip to content

Commit

Permalink
feat: better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SpideyZac committed Jun 2, 2024
1 parent e8095ab commit d3abd8a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod ir;
pub mod target;
pub mod visit;
5 changes: 5 additions & 0 deletions src/compiler/visit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::parser::parser;

pub struct Visitor<'a> {
pub ast_tree: parser::ParserReturn<'a>,
}
59 changes: 50 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,76 @@
pub mod compiler;
pub mod lexer;
pub mod parser;
pub mod utils;

use compiler::target::Target;

use crate::compiler::ir::{IRFunction, IRFunctionEntry, IRStatement, IR};
use crate::compiler::target::vm::VM;
use crate::lexer::lexer as l;
use crate::lexer::tokens as t;
use crate::parser::parser as p;
use crate::utils::get_line;

fn main() {
let contents = "HAI 1.2,HOW IZ I sum ITZ NUMBER YR a ITZ NUMBER AN YR b ITZ NUMBER,FOUND YR SUM OF a AN b,IF U SAY SO,KTHXBYE";
let contents = "HAI 1.2\nHOW IZ I sum ITZ NUMBER YR a ITZ NOOB AN YR b ITZ NUMBER\nFOUND YR SUM OF a AN b\nIF U SAY SO\nKTHXBYE";
let lines = contents.split("\n").collect::<Vec<&str>>();

let mut l = l::Lexer::init(contents);
let tokens = l.get_tokens();

println!("{:?}\n\n", tokens);

if l::Lexer::has_errors(&tokens) {
println!("{:#?}\n\n", l::Lexer::get_first_error(&tokens).unwrap());
let error = l::Lexer::get_first_error(&tokens).unwrap();

let (line, count) = get_line(&lines, error.start);

match &error.token {
t::Token::Illegal(e) => {
println!("{}", lines[line]);
let arrow =
" ".repeat(error.start - count) + "^".repeat(error.end - error.start).as_str();
println!("{}", arrow);
println!(
"Error: {} at line {}, column {}:{}",
e,
line + 1,
error.start - count + 1,
error.end - count + 1
);
}
_ => {
panic!("Unexpected error token");
}
}

return;
}

let p = p::Parser::parse(tokens);
println!("{:?}\n\n", p.ast);

for error in p.errors.iter() {
println!("{:#?}", error);
}
if p.errors.len() > 0 {
println!("\n\n");
let reversed = p.errors.iter().rev().collect::<Vec<&p::ParserError>>();

for (i, error) in reversed.iter().enumerate() {
let (line, count) = get_line(&lines, error.token.start);

println!("{}", lines[line]);
let arrow = " ".repeat(error.token.start - count)
+ "^".repeat(error.token.end - error.token.start).as_str();
println!("{}", arrow);
println!(
"Error: {} at line {}, column {}:{}",
error.message,
line + 1,
error.token.start - count + 1,
error.token.end - count + 1
);

if i != reversed.len() - 1 {
println!("\nWhich was caused by:");
}
}

return;
}

Expand Down
13 changes: 13 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub fn get_line(lines: &Vec<&str>, start: usize) -> (usize, usize) {
let mut line = 0;
let mut count = 0;
for (i, l) in lines.iter().enumerate() {
if start - count < l.len() {
line = i;
break;
}
count += l.len() + 1;
}

(line, count)
}

0 comments on commit d3abd8a

Please sign in to comment.