few fixes

This commit is contained in:
2024-06-07 14:29:27 +02:00
parent 5fd9ab86bc
commit e82dae5dfc
3 changed files with 73 additions and 57 deletions
+17 -20
View File
@@ -73,10 +73,7 @@ pub struct Parser {
pub fn new_vars() -> HashMap<String, Variable> {
HashMap::from([(
"void".to_string(),
Variable {
vtype: VariableType::Keyword,
state: LexerState { line: 0, column: 0 },
},
Variable::new_var(VariableType::Keyword, LexerState { line: 0, column: 0 }),
)])
}
@@ -182,13 +179,13 @@ impl Parser {
}
self.variables.insert(
self.tokens[index + 1].clone().value,
Variable {
vtype: VariableType::Func,
state: LexerState {
Variable::new_func(
VariableType::Func,
LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column,
},
},
),
);
self.index += 3;
} else if self.tokens.len() - index > 1
@@ -212,13 +209,13 @@ impl Parser {
self.index += 1;
self.variables.insert(
self.tokens[index + 1].clone().value,
Variable {
vtype: VariableType::Var,
state: LexerState {
Variable::new_var(
VariableType::Var,
LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column,
},
},
),
);
} else if self.tokens.len() - index > 2
&& self.tokens[index + 2].token_type == TokenType::Identifier
@@ -232,13 +229,13 @@ impl Parser {
self.index += 2;
self.variables.insert(
self.tokens[index + 1].clone().value,
Variable {
vtype: VariableType::Var,
state: LexerState {
Variable::new_var(
VariableType::Var,
LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column,
},
},
),
);
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*"
@@ -249,13 +246,13 @@ impl Parser {
self.index += 2;
self.variables.insert(
self.tokens[index + 1].clone().value,
Variable {
vtype: VariableType::Var,
state: LexerState {
Variable::new_var(
VariableType::Var,
LexerState {
line: self.tokens[index + 1].clone().line,
column: self.tokens[index + 1].clone().column,
},
},
),
);
}
}
+37 -37
View File
@@ -3,8 +3,8 @@ use crate::{
parser::{new_vars, Ast, AstType, Parser},
variable::{Variable, VariableType},
};
use std::collections::HashMap;
use std::fs;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Transpiler {
@@ -98,31 +98,28 @@ impl Transpiler {
if self.auto_pub {
result += "pub ";
}
let mut vars: HashMap<String, Variable> = HashMap::new();
let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars);
result += format!(
"fn {}({}) -> {} {}",
ast.tokens[1].value,
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
round,
ast.tokens[0].value,
self.transpile(
ast.tokens[3].value.clone(),
indent + 1,
variables.clone()
)
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars)
)
.as_str();
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
if self.auto_pub {
result += "pub ";
}
let mut vars: HashMap<String, Variable> = HashMap::new();
let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars);
// panic!("{:?}", vars);
result += format!(
"fn {}({}) {}",
ast.tokens[1].value,
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
self.transpile(
ast.tokens[3].value.clone(),
indent + 1,
variables.clone()
)
round,
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars)
)
.as_str();
} else if ast.ast_type == AstType::StructDeceleration {
@@ -133,8 +130,11 @@ impl Transpiler {
"struct {} {} {}",
ast.tokens[0].value,
"{\n",
self.transpile_round(ast.tokens[1].value.clone(), variables.clone())
.trim_end()
self.transpile_round(
ast.tokens[1].value.clone(),
&mut variables.clone()
)
.trim_end()
)
.replace(
"\n",
@@ -162,7 +162,10 @@ impl Transpiler {
{
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
self.transpile_round(
ast.tokens[0].value.clone(),
&mut variables.clone()
)
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
@@ -242,7 +245,10 @@ impl Transpiler {
result += format!(
"{} {} {}",
ast.tokens[0].value.clone(),
self.transpile_round(ast.tokens[1].value.clone(), variables.clone()),
self.transpile_round(
ast.tokens[1].value.clone(),
&mut variables.clone()
),
self.transpile(
ast.tokens[2].value.clone(),
indent + 1,
@@ -326,22 +332,7 @@ impl Transpiler {
}
}
}
result = result.trim_end().to_string();
// if self.var_match != "" && (self.state.line + input.len()) > self.var_state.line {
// for (name, var) in variables {
// if (self.var_state.line > var.line
// || (self.var_state.line == var.line
// && self.var_state.column > var.column))
// && name.to_lowercase().starts_with(&self.var_match)
// {
// self.matched_vars.insert(name, var);
// }
// }
// self.var_match = String::new();
// }
if indent > 0 {
result += "\n";
result += " ".repeat((indent as usize - 1) * 2).as_str();
@@ -374,7 +365,7 @@ impl Transpiler {
pub fn transpile_round(
&mut self,
input: String,
variables: HashMap<String, Variable>,
variables: &mut HashMap<String, Variable>,
) -> String {
let mut result = String::new();
let lexer_out = lex(input.as_str(), false, self.state);
@@ -386,7 +377,7 @@ impl Transpiler {
tokens: vec![],
};
for ast in full_ast.parse() {
let variables = full_ast.variables.clone();
*variables = full_ast.variables.clone();
if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Identifier
&& ast.tokens[0].value.contains(&self.peek)
@@ -435,7 +426,10 @@ impl Transpiler {
{
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
self.transpile_round(
ast.tokens[0].value.clone(),
&mut variables.clone()
)
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
@@ -553,7 +547,10 @@ impl Transpiler {
{
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
self.transpile_round(
ast.tokens[0].value.clone(),
&mut variables.clone()
)
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
@@ -670,7 +667,10 @@ impl Transpiler {
{
result += format!(
"({})",
self.transpile_round(ast.tokens[0].value.clone(), variables.clone())
self.transpile_round(
ast.tokens[0].value.clone(),
&mut variables.clone()
)
)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
+19
View File
@@ -11,4 +11,23 @@ pub enum VariableType {
pub struct Variable {
pub vtype: VariableType,
pub state: LexerState,
pub params: Vec<[String; 2]>,
}
impl Variable {
pub fn new_var(vtype: VariableType, state: LexerState) -> Variable {
Variable {
vtype,
state,
params: vec![],
}
}
pub fn new_func(vtype: VariableType, state: LexerState) -> Variable {
Variable {
vtype,
state,
params: vec![],
}
}
}