feat: regex for keywords, expect() to function as a parser

This commit is contained in:
2026-09-01 21:08:01 +05:30
parent 79a92f1d2d
commit 0370c48eee
3 changed files with 79 additions and 6 deletions
+25
View File
@@ -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);
}
+3 -1
View File
@@ -1,5 +1,7 @@
use foo;
// comment
main() { main() {
uint32 y = 1; uint y = 1;
uint8 x = 0; uint8 x = 0;
if x > 0 { if x > 0 {
print("Hello World, x is zero"); print("Hello World, x is zero");
+51 -5
View File
@@ -8,6 +8,8 @@ use std::io::{BufRead, BufReader};
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use plume_log::{PlumeBuilder, success}; use plume_log::{PlumeBuilder, success};
use regex::Regex; use regex::Regex;
use std::io::BufWriter;
use std::io::Write;
struct SymbolRow { struct SymbolRow {
sym: String, sym: String,
@@ -18,16 +20,56 @@ struct SymbolRow {
fn build_sym_table(stream: &Vec::<&str>) -> Vec<SymbolRow> { fn build_sym_table(stream: &Vec::<&str>) -> Vec<SymbolRow> {
let sym_table: Vec<SymbolRow> = vec![]; let sym_table: Vec<SymbolRow> = vec![];
let rx_func = Regex::new(r"int[0-9]+|uint[0-9]+|string|char").unwrap(); let is_keyword_regex = Regex::new(r"bool|int[0-9]*|uint[0-9]*|string|char").unwrap();
for token in stream {
if rx_func.is_match(token){
print!("{token} ");
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; 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<()>{ fn main() -> std::io::Result<()>{
PlumeBuilder::new() PlumeBuilder::new()
.with_level(log::LevelFilter::Trace) .with_level(log::LevelFilter::Trace)
@@ -37,15 +79,19 @@ fn main() -> std::io::Result<()>{
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let file_path = &args[1]; let file_path = &args[1];
let extension = &file_path[file_path.len()-2..file_path.len()]; 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" { if extension != "sr" {
error!("File is not in .sr"); error!("File is not in .sr");
std::process::exit(-1); std::process::exit(-1);
} }
info!("File: {}", file_path); info!("File: {}", file_path);
// info!("File: {} | Copied To: {}", file_path, new_file_name);
let content = fs::read_to_string(file_path) let content = fs::read_to_string(file_path)
.expect("Should've read the file"); .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(); let tokens: Vec<&str> = re.split(&content).filter(|s| !s.is_empty()).collect();
build_sym_table(&tokens); build_sym_table(&tokens);
Ok(()) Ok(())