99 lines
2.2 KiB
Rust
99 lines
2.2 KiB
Rust
#![allow(unused)]
|
|
#![allow(dead_code)]
|
|
|
|
use std::env;
|
|
use std::fs::File;
|
|
use std::fs;
|
|
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,
|
|
_type: String,
|
|
scope: String,
|
|
is_const: bool,
|
|
}
|
|
fn build_sym_table(stream: &Vec::<&str>) -> Vec<SymbolRow> {
|
|
let sym_table: Vec<SymbolRow> = vec![];
|
|
|
|
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)
|
|
.init()
|
|
.expect("Failed to init the logger");
|
|
|
|
let args: Vec<String> = 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"[ \t]+").unwrap();
|
|
let tokens: Vec<&str> = re.split(&content).filter(|s| !s.is_empty()).collect();
|
|
build_sym_table(&tokens);
|
|
Ok(())
|
|
}
|