diff --git a/src/lexer.rs b/src/lexer.rs index 0800940..cbd4a3d 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -108,7 +108,7 @@ const SYNTAX: [Node; 16] = [ }, Node { token_type: TokenType::Operator, - token_regex: Lazy::new(|| Regex::new(r"^[|\-|\+|\*|\=|\!|\&|\:]").unwrap()), + token_regex: Lazy::new(|| Regex::new(r"^[\&\&|\-|\+|\*|\=|\!|\&|\:]").unwrap()), }, Node { token_type: TokenType::Round, @@ -195,39 +195,6 @@ pub fn lex( } } } - "*" => { - brstr += "*"; - code = code.strip_prefix(fch.as_str()).expect(""); - if code.len() > 0 { - let sch = get_first_char(code); - if sch == "/" && brtp[brln - 1] == 5 { - code = code.strip_prefix(sch.as_str()).expect(""); - brstr += &sch; - brtp.pop(); - tokens.push(Token { - token_type: TokenType::Comment, - value: brstr.clone(), - column: br_state.column, - line: br_state.line, - }); - brstr = String::new(); - } else if brln == 0 { - tokens.push(Token { - token_type: TokenType::Operator, - value: "*".to_string(), - column: state.column, - line: state.line, - }); - } - } else if brln == 0 { - tokens.push(Token { - token_type: TokenType::Operator, - value: "*".to_string(), - column: state.column, - line: state.line, - }); - } - } "\"" => { brstr += "\""; code = code.strip_prefix(fch.as_str()).expect(""); diff --git a/src/main.rs b/src/main.rs index 3513b43..90a15a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,33 +24,34 @@ fn main() { let args = Args::parse(); let file_content = fs::read_to_string(&args.file) .expect("Error reading file"); - if Path::new("wyst_tmp").exists() { - fs::remove_dir_all("wyst_tmp").expect("err rm wyst_tmp"); + if Path::new("build").exists() { + fs::remove_dir_all("build").expect("err rm build"); } - fs::create_dir("wyst_tmp").expect("error making wyst_tmp"); + fs::create_dir("build").expect("error making build"); let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }, &mut Options::default()); match args.rust { Some(ref rust_file_name) => { // Write transpiled code to Rust file - compile::write_to_rust_file(&transpiled_code, rust_file_name) + compile::write_to_rust_file(&transpiled_code, ("build/".to_string()+rust_file_name).as_str()) .expect("Error writing to Rust file"); println!("Transpiled code written to {}", rust_file_name); } None => { // No Rust file specified, print transpiled code - println!("{}", transpiled_code); + if !args.compile.is_some() { + println!("{}", transpiled_code); + } } } match args.compile { Some(ref exe_name) => { - // Compile transpiled Rust code into executable - compile::write_to_rust_file(&transpiled_code, "wyst_tmp/temp.rs") + compile::write_to_rust_file(&transpiled_code, "build/temp.rs") .expect("Error writing to temporary Rust file"); - compile::compile_to_executable("wyst_tmp/temp.rs", exe_name) + compile::compile_to_executable("build/temp.rs", exe_name) .expect("Error compiling to executable"); - fs::remove_dir_all("wyst_tmp").expect("err rm wyst_tmp"); + fs::remove_dir_all("build").expect("err rm build"); } None => {} } diff --git a/src/parser.rs b/src/parser.rs index 18735a3..9615bbb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,6 +5,7 @@ use std::fmt; #[derive(Clone, Debug, PartialEq, Eq)] pub enum AstType { + Ref, FunctionDeceleration, StructDeceleration, StructCall, @@ -93,6 +94,12 @@ impl Parser { ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index + 2].clone()); self.index += 2; + } else if self.tokens.len() - index > 1 + && self.tokens[index].value == "&" + && self.tokens[index + 1].token_type == TokenType::Identifier { + ast_res.tokens.push(self.tokens[index + 1].clone()); + ast_res.ast_type = AstType::Ref; + self.index += 1; } else if self.tokens.len() - index > 2 && self.tokens[index].value == "struct" && self.tokens[index + 1].token_type == TokenType::Identifier @@ -188,8 +195,7 @@ impl Parser { } else if self.tokens.len() - index > 2 && self.tokens[index + 1].value == "*" && self.tokens[index + 2].token_type == TokenType::Identifier { - ast_res.tokens.push(self.tokens[index].clone()); - ast_res.tokens.push(self.tokens[index + 1].clone()); + ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.ast_type = AstType::PointerDeceleration; self.index += 2; } diff --git a/src/transpiler.rs b/src/transpiler.rs index 3facaff..cf259a1 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -51,7 +51,7 @@ pub fn transpile_mod(ast: Ast, options: &mut Options, s: &str) -> String { &mut Options::default(), ); fs::write( - ("wyst_tmp/".to_string() + modname.as_str()) + ".rs", + ("build/".to_string() + modname.as_str()) + ".rs", transpiled_code, ) .expect("Error writing file"); @@ -78,7 +78,6 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op }; while full_ast.tokens.len() > full_ast.index as usize { let ast = full_ast.next(); - println!("{ast}"); if last_ast.tokens.len() > 0 { let mut fl = 0; for t in &last_ast.tokens { @@ -334,6 +333,14 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op "}" ) .as_str(); + } else if ast.ast_type == AstType::PointerDeceleration { + if options.auto_mut { + result += + format!("let mut {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); + } else { + result += + format!("let {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); + } } // flp else { @@ -465,6 +472,9 @@ pub fn transpile_round(input: String, state: LexerState) -> String { result += " {"; result += ast.tokens[1].value.as_str(); result += "}"; + } else if ast.ast_type == AstType::Ref { + result += "&mut "; + result += ast.tokens[0].value.as_str(); } // flp else { @@ -520,9 +530,6 @@ pub fn transpile_square(input: String, state: LexerState) -> String { } else if ast.ast_type == AstType::MutVariableDeceleration { result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); - } else if ast.ast_type == AstType::PointerDeceleration { - result += - format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { result += format!( "({})",