bug fixes

This commit is contained in:
2024-05-25 13:12:09 +02:00
parent 4cbf46da53
commit 5fafd054b1
4 changed files with 31 additions and 50 deletions
+1 -34
View File
@@ -108,7 +108,7 @@ const SYNTAX: [Node; 16] = [
}, },
Node { Node {
token_type: TokenType::Operator, token_type: TokenType::Operator,
token_regex: Lazy::new(|| Regex::new(r"^[|\-|\+|\*|\=|\!|\&|\:]").unwrap()), token_regex: Lazy::new(|| Regex::new(r"^[\&\&|\-|\+|\*|\=|\!|\&|\:]").unwrap()),
}, },
Node { Node {
token_type: TokenType::Round, 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 += "\""; brstr += "\"";
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
+9 -8
View File
@@ -24,33 +24,34 @@ fn main() {
let args = Args::parse(); let args = Args::parse();
let file_content = fs::read_to_string(&args.file) let file_content = fs::read_to_string(&args.file)
.expect("Error reading file"); .expect("Error reading file");
if Path::new("wyst_tmp").exists() { if Path::new("build").exists() {
fs::remove_dir_all("wyst_tmp").expect("err rm wyst_tmp"); 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()); let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }, &mut Options::default());
match args.rust { match args.rust {
Some(ref rust_file_name) => { Some(ref rust_file_name) => {
// Write transpiled code to Rust file // 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"); .expect("Error writing to Rust file");
println!("Transpiled code written to {}", rust_file_name); println!("Transpiled code written to {}", rust_file_name);
} }
None => { None => {
// No Rust file specified, print transpiled code // No Rust file specified, print transpiled code
if !args.compile.is_some() {
println!("{}", transpiled_code); println!("{}", transpiled_code);
} }
} }
}
match args.compile { match args.compile {
Some(ref exe_name) => { Some(ref exe_name) => {
// Compile transpiled Rust code into executable compile::write_to_rust_file(&transpiled_code, "build/temp.rs")
compile::write_to_rust_file(&transpiled_code, "wyst_tmp/temp.rs")
.expect("Error writing to temporary Rust file"); .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"); .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 => {} None => {}
} }
+8 -2
View File
@@ -5,6 +5,7 @@ use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
Ref,
FunctionDeceleration, FunctionDeceleration,
StructDeceleration, StructDeceleration,
StructCall, StructCall,
@@ -93,6 +94,12 @@ impl Parser {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
self.index += 2; 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 } else if self.tokens.len() - index > 2
&& self.tokens[index].value == "struct" && self.tokens[index].value == "struct"
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
@@ -188,8 +195,7 @@ impl Parser {
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*" && self.tokens[index + 1].value == "*"
&& self.tokens[index + 2].token_type == TokenType::Identifier { && self.tokens[index + 2].token_type == TokenType::Identifier {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::PointerDeceleration; ast_res.ast_type = AstType::PointerDeceleration;
self.index += 2; self.index += 2;
} }
+12 -5
View File
@@ -51,7 +51,7 @@ pub fn transpile_mod(ast: Ast, options: &mut Options, s: &str) -> String {
&mut Options::default(), &mut Options::default(),
); );
fs::write( fs::write(
("wyst_tmp/".to_string() + modname.as_str()) + ".rs", ("build/".to_string() + modname.as_str()) + ".rs",
transpiled_code, transpiled_code,
) )
.expect("Error writing file"); .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 { while full_ast.tokens.len() > full_ast.index as usize {
let ast = full_ast.next(); let ast = full_ast.next();
println!("{ast}");
if last_ast.tokens.len() > 0 { if last_ast.tokens.len() > 0 {
let mut fl = 0; let mut fl = 0;
for t in &last_ast.tokens { for t in &last_ast.tokens {
@@ -334,6 +333,14 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
"}" "}"
) )
.as_str(); .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 // flp
else { else {
@@ -465,6 +472,9 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
result += " {"; result += " {";
result += ast.tokens[1].value.as_str(); result += ast.tokens[1].value.as_str();
result += "}"; result += "}";
} else if ast.ast_type == AstType::Ref {
result += "&mut ";
result += ast.tokens[0].value.as_str();
} }
// flp // flp
else { else {
@@ -520,9 +530,6 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += result +=
format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); 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 { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round {
result += format!( result += format!(
"({})", "({})",