This commit is contained in:
2024-06-09 15:01:38 +02:00
parent d9dcce3ea3
commit c9a135f6d7
2 changed files with 45 additions and 18 deletions
+37 -12
View File
@@ -40,7 +40,6 @@ impl Transpiler {
indent: u32, indent: u32,
variables: HashMap<String, Variable>, variables: HashMap<String, Variable>,
) -> String { ) -> String {
let mut variables = variables;
let mut result = String::new(); let mut result = String::new();
if indent == 0 { if indent == 0 {
// result += "type int = i32;\n"; // result += "type int = i32;\n";
@@ -56,8 +55,9 @@ impl Transpiler {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
}; };
for ast in full_ast.parse() { let f_ast = full_ast.parse();
variables = full_ast.variables.clone(); let mut variables = full_ast.variables.clone();
for ast in f_ast {
if ast.ast_type == AstType::Other if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Identifier && ast.tokens[0].token_type == TokenType::Identifier
&& ast.tokens[0].value.contains(&self.peek) && ast.tokens[0].value.contains(&self.peek)
@@ -100,43 +100,59 @@ impl Transpiler {
if self.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
let mut vars: HashMap<String, Variable> = variables; let mut vars: HashMap<String, Variable> = variables.clone();
let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars); let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars);
result += format!( result += format!(
"fn {}({}) -> {} {}", "fn {}({}) -> {} {}",
ast.tokens[1].value, ast.tokens[1].value,
round, round,
ast.tokens[0].value, ast.tokens[0].value,
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars) self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
) )
.as_str(); .as_str();
for (name, var) in vars {
if let Some(v) = variables.get_mut(&ast.tokens[0].value) {
v.params.insert(
name,
Variable::new_var(LexerState { line: 0, column: 0 }, var.desc),
);
println!("{:?}", v);
}
}
} else if ast.ast_type == AstType::VoidFunctionDeceleration { } else if ast.ast_type == AstType::VoidFunctionDeceleration {
if self.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
let mut vars: HashMap<String, Variable> = variables; let mut vars: HashMap<String, Variable> = variables.clone();
let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars); let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars);
// panic!("{:?}", vars); // panic!("{:?}", vars);
result += format!( result += format!(
"fn {}({}) {}", "fn {}({}) {}",
ast.tokens[1].value, ast.tokens[1].value,
round, round,
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars) self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
) )
.as_str(); .as_str();
for (name, var) in vars {
if let Some(v) = variables.get_mut(&ast.tokens[0].value) {
v.params.insert(
name,
Variable::new_var(LexerState { line: 0, column: 0 }, var.desc),
);
println!("{:?}", v);
}
}
} else if ast.ast_type == AstType::StructDeceleration { } else if ast.ast_type == AstType::StructDeceleration {
if self.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
let mut vars: HashMap<String, Variable> = variables.clone();
let round = self.transpile_round(ast.tokens[1].value.clone(), &mut vars);
result += format!( result += format!(
"struct {} {} {}", "struct {} {} {}",
ast.tokens[0].value, ast.tokens[0].value,
"{\n", "{\n",
self.transpile_round( round.trim_end()
ast.tokens[1].value.clone(),
&mut variables.clone()
)
.trim_end()
) )
.replace( .replace(
"\n", "\n",
@@ -145,6 +161,15 @@ impl Transpiler {
) )
.as_str(); .as_str();
result += "\n}\n"; result += "\n}\n";
for (name, var) in vars {
if let Some(v) = variables.get_mut(&ast.tokens[0].value) {
v.params.insert(
name,
Variable::new_var(LexerState { line: 0, column: 0 }, var.desc),
);
println!("{:?}", v);
}
}
} else if ast.ast_type == AstType::VariableDeceleration { } else if ast.ast_type == AstType::VariableDeceleration {
if self.clone().auto_mut { if self.clone().auto_mut {
result += result +=
+8 -6
View File
@@ -1,3 +1,5 @@
use std::collections::HashMap;
use crate::lexer::LexerState; use crate::lexer::LexerState;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
@@ -14,7 +16,7 @@ pub struct Variable {
pub vtype: VariableType, pub vtype: VariableType,
pub desc: String, pub desc: String,
pub state: LexerState, pub state: LexerState,
pub params: Vec<[String; 2]>, pub params: HashMap<String, Variable>,
} }
impl Variable { impl Variable {
@@ -23,7 +25,7 @@ impl Variable {
vtype: VariableType::Var, vtype: VariableType::Var,
desc, desc,
state, state,
params: vec![], params: HashMap::new(),
} }
} }
@@ -32,7 +34,7 @@ impl Variable {
vtype: VariableType::Namespace, vtype: VariableType::Namespace,
desc, desc,
state, state,
params: vec![], params: HashMap::new(),
} }
} }
@@ -41,7 +43,7 @@ impl Variable {
vtype: VariableType::Struct, vtype: VariableType::Struct,
desc, desc,
state, state,
params: vec![], params: HashMap::new(),
} }
} }
@@ -50,7 +52,7 @@ impl Variable {
vtype: VariableType::Keyword, vtype: VariableType::Keyword,
desc, desc,
state, state,
params: vec![], params: HashMap::new(),
} }
} }
@@ -59,7 +61,7 @@ impl Variable {
vtype: VariableType::Func, vtype: VariableType::Func,
desc, desc,
state, state,
params: vec![], params: HashMap::new(),
} }
} }
} }