I was writing code for my own programming language, and I keep running into an error where my code runs forever with no error message. Heres my code (and yes I know my style of importing is wierd, don't comment on that)
use crate::tokenz::TOKENS;
pub enum Operator {
Plus,
Minus,
Division,
Multiplication,
Lparanthesis,
Rparanthesis,
Float(f64),
Int(i64),
}
pub mod tokenz {
pub static mut TOKENS: Vec<crate::Operator> = vec![];
}
#[derive(Debug, Clone)]
pub struct Lexer {
pub text: Vec<char>,
pub pos: usize,
pub current_char: char,
}
impl Lexer {
pub fn advance(mut self) {
self.pos += 1;
self.current_char = self.text[self.pos];
}
pub fn make_number(&mut self) -> Operator {
const DIGIT: &str = "0123456789";
let mut num_str = String::new();
let mut dot_count = 0;
while DIGIT.find(self.current_char).is_some() || self.current_char == '.' {
if self.current_char == '.' {
if dot_count == 1 {
break;
}
dot_count += 1;
} else {
num_str.push(self.current_char);
self.clone().advance();
}
}
if dot_count == 0 {
return Operator::Int(num_str.parse::<i64>().unwrap())
} else {
return Operator::Float(num_str.parse::<f64>().unwrap())
}
}
pub fn make_tokens(&mut self) {
const DIGIT: &str = "0123456789";
while self.current_char != '\0' {
match self.current_char {
' ' => self.clone().advance(),
'+' => {
unsafe { TOKENS.push(Operator::Plus) };
self.clone().advance();
}
'-' => {
unsafe { TOKENS.push(Operator::Minus) };
self.clone().advance();
}
'*' => {
unsafe { TOKENS.push(Operator::Multiplication) };
self.clone().advance();
}
'/' => {
unsafe { TOKENS.push(Operator::Division) };
self.clone().advance();
}
'(' => {
unsafe { TOKENS.push(Operator::Lparanthesis) };
self.clone().advance();
}
')' => {
unsafe { TOKENS.push(Operator::Rparanthesis) };
self.clone().advance();
}
_ => {
if DIGIT.find(self.current_char).is_some() {
self.make_number();
} else {
self.clone().advance();
}
}
}
}
}
}
use std::{io};
// use std::{fs, io, io::Read};
fn main() {
/*
println!("welcome to loxable shell, please input file name.");
let mut filename = String::new();
io::stdin().read_line(&mut filename).expect("not a valid input. (please dont use emojis or etc)");
let filename = filename.trim();
let mut args = String::new();
let mut file = fs::File::open(filename).unwrap();
file.read_to_string(&mut args).unwrap();
dbg!(file);
let args = args.trim();
// println!("would you like to use untested/unstable version? (y/n) \n");
println!("{}",args);
if args.starts_with("loxabla beta/") {
println!("version type declared succesfully.");
} else if !args.starts_with("loxable beta/") {
panic!("current version of loxable is unavailable in this version, please upgrade your loxable version.");
}
*/
println!("welcome to loxable shell, alpha version.");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("not a valid ascii string");
let input = input.trim();
let input: Vec<char> = input.chars().collect();
let mut to_lex = Lexer { text: input, pos: 0, current_char: ' ' };
let to_lex2 = to_lex.make_tokens();
println!("{:#?}",to_lex2);
}