From 0370c48eeec33b95ab6095f0a10b79e305d5a789 Mon Sep 17 00:00:00 2001 From: veradec Date: Tue, 1 Sep 2026 21:08:01 +0530 Subject: [PATCH] feat: regex for keywords, expect() to function as a parser --- examples/print._tmp.sr | 25 +++++++++++++++++++ examples/print.sr | 4 ++- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 examples/print._tmp.sr diff --git a/examples/print._tmp.sr b/examples/print._tmp.sr new file mode 100644 index 0000000..f8aeb4d --- /dev/null +++ b/examples/print._tmp.sr @@ -0,0 +1,25 @@ +// import somefoo +//thisissomefoo +main() { + uint1 y = 1; + uint8 x = 0; + if x > 0 { + print("Hello World, x is zero"); + } else { + print("Hello World, x is %", x); + } + + while x < 10 { + x++; + } + print("x is % now", x); + + string file_path = "../examples/print.sr"; + fs.read(file_path); + + + uint8[] x = [1,2,3]; + x.push(4); + x.push(1); + x.push(2); +} \ No newline at end of file diff --git a/examples/print.sr b/examples/print.sr index 2c7cb4b..67d8a06 100644 --- a/examples/print.sr +++ b/examples/print.sr @@ -1,5 +1,7 @@ +use foo; +// comment main() { - uint32 y = 1; + uint y = 1; uint8 x = 0; if x > 0 { print("Hello World, x is zero"); diff --git a/src/main.rs b/src/main.rs index f0d022f..55c4077 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use std::io::{BufRead, BufReader}; use log::{debug, error, info, trace, warn}; use plume_log::{PlumeBuilder, success}; use regex::Regex; +use std::io::BufWriter; +use std::io::Write; struct SymbolRow { sym: String, @@ -18,16 +20,56 @@ struct SymbolRow { fn build_sym_table(stream: &Vec::<&str>) -> Vec { let sym_table: Vec = vec![]; - let rx_func = Regex::new(r"int[0-9]+|uint[0-9]+|string|char").unwrap(); - for token in stream { - if rx_func.is_match(token){ - print!("{token} "); + let is_keyword_regex = Regex::new(r"bool|int[0-9]*|uint[0-9]*|string|char").unwrap(); + + + + for (index, token) in stream.iter().enumerate() { + if expect(stream, 0, "use") { + println!("found"); } + // Regex match for keyword + if is_keyword_regex.is_match(token){ + if token == &"uint" { + print!("uint16+"); + continue; + } + print!("{token}+"); + continue; + } + + print!("{token}+"); + } return sym_table; } + +fn expect(stream: &Vec::<&str>, index: usize, to_be_expect:&str) -> bool { + let found: Option<&&str> = stream.get(index); + let Some(s) = found else { todo!()}; + if *s == to_be_expect { + return true; + } + + return false; +} + + +fn expect(stream: &Vec::<&str>, index: usize, expect_vector: &Vec::<&str>) -> bool { + let found: Option<&&str> = stream.get(index); + let Some(s) = found else { todo!()}; + for expect in expect_vector { + if &*s == expect{ + return true; + } + } + + return false; +} + + fn main() -> std::io::Result<()>{ PlumeBuilder::new() .with_level(log::LevelFilter::Trace) @@ -37,15 +79,19 @@ fn main() -> std::io::Result<()>{ let args: Vec = env::args().collect(); let file_path = &args[1]; let extension = &file_path[file_path.len()-2..file_path.len()]; + // let new_file_name = format!("{}_tmp.sr", &file_path[..file_path.len() - 2]); + if extension != "sr" { error!("File is not in .sr"); std::process::exit(-1); } info!("File: {}", file_path); + // info!("File: {} | Copied To: {}", file_path, new_file_name); + let content = fs::read_to_string(file_path) .expect("Should've read the file"); - let re = Regex::new(r"[ \n\t]+").unwrap(); + let re = Regex::new(r"[ \t]+").unwrap(); let tokens: Vec<&str> = re.split(&content).filter(|s| !s.is_empty()).collect(); build_sym_table(&tokens); Ok(())