struct var defs

This commit is contained in:
2024-05-16 18:07:48 +02:00
parent 9dc8712364
commit a5e23db625
2 changed files with 38 additions and 5 deletions
+11
View File
@@ -7,6 +7,8 @@ use once_cell::sync::Lazy;
pub enum AstType { pub enum AstType {
FunctionDeceleration, FunctionDeceleration,
StructDeceleration, StructDeceleration,
StructCall,
StructVar,
VoidFunctionDeceleration, VoidFunctionDeceleration,
VariableDeceleration, VariableDeceleration,
PointerDeceleration, PointerDeceleration,
@@ -98,6 +100,15 @@ impl Parser {
ast_res.ast_type = AstType::FunctionDeceleration; ast_res.ast_type = AstType::FunctionDeceleration;
} }
self.index += 3; self.index += 3;
} else if self.tokens.len()-index > 1 && self.tokens[index+1].token_type==TokenType::Curly {
ast_res.tokens.push(self.tokens[index+1].clone());
ast_res.ast_type = AstType::StructCall;
self.index += 1;
} else if self.tokens.len()-index > 2 && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Curly {
ast_res.tokens.push(self.tokens[index+1].clone());
ast_res.tokens.push(self.tokens[index+2].clone());
ast_res.ast_type = AstType::StructVar;
self.index += 2;
} else if self.tokens.len()-index > 1 { } else if self.tokens.len()-index > 1 {
if self.tokens[index+1].token_type==TokenType::Identifier { if self.tokens[index+1].token_type==TokenType::Identifier {
ast_res.tokens.push(self.tokens[index+1].clone()); ast_res.tokens.push(self.tokens[index+1].clone());
+26 -4
View File
@@ -1,14 +1,16 @@
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser}; use crate::parser::{Ast, AstType, Parser};
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone)]
pub struct Options { pub struct Options {
auto_mut: bool auto_mut: bool,
auto_macro: bool,
macros: Vec<String>
} }
impl Default for Options { impl Default for Options {
fn default() -> Options { fn default() -> Options {
Options { auto_mut: true } Options { auto_mut: true, auto_macro: true, macros: vec![String::from("println")] }
} }
} }
@@ -47,7 +49,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
}, },
options options.clone()
) )
) )
.as_str(); .as_str();
@@ -129,6 +131,13 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
// } // }
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
result+="."; result+=".";
} else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str();
result += " {";
result += ast.tokens[1].value.as_str();
result += "}";
} else if ast.ast_type == AstType::StructVar {
result += format!("let mut {}: {} = {} {}{}{}", ast.tokens[1].value.as_str(), ast.tokens[0].value.as_str(), ast.tokens[0].value.as_str(), "{", ast.tokens[2].value.as_str(), "}").as_str();
} // flp } // flp
else { else {
if ast.tokens[0].token_type == TokenType::Newline { if ast.tokens[0].token_type == TokenType::Newline {
@@ -152,6 +161,9 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
} }
} }
result += ast.tokens[0].value.as_str(); 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; last_ast = ast;
@@ -213,6 +225,11 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
line: ast.tokens[0].line, line: ast.tokens[0].line,
column: ast.tokens[0].column column: ast.tokens[0].column
}).as_str(); }).as_str();
} else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str();
result += " {";
result += ast.tokens[1].value.as_str();
result += "}";
} // flp } // flp
else { else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
@@ -272,6 +289,11 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
line: ast.tokens[0].line, line: ast.tokens[0].line,
column: ast.tokens[0].column column: ast.tokens[0].column
}).as_str(); }).as_str();
} else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str();
result += " {";
result += ast.tokens[1].value.as_str();
result += "}";
} // flp } // flp
else { else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();