From ae281d70d1aa67745174c0e96489d7be75e682bf Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Sat, 18 May 2024 17:43:39 +0200 Subject: [PATCH] added float and as --- src/lexer.rs | 10 +++++++++- src/parser.rs | 10 +--------- src/transpiler.rs | 40 +++++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index b1b669a..7c22217 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -72,7 +72,7 @@ const SYNTAX: [Node; 14] = [ }, Node { token_type: TokenType::Keyword, - token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use|cb|struct|impl|for|in)\b").unwrap()) + token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use|cb|struct|impl|for|in|as)\b").unwrap()) }, Node { token_type: TokenType::Identifier, @@ -386,6 +386,14 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result { + tokens.push(Token { + token_type: s.token_type, + value: "f32".to_string(), + line: state.line, + column: state.column + }); + } _ => { tokens.push(Token { token_type: s.token_type, diff --git a/src/parser.rs b/src/parser.rs index 76079c4..4bc02b1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -114,21 +114,13 @@ impl Parser { ast_res.tokens.push(self.tokens[index+1].clone()); ast_res.ast_type = AstType::VariableDeceleration; self.index += 1; - } else if self.tokens[index+2].token_type==TokenType::Identifier && self.tokens[index+1].token_type==TokenType::Angle { + } else if self.tokens.len()-index > 2 && self.tokens[index+2].token_type==TokenType::Identifier && self.tokens[index+1].token_type==TokenType::Angle { ast_res.tokens.push(self.tokens[index+2].clone()); ast_res.tokens[0].value += "<"; ast_res.tokens[0].value += self.tokens[index+1].value.as_str(); ast_res.tokens[0].value += ">"; ast_res.ast_type = AstType::VariableDeceleration; self.index += 2; - } else if self.tokens[index+1].value=="mut" && self.tokens[index+2].token_type==TokenType::Identifier { - ast_res.tokens.push(self.tokens[index+2].clone()); - ast_res.ast_type = AstType::MutVariableDeceleration; - self.index += 2; - } else if self.tokens[index+1].value=="*" && self.tokens[index+2].token_type==TokenType::Identifier { - 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 903f7a2..e550fb1 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -32,7 +32,19 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options 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 { fl+=t.value.len() } + // if ast.tokens[ast.tokens.len()-1].line > last_ast.tokens[last_ast.tokens.len()-1].line { + // // result += ("\n".to_string() + " ".repeat(((indent+1) as usize)*2).as_str()).repeat(ast.tokens[ast.tokens.len()-1].line - last_ast.tokens[last_ast.tokens.len()-1].line).as_str(); + // result += "\n".repeat(ast.tokens[ast.tokens.len()-1].line - last_ast.tokens[last_ast.tokens.len()-1].line).as_str(); + // } + if ast.tokens[ast.tokens.len()-1].column > last_ast.tokens[last_ast.tokens.len()-1].column+fl { + result += " ".repeat(ast.tokens[ast.tokens.len()-1].column - (last_ast.tokens[last_ast.tokens.len()-1].column+fl)).as_str(); + } + } + last_ast = Ast {ast_type: ast.ast_type.clone(), tokens: ast.tokens.clone()}; + if ast.ast_type == AstType::FunctionDeceleration { result += format!( "fn {}({}) -> {} {}", @@ -95,7 +107,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options } } else if ast.ast_type == AstType::MutVariableDeceleration { result += format!("let 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.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Round { result += format!( "({})", transpile_round(ast.tokens[0].value.clone(), LexerState { @@ -148,25 +160,23 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options result += ";\n"; result += " ".repeat((indent as usize) * 2).as_str(); } else { - if last_ast.tokens.len() > 0 && ( - ast.tokens[0].token_type == TokenType::Identifier || - ast.tokens[0].token_type == TokenType::Keyword || - ast.tokens[0].token_type == TokenType::Number - ) { - let ltkn = last_ast.tokens[last_ast.tokens.len()-1].token_type; - if ltkn == TokenType::Identifier || - ltkn == TokenType::Keyword || - ltkn == TokenType::Number { - result += " "; - } - } + // if last_ast.tokens.len() > 0 && ( + // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Identifier || + // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Keyword || + // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Number + // ) { + // let ltkn = last_ast.tokens[last_ast.tokens.len()-1].token_type; + // if ltkn == TokenType::Identifier || + // ltkn == TokenType::Keyword || + // ltkn == TokenType::Number { + // } + // } result += ast.tokens[0].value.as_str(); if options.auto_macro && options.macros.contains(&ast.tokens[0].value.as_str().to_string()) { result += "!"; } } } - last_ast = ast; } result = result.trim_end().to_string();