Compare commits

..

No commits in common. "52bb445f90fb8e7675a8180f1b3987eb86add4ec" and "2830a1befdad3d65b70b6d64070a49d17fddb933" have entirely different histories.

5 changed files with 10 additions and 73 deletions

View File

@ -1,7 +1,6 @@
mod compile; mod compile;
mod empty; mod empty;
mod help; mod help;
mod repl;
mod types; mod types;
use types::CommandType; use types::CommandType;

View File

@ -1,4 +0,0 @@
pub fn repl_command(_arguments: Vec<String>) {
println!("{}", super::get_version());
let _ = crate::repl::run();
}

View File

@ -16,7 +16,6 @@ impl CommandType {
match self { match self {
CommandType::Help => super::help::help_command(options), CommandType::Help => super::help::help_command(options),
CommandType::Compile => super::compile::compile_command(options), CommandType::Compile => super::compile::compile_command(options),
CommandType::Repl => super::repl::repl_command(options),
CommandType::None => super::empty::empty_command(options), CommandType::None => super::empty::empty_command(options),
_ => { _ => {
println!("Not implemented yet! {:?} {:?}", self, options); println!("Not implemented yet! {:?} {:?}", self, options);

View File

@ -4,14 +4,12 @@ use std::collections::VecDeque;
impl PrintableError for LexError { impl PrintableError for LexError {
// TODO: Count and show line number // TODO: Count and show line number
fn get_error_str(&self, chars: &Vec<char>) -> String { fn get_error_str(&self, chars: &Vec<char>) -> String {
let line_number = get_line_number(chars, self.position);
let (erroneous_code, back_count) = get_line(chars, self.position); let (erroneous_code, back_count) = get_line(chars, self.position);
let whitespace = " ".repeat(back_count + line_number.to_string().len() + 1); let whitespace = vec![' '; back_count].iter().collect::<String>();
format!( format!(
"\n{}|{}\n{}^\n\n{}{}\n{}", "\n{}\n{}^\n\n{}{}\n{}",
line_number,
erroneous_code, erroneous_code,
whitespace, whitespace,
"Invalid character at pos ", "Invalid character at pos ",
@ -71,22 +69,6 @@ fn get_line(chars: &Vec<char>, pos: usize) -> (String, usize) {
(result_chars.iter().collect::<String>(), pos - before_pos) (result_chars.iter().collect::<String>(), pos - before_pos)
} }
fn get_line_number(chars: &Vec<char>, target_pos: usize) -> usize {
let mut count = 1;
for (pos, char) in chars.iter().enumerate() {
if pos >= target_pos {
break;
}
if *char == '\n' {
count += 1;
}
}
count
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -103,8 +85,9 @@ mod tests {
let chars: Vec<char> = input.chars().into_iter().collect(); let chars: Vec<char> = input.chars().into_iter().collect();
let err_str = err_data.get_error_str(&chars); let err_str = err_data.get_error_str(&chars);
// TODO: check for line number
let expected_str = format!( let expected_str = format!(
"\n1|{}\n {}^\n\nInvalid character at pos 9\n{}", "\n{}\n{}^\n\nInvalid character at pos 9\n{}",
"val name' = 20", " ", "Unrecognized character `'` (escaped: `\\'`)" "val name' = 20", " ", "Unrecognized character `'` (escaped: `\\'`)"
); );
@ -131,22 +114,4 @@ mod tests {
assert_eq!("val binding = 322", result); assert_eq!("val binding = 322", result);
assert_eq!(6, back_count); assert_eq!(6, back_count);
} }
#[test]
fn should_get_line_number() {
let input = String::from("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten");
let chars: Vec<char> = input.chars().into_iter().collect();
let line_number = get_line_number(&chars, 11);
assert_eq!(3, line_number);
let line_number = get_line_number(&chars, 0);
assert_eq!(1, line_number);
let line_number = get_line_number(&chars, 3);
assert_eq!(1, line_number);
let line_number = get_line_number(&chars, 15);
assert_eq!(4, line_number);
}
} }

View File

@ -1,4 +1,3 @@
use colored::*;
use std::{fs, path::Path}; use std::{fs, path::Path};
use crate::lexic::token::Token; use crate::lexic::token::Token;
@ -8,49 +7,28 @@ pub fn compile_file(input: &String) {
let input_path = Path::new(input); let input_path = Path::new(input);
if !input_path.is_file() { if !input_path.is_file() {
println!( panic!("Input path is not a valid file")
"{}: {} {}",
"error".on_red(),
"Input path is not a valid file:".red(),
input
);
return;
} }
let bytes = fs::read(input_path).expect("INPUT_PATH should be valid"); let bytes = fs::read(input_path).expect("INPUT_PATH should be valid");
let contents = String::from_utf8(bytes).expect("INPUT_PATH's encoding MUST be UTF-8");
let contents = match String::from_utf8(bytes) { let out_code = compile(&contents);
Ok(str) => str,
Err(_) => {
println!("{}: Input file contains invalid UTF-8", "error".on_red());
return;
}
};
let Some(out_code) = compile(&contents) else {
return;
};
let mut output_path = Path::new(input).canonicalize().unwrap(); let mut output_path = Path::new(input).canonicalize().unwrap();
output_path.set_extension("php"); output_path.set_extension("php");
fs::write(output_path, out_code).expect("Error writing to output path"); fs::write(output_path, out_code).expect("Error writing to output path");
} }
/// Executes Lexical analysis, handles errors and calls build_ast for the next phase /// Executes Lexical analysis, handles errors and calls build_ast for the next phase
fn compile(input: &String) -> Option<String> { fn compile(input: &String) -> String {
let tokens = lexic::get_tokens(input); let tokens = lexic::get_tokens(input);
match tokens { match tokens {
Ok(tokens) => Some(build_ast(input, tokens)), Ok(tokens) => build_ast(input, tokens),
Err(error) => { Err(error) => {
let chars: Vec<char> = input.chars().into_iter().collect(); let chars: Vec<char> = input.chars().into_iter().collect();
println!( panic!("{}", error.get_error_str(&chars))
"{}:\n{}",
"syntax error".on_red(),
error.get_error_str(&chars)
);
None
} }
} }
} }