diff --git a/src/lexer.rs b/src/lexer.rs index 3729a2d..4ed6894 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -172,7 +172,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { '{' => { is_match = true; code = code.strip_prefix("{").unwrap_or(code); - inner_string += "{"; if svl == 0 { bracket_vec.push('{'); } @@ -180,8 +179,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { '}' => { is_match = true; code = code.strip_prefix("}").unwrap_or(code); - println!("{code} 585"); - inner_string += "}"; if bracket_vec[svl-1] == '{' { bracket_vec.pop(); tokens.push(Token { @@ -197,7 +194,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { '(' => { is_match = true; code = code.strip_prefix("(").unwrap_or(code); - inner_string += "("; if svl == 0 { bracket_vec.push('('); } @@ -205,7 +201,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { ')' => { is_match = true; code = code.strip_prefix(")").unwrap_or(code); - inner_string += ")"; if bracket_vec[svl-1] == '(' { bracket_vec.pop(); tokens.push(Token { @@ -221,7 +216,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { '[' => { is_match = true; code = code.strip_prefix("[").unwrap_or(code); - inner_string += "["; if svl == 0 { bracket_vec.push('['); @@ -230,7 +224,6 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Vec { ']' => { is_match = true; code = code.strip_prefix("]").unwrap_or(code); - inner_string += "]"; if bracket_vec[svl-1] == '[' { bracket_vec.pop(); tokens.push(Token { diff --git a/src/transpiler.rs b/src/transpiler.rs index f2b70ee..e45c754 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -1,32 +1,13 @@ use crate::lexer::{lex, TokenType}; use crate::parser::{Parser, AstType}; -pub fn auto_strip(input: String) -> String { - match input.chars().next().unwrap() { - '(' => { - return input.strip_prefix("(").unwrap().strip_suffix(")").unwrap().to_string() - } - '{' => { - return input.strip_prefix("{").unwrap().strip_suffix("}").unwrap().to_string() - } - _ => { - return input - } - } -} - pub fn transpile(input: String, indent: u32) -> String { - let mut input = input; - if indent > 0 { - input = auto_strip(input); - } let mut result = String::new(); let tokens = lex(input.as_str(), false); println!("\n\n\n\n"); let mut full_ast = Parser::new(tokens.clone()); while full_ast.tokens.len() > full_ast.index as usize { let ast = full_ast.next(); - println!("{}", ast); if ast.ast_type == AstType::FunctionDeceleration { result += format!("fn {}{} -> {} {}", ast.tokens[1].value, ast.tokens[2].value, ast.tokens[0].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str(); } else if ast.ast_type == AstType::VoidFunctionDeceleration { @@ -50,7 +31,28 @@ pub fn transpile(input: String, indent: u32) -> String { } result += ")"; } else { - result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); + result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); + } + } else if ast.ast_type == AstType::MutVariableDeceleration { + let _o = String::new(); + let mut oy = ast.tokens.clone(); + oy.remove(0); + if oy.len() > 1 { + result += "("; + for t in &oy { + result+=t.value.as_str() + } + result += "): ("; + for t in oy { + if t.value == "," { + result+=", " + } else { + result+=ast.tokens[0].value.as_str() + } + } + result += ")"; + } else { + result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); } } else { if ast.tokens[0].token_type==TokenType::Newline { @@ -60,6 +62,7 @@ pub fn transpile(input: String, indent: u32) -> String { result += ast.tokens[0].value.as_str() } } + } result = result.trim_end().to_string(); if indent > 0 {