few fixes
This commit is contained in:
+17
-20
@@ -73,10 +73,7 @@ pub struct Parser {
|
|||||||
pub fn new_vars() -> HashMap<String, Variable> {
|
pub fn new_vars() -> HashMap<String, Variable> {
|
||||||
HashMap::from([(
|
HashMap::from([(
|
||||||
"void".to_string(),
|
"void".to_string(),
|
||||||
Variable {
|
Variable::new_var(VariableType::Keyword, LexerState { line: 0, column: 0 }),
|
||||||
vtype: VariableType::Keyword,
|
|
||||||
state: LexerState { line: 0, column: 0 },
|
|
||||||
},
|
|
||||||
)])
|
)])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,13 +179,13 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.insert(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable {
|
Variable::new_func(
|
||||||
vtype: VariableType::Func,
|
VariableType::Func,
|
||||||
state: LexerState {
|
LexerState {
|
||||||
line: self.tokens[index + 1].clone().line,
|
line: self.tokens[index + 1].clone().line,
|
||||||
column: self.tokens[index + 1].clone().column,
|
column: self.tokens[index + 1].clone().column,
|
||||||
},
|
},
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
self.index += 3;
|
self.index += 3;
|
||||||
} else if self.tokens.len() - index > 1
|
} else if self.tokens.len() - index > 1
|
||||||
@@ -212,13 +209,13 @@ impl Parser {
|
|||||||
self.index += 1;
|
self.index += 1;
|
||||||
self.variables.insert(
|
self.variables.insert(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable {
|
Variable::new_var(
|
||||||
vtype: VariableType::Var,
|
VariableType::Var,
|
||||||
state: LexerState {
|
LexerState {
|
||||||
line: self.tokens[index + 1].clone().line,
|
line: self.tokens[index + 1].clone().line,
|
||||||
column: self.tokens[index + 1].clone().column,
|
column: self.tokens[index + 1].clone().column,
|
||||||
},
|
},
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index + 2].token_type == TokenType::Identifier
|
&& self.tokens[index + 2].token_type == TokenType::Identifier
|
||||||
@@ -232,13 +229,13 @@ impl Parser {
|
|||||||
self.index += 2;
|
self.index += 2;
|
||||||
self.variables.insert(
|
self.variables.insert(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable {
|
Variable::new_var(
|
||||||
vtype: VariableType::Var,
|
VariableType::Var,
|
||||||
state: LexerState {
|
LexerState {
|
||||||
line: self.tokens[index + 1].clone().line,
|
line: self.tokens[index + 1].clone().line,
|
||||||
column: self.tokens[index + 1].clone().column,
|
column: self.tokens[index + 1].clone().column,
|
||||||
},
|
},
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index + 1].value == "*"
|
&& self.tokens[index + 1].value == "*"
|
||||||
@@ -249,13 +246,13 @@ impl Parser {
|
|||||||
self.index += 2;
|
self.index += 2;
|
||||||
self.variables.insert(
|
self.variables.insert(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable {
|
Variable::new_var(
|
||||||
vtype: VariableType::Var,
|
VariableType::Var,
|
||||||
state: LexerState {
|
LexerState {
|
||||||
line: self.tokens[index + 1].clone().line,
|
line: self.tokens[index + 1].clone().line,
|
||||||
column: self.tokens[index + 1].clone().column,
|
column: self.tokens[index + 1].clone().column,
|
||||||
},
|
},
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-36
@@ -3,8 +3,8 @@ use crate::{
|
|||||||
parser::{new_vars, Ast, AstType, Parser},
|
parser::{new_vars, Ast, AstType, Parser},
|
||||||
variable::{Variable, VariableType},
|
variable::{Variable, VariableType},
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Transpiler {
|
pub struct Transpiler {
|
||||||
@@ -98,31 +98,28 @@ impl Transpiler {
|
|||||||
if self.auto_pub {
|
if self.auto_pub {
|
||||||
result += "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!(
|
result += format!(
|
||||||
"fn {}({}) -> {} {}",
|
"fn {}({}) -> {} {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
round,
|
||||||
ast.tokens[0].value,
|
ast.tokens[0].value,
|
||||||
self.transpile(
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars)
|
||||||
ast.tokens[3].value.clone(),
|
|
||||||
indent + 1,
|
|
||||||
variables.clone()
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} 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> = HashMap::new();
|
||||||
|
let round = self.transpile_round(ast.tokens[2].value.clone(), &mut vars);
|
||||||
|
// panic!("{:?}", vars);
|
||||||
result += format!(
|
result += format!(
|
||||||
"fn {}({}) {}",
|
"fn {}({}) {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
round,
|
||||||
self.transpile(
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars)
|
||||||
ast.tokens[3].value.clone(),
|
|
||||||
indent + 1,
|
|
||||||
variables.clone()
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructDeceleration {
|
} else if ast.ast_type == AstType::StructDeceleration {
|
||||||
@@ -133,7 +130,10 @@ impl Transpiler {
|
|||||||
"struct {} {} {}",
|
"struct {} {} {}",
|
||||||
ast.tokens[0].value,
|
ast.tokens[0].value,
|
||||||
"{\n",
|
"{\n",
|
||||||
self.transpile_round(ast.tokens[1].value.clone(), variables.clone())
|
self.transpile_round(
|
||||||
|
ast.tokens[1].value.clone(),
|
||||||
|
&mut variables.clone()
|
||||||
|
)
|
||||||
.trim_end()
|
.trim_end()
|
||||||
)
|
)
|
||||||
.replace(
|
.replace(
|
||||||
@@ -162,7 +162,10 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
result += format!(
|
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();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
@@ -242,7 +245,10 @@ impl Transpiler {
|
|||||||
result += format!(
|
result += format!(
|
||||||
"{} {} {}",
|
"{} {} {}",
|
||||||
ast.tokens[0].value.clone(),
|
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(
|
self.transpile(
|
||||||
ast.tokens[2].value.clone(),
|
ast.tokens[2].value.clone(),
|
||||||
indent + 1,
|
indent + 1,
|
||||||
@@ -326,22 +332,7 @@ impl Transpiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result.trim_end().to_string();
|
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 {
|
if indent > 0 {
|
||||||
result += "\n";
|
result += "\n";
|
||||||
result += " ".repeat((indent as usize - 1) * 2).as_str();
|
result += " ".repeat((indent as usize - 1) * 2).as_str();
|
||||||
@@ -374,7 +365,7 @@ impl Transpiler {
|
|||||||
pub fn transpile_round(
|
pub fn transpile_round(
|
||||||
&mut self,
|
&mut self,
|
||||||
input: String,
|
input: String,
|
||||||
variables: HashMap<String, Variable>,
|
variables: &mut HashMap<String, Variable>,
|
||||||
) -> String {
|
) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, self.state);
|
let lexer_out = lex(input.as_str(), false, self.state);
|
||||||
@@ -386,7 +377,7 @@ impl Transpiler {
|
|||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
let variables = full_ast.variables.clone();
|
*variables = full_ast.variables.clone();
|
||||||
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)
|
||||||
@@ -435,7 +426,10 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
result += format!(
|
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();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
@@ -553,7 +547,10 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
result += format!(
|
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();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
@@ -670,7 +667,10 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
result += format!(
|
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();
|
.as_str();
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
|
||||||
|
|||||||
@@ -11,4 +11,23 @@ pub enum VariableType {
|
|||||||
pub struct Variable {
|
pub struct Variable {
|
||||||
pub vtype: VariableType,
|
pub vtype: VariableType,
|
||||||
pub state: LexerState,
|
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![],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user