New variable system and stage 1 error checking
This commit is contained in:
+5
-2
@@ -1,4 +1,7 @@
|
|||||||
use crate::lspcom::{get_completion, get_items, request_methods, LspServer, TextDocumentChangeParams};
|
use crate::{
|
||||||
|
lspcom::{get_completion, get_items, request_methods, LspServer, TextDocumentChangeParams},
|
||||||
|
variable::Variables,
|
||||||
|
};
|
||||||
use lsp_types::*;
|
use lsp_types::*;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@@ -33,7 +36,7 @@ impl LspServer for Server {
|
|||||||
.documents
|
.documents
|
||||||
.get(params.text_document_position.text_document.uri.as_str())
|
.get(params.text_document_position.text_document.uri.as_str())
|
||||||
.expect("err_textdoc");
|
.expect("err_textdoc");
|
||||||
let items: HashMap<String, crate::variable::Variable> = get_completion(
|
let items: Variables = get_completion(
|
||||||
text.clone(),
|
text.clone(),
|
||||||
params.text_document_position.position.line as usize + 1,
|
params.text_document_position.position.line as usize + 1,
|
||||||
params.text_document_position.position.character as usize,
|
params.text_document_position.position.character as usize,
|
||||||
|
|||||||
+23
-12
@@ -1,14 +1,12 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
parser::new_vars,
|
|
||||||
transpiler::Transpiler,
|
transpiler::Transpiler,
|
||||||
variable::{Variable, VariableType},
|
variable::{VariableType, Variables},
|
||||||
};
|
};
|
||||||
use lsp_types::{
|
use lsp_types::{
|
||||||
CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse, InitializeResult,
|
CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse, InitializeResult,
|
||||||
};
|
};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String {
|
pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String {
|
||||||
let mut line: usize = 1;
|
let mut line: usize = 1;
|
||||||
@@ -28,14 +26,14 @@ pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_completion(input: String, line: usize, column: usize) -> HashMap<String, Variable> {
|
pub fn get_completion(input: String, line: usize, column: usize) -> Variables {
|
||||||
let rand_id: u32 = thread_rng().gen();
|
let rand_id: u32 = thread_rng().gen();
|
||||||
let rand_id: String = rand_id.to_string();
|
let rand_id: String = rand_id.to_string();
|
||||||
let mut transpiler = Transpiler {
|
let mut transpiler = Transpiler {
|
||||||
peek: rand_id.clone(),
|
peek: rand_id.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
transpiler.transpile(place_at(input, rand_id, line, column), 0, new_vars());
|
transpiler.transpile(place_at(input, rand_id, line, column), 0, Variables::new());
|
||||||
transpiler.matched_vars
|
transpiler.matched_vars
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,15 +61,16 @@ pub struct TextDocumentChangeParams {
|
|||||||
pub text: String,
|
pub text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_items(
|
pub fn get_items(mut items: Variables, lname: String) -> Vec<CompletionItem> {
|
||||||
items: HashMap<String, crate::variable::Variable>,
|
|
||||||
lname: String,
|
|
||||||
) -> Vec<CompletionItem> {
|
|
||||||
let mut completion_items: Vec<CompletionItem> = Vec::new();
|
let mut completion_items: Vec<CompletionItem> = Vec::new();
|
||||||
for (name, var) in items {
|
for (name, var) in items.iter_mut() {
|
||||||
let mut item = CompletionItem::new_simple(lname.clone() + &name, var.desc);
|
let mut item = CompletionItem::new_simple(lname.clone() + &name, var.desc.clone());
|
||||||
if var.vtype != VariableType::Func {
|
if var.vtype != VariableType::Func {
|
||||||
completion_items.extend(get_items(var.params, name + "::").iter().cloned())
|
completion_items.extend(
|
||||||
|
get_items(var.params.clone(), name.to_owned() + "::")
|
||||||
|
.iter()
|
||||||
|
.cloned(),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
// item.documentation = Some(Documentation::MarkupContent(MarkupContent {
|
// item.documentation = Some(Documentation::MarkupContent(MarkupContent {
|
||||||
// kind: MarkupKind::Markdown,
|
// kind: MarkupKind::Markdown,
|
||||||
@@ -99,3 +98,15 @@ pub fn get_items(
|
|||||||
}
|
}
|
||||||
completion_items
|
completion_items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum ProblemType {
|
||||||
|
VariableNotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Problem {
|
||||||
|
pub code: u32,
|
||||||
|
pub problem_type: ProblemType,
|
||||||
|
pub problem_msg: String,
|
||||||
|
}
|
||||||
+5
-2
@@ -6,9 +6,9 @@ mod parser;
|
|||||||
mod transpiler;
|
mod transpiler;
|
||||||
mod variable;
|
mod variable;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use parser::new_vars;
|
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
use transpiler::Transpiler;
|
use transpiler::Transpiler;
|
||||||
|
use variable::Variables;
|
||||||
|
|
||||||
use crate::lsp::run_lsp_server;
|
use crate::lsp::run_lsp_server;
|
||||||
|
|
||||||
@@ -38,7 +38,10 @@ fn main() {
|
|||||||
}
|
}
|
||||||
fs::create_dir("build").expect("error making build");
|
fs::create_dir("build").expect("error making build");
|
||||||
let mut trsp = Transpiler::default();
|
let mut trsp = Transpiler::default();
|
||||||
let transpiled_code = trsp.transpile(file_content, 0, new_vars());
|
let transpiled_code = trsp.transpile(file_content, 0, Variables::new());
|
||||||
|
for problem in trsp.problems {
|
||||||
|
println!("{}", problem.problem_msg)
|
||||||
|
}
|
||||||
|
|
||||||
match args.rust {
|
match args.rust {
|
||||||
Some(ref rust_file_name) => {
|
Some(ref rust_file_name) => {
|
||||||
|
|||||||
+21
-32
@@ -1,10 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
lexer::{LexerState, Token, TokenType},
|
lexer::{LexerState, Token, TokenType},
|
||||||
variable::Variable,
|
variable::Variables,
|
||||||
};
|
};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{collections::HashMap, fmt};
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum AstType {
|
pub enum AstType {
|
||||||
@@ -28,6 +28,16 @@ pub enum AstType {
|
|||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_decl(ast: &Ast) -> bool {
|
||||||
|
ast.ast_type == AstType::FunctionDeceleration
|
||||||
|
|| ast.ast_type == AstType::VoidFunctionDeceleration
|
||||||
|
|| ast.ast_type == AstType::Namespace
|
||||||
|
|| ast.ast_type == AstType::VariableDeceleration
|
||||||
|
|| ast.ast_type == AstType::PointerDeceleration
|
||||||
|
|| ast.ast_type == AstType::MutVariableDeceleration
|
||||||
|
|| ast.ast_type == AstType::StructDeceleration
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Ast {
|
pub struct Ast {
|
||||||
pub tokens: Vec<Token>,
|
pub tokens: Vec<Token>,
|
||||||
pub ast_type: AstType,
|
pub ast_type: AstType,
|
||||||
@@ -66,19 +76,12 @@ pub struct Parser {
|
|||||||
pub index: u32,
|
pub index: u32,
|
||||||
pub include_regex: Lazy<Regex>,
|
pub include_regex: Lazy<Regex>,
|
||||||
pub include_regex_local: Lazy<Regex>,
|
pub include_regex_local: Lazy<Regex>,
|
||||||
pub variables: HashMap<String, Variable>,
|
pub variables: Variables,
|
||||||
pub json: bool,
|
pub json: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_vars() -> HashMap<String, Variable> {
|
|
||||||
HashMap::from([(
|
|
||||||
"void".to_string(),
|
|
||||||
Variable::new_keyword(LexerState { line: 0, column: 0 }, String::from("")),
|
|
||||||
)])
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parser {
|
impl Parser {
|
||||||
pub fn new(tokens: Vec<Token>, variables: HashMap<String, Variable>) -> Parser {
|
pub fn new(tokens: Vec<Token>, variables: Variables) -> Parser {
|
||||||
Parser {
|
Parser {
|
||||||
tokens: tokens,
|
tokens: tokens,
|
||||||
index: 0,
|
index: 0,
|
||||||
@@ -128,15 +131,13 @@ impl Parser {
|
|||||||
if index > 0 && self.tokens[index - 1].token_type == TokenType::Comment {
|
if index > 0 && self.tokens[index - 1].token_type == TokenType::Comment {
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_struct(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_struct(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index].value == "namespace"
|
&& self.tokens[index].value == "namespace"
|
||||||
@@ -151,15 +152,13 @@ impl Parser {
|
|||||||
if index > 0 && self.tokens[index - 1].token_type == TokenType::Comment {
|
if index > 0 && self.tokens[index - 1].token_type == TokenType::Comment {
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_namespace(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_namespace(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index].value == "impl"
|
&& self.tokens[index].value == "impl"
|
||||||
@@ -211,15 +210,13 @@ impl Parser {
|
|||||||
{
|
{
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_func(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_func(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 1
|
} else if self.tokens.len() - index > 1
|
||||||
&& self.tokens[index + 1].token_type == TokenType::Curly
|
&& self.tokens[index + 1].token_type == TokenType::Curly
|
||||||
@@ -240,15 +237,13 @@ impl Parser {
|
|||||||
{
|
{
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_var(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_var(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} 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 {
|
||||||
@@ -261,15 +256,13 @@ impl Parser {
|
|||||||
{
|
{
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_var(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_var(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} 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
|
||||||
@@ -287,15 +280,13 @@ impl Parser {
|
|||||||
{
|
{
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_var(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_var(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index + 1].value == "*"
|
&& self.tokens[index + 1].value == "*"
|
||||||
@@ -310,15 +301,13 @@ impl Parser {
|
|||||||
{
|
{
|
||||||
desc = self.tokens[index - 1].value.clone()
|
desc = self.tokens[index - 1].value.clone()
|
||||||
}
|
}
|
||||||
self.variables.insert(
|
self.variables.new_var(
|
||||||
self.tokens[index + 1].clone().value,
|
self.tokens[index + 1].clone().value,
|
||||||
Variable::new_var(
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
desc,
|
desc,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+174
-102
@@ -1,9 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
lexer::{lex, LexerState, TokenType},
|
lexer::{lex, LexerState, TokenType},
|
||||||
parser::{new_vars, Ast, AstType, Parser},
|
lspcom::Problem,
|
||||||
variable::{Variable, VariableType},
|
parser::{is_decl, Ast, AstType, Parser},
|
||||||
|
variable::{VariableType, Variables},
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -15,7 +16,8 @@ pub struct Transpiler {
|
|||||||
pub macros: Vec<String>,
|
pub macros: Vec<String>,
|
||||||
pub modnum: u32,
|
pub modnum: u32,
|
||||||
pub peek: String,
|
pub peek: String,
|
||||||
pub matched_vars: HashMap<String, Variable>,
|
pub matched_vars: Variables,
|
||||||
|
pub problems: Vec<Problem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Transpiler {
|
impl Default for Transpiler {
|
||||||
@@ -28,18 +30,14 @@ impl Default for Transpiler {
|
|||||||
macros: vec![String::from("println")],
|
macros: vec![String::from("println")],
|
||||||
modnum: 0,
|
modnum: 0,
|
||||||
peek: String::new(),
|
peek: String::new(),
|
||||||
matched_vars: HashMap::new(),
|
matched_vars: Variables::new(),
|
||||||
|
problems: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transpiler {
|
impl Transpiler {
|
||||||
pub fn transpile(
|
pub fn transpile(&mut self, input: String, indent: u32, variables: Variables) -> String {
|
||||||
&mut self,
|
|
||||||
input: String,
|
|
||||||
indent: u32,
|
|
||||||
variables: HashMap<String, Variable>,
|
|
||||||
) -> String {
|
|
||||||
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";
|
||||||
@@ -58,6 +56,7 @@ impl Transpiler {
|
|||||||
let f_ast = full_ast.parse();
|
let f_ast = full_ast.parse();
|
||||||
let mut variables = full_ast.variables.clone();
|
let mut variables = full_ast.variables.clone();
|
||||||
for ast in f_ast {
|
for ast in f_ast {
|
||||||
|
let mut ast = 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)
|
||||||
@@ -65,15 +64,28 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
let ctoken = &ast.tokens[0];
|
let ctoken = &ast.tokens[0];
|
||||||
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
for (name, var) in variables.clone() {
|
for (name, var) in variables.clone().iter_mut() {
|
||||||
if (ctoken.line > var.state.line && var.vtype == VariableType::Var)
|
if (ctoken.line > var.state.line && var.vtype == VariableType::Var)
|
||||||
|| var.vtype != VariableType::Var
|
|| var.vtype != VariableType::Var
|
||||||
{
|
{
|
||||||
self.matched_vars.insert(name, var);
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.peek = String::new();
|
self.peek = String::new();
|
||||||
continue;
|
continue;
|
||||||
|
} else {
|
||||||
|
for i in (if is_decl(&ast) { 1 } else { 0 })..ast.tokens.len() {
|
||||||
|
if ast.tokens[i].token_type == TokenType::Identifier {
|
||||||
|
if ast.tokens[i].value.contains(&self.peek) && self.peek != "" {}
|
||||||
|
let x = variables.get_var(ast.tokens[i].value.clone(), self);
|
||||||
|
ast.tokens[i].value = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if last_ast.tokens.len() > 0 {
|
if last_ast.tokens.len() > 0 {
|
||||||
let mut fl = 0;
|
let mut fl = 0;
|
||||||
@@ -100,7 +112,7 @@ impl Transpiler {
|
|||||||
if self.auto_pub {
|
if self.auto_pub {
|
||||||
result += "pub ";
|
result += "pub ";
|
||||||
}
|
}
|
||||||
let mut vars: HashMap<String, Variable> = variables.clone();
|
let mut vars: Variables = 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 {}({}) -> {} {}",
|
||||||
@@ -110,27 +122,12 @@ impl Transpiler {
|
|||||||
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
let vvars = variables.clone();
|
|
||||||
if let Some(v) = variables.get_mut(&ast.tokens[1].value) {
|
|
||||||
for (name, var) in vars {
|
|
||||||
if !(vvars.contains_key(&name)) {
|
|
||||||
v.params.insert(
|
|
||||||
name,
|
|
||||||
Variable::new_var(
|
|
||||||
LexerState { line: 0, column: 0 },
|
|
||||||
var.desc,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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.clone();
|
let mut vars: Variables = 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);
|
|
||||||
result += format!(
|
result += format!(
|
||||||
"fn {}({}) {}",
|
"fn {}({}) {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
@@ -138,25 +135,11 @@ impl Transpiler {
|
|||||||
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, vars.clone())
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
let vvars = variables.clone();
|
|
||||||
if let Some(v) = variables.get_mut(&ast.tokens[1].value) {
|
|
||||||
for (name, var) in vars {
|
|
||||||
if !(vvars.contains_key(&name)) {
|
|
||||||
v.params.insert(
|
|
||||||
name,
|
|
||||||
Variable::new_var(
|
|
||||||
LexerState { line: 0, column: 0 },
|
|
||||||
var.desc,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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 mut vars: Variables = variables.clone();
|
||||||
let round = self.transpile_round(ast.tokens[1].value.clone(), &mut vars);
|
let round = self.transpile_round(ast.tokens[1].value.clone(), &mut vars);
|
||||||
result += format!(
|
result += format!(
|
||||||
"struct {} {} {}",
|
"struct {} {} {}",
|
||||||
@@ -172,15 +155,13 @@ impl Transpiler {
|
|||||||
.as_str();
|
.as_str();
|
||||||
result += "\n}\n";
|
result += "\n}\n";
|
||||||
let vvars = variables.clone();
|
let vvars = variables.clone();
|
||||||
if let Some(v) = variables.get_mut(&ast.tokens[0].value) {
|
if let Some(v) = variables.get_mut(ast.tokens[0].value.clone()) {
|
||||||
for (name, var) in vars {
|
for (name, var) in vars.iter_mut() {
|
||||||
if !(vvars.contains_key(&name)) {
|
if !(vvars.vars.contains_key(name)) {
|
||||||
v.params.insert(
|
v.params.new_var(
|
||||||
name,
|
name.to_string(),
|
||||||
Variable::new_var(
|
|
||||||
LexerState { line: 0, column: 0 },
|
LexerState { line: 0, column: 0 },
|
||||||
var.desc,
|
var.desc.clone(),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,6 +191,11 @@ impl Transpiler {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
|
} else if ast.ast_type == AstType::Other
|
||||||
|
&& ast.tokens[0].token_type == TokenType::Comment
|
||||||
|
{
|
||||||
|
result += ast.tokens[0].value.clone().as_str();
|
||||||
|
result += " ".repeat((indent as usize) * 2).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
|
||||||
{
|
{
|
||||||
result += format!(
|
result += format!(
|
||||||
@@ -230,12 +216,6 @@ impl Transpiler {
|
|||||||
result += "}";
|
result += "}";
|
||||||
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly
|
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly
|
||||||
{
|
{
|
||||||
// if ast.tokens[0].token_type == TokenType::Newline {
|
|
||||||
// result += (ast.tokens[0].value.as_str().to_owned()
|
|
||||||
// + (" ".repeat((indent as usize) * 2).as_str()))
|
|
||||||
// .as_str();
|
|
||||||
// } else {
|
|
||||||
// result += ast.tokens[0].value.as_str();
|
|
||||||
result += self
|
result += self
|
||||||
.transpile_json(
|
.transpile_json(
|
||||||
ast.tokens[0].value.as_str().to_string(),
|
ast.tokens[0].value.as_str().to_string(),
|
||||||
@@ -341,6 +321,9 @@ impl Transpiler {
|
|||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
}
|
}
|
||||||
|
} else if ast.ast_type == AstType::Ref {
|
||||||
|
result += "&mut ";
|
||||||
|
result += ast.tokens[0].value.as_str();
|
||||||
}
|
}
|
||||||
// flp
|
// flp
|
||||||
else {
|
else {
|
||||||
@@ -352,17 +335,6 @@ impl Transpiler {
|
|||||||
result += ";\n";
|
result += ";\n";
|
||||||
result += " ".repeat((indent as usize) * 2).as_str();
|
result += " ".repeat((indent as usize) * 2).as_str();
|
||||||
} else {
|
} else {
|
||||||
// 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();
|
result += ast.tokens[0].value.as_str();
|
||||||
if self.auto_macro
|
if self.auto_macro
|
||||||
&& self
|
&& self
|
||||||
@@ -396,7 +368,7 @@ impl Transpiler {
|
|||||||
self.clone().modnum
|
self.clone().modnum
|
||||||
);
|
);
|
||||||
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
|
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
|
||||||
let transpiled_code = self.transpile(file_content, 0, new_vars());
|
let transpiled_code = self.transpile(file_content, 0, Variables::new());
|
||||||
fs::write(
|
fs::write(
|
||||||
("build/".to_string() + modname.as_str()) + ".rs",
|
("build/".to_string() + modname.as_str()) + ".rs",
|
||||||
transpiled_code,
|
transpiled_code,
|
||||||
@@ -404,11 +376,7 @@ impl Transpiler {
|
|||||||
.expect("Error writing file");
|
.expect("Error writing file");
|
||||||
modname
|
modname
|
||||||
}
|
}
|
||||||
pub fn transpile_round(
|
pub fn transpile_round(&mut self, input: String, variables: &mut Variables) -> String {
|
||||||
&mut self,
|
|
||||||
input: String,
|
|
||||||
variables: &mut HashMap<String, Variable>,
|
|
||||||
) -> 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);
|
||||||
match lexer_out {
|
match lexer_out {
|
||||||
@@ -418,8 +386,10 @@ impl Transpiler {
|
|||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
let fast = full_ast.parse();
|
||||||
*variables = full_ast.variables.clone();
|
*variables = full_ast.variables.clone();
|
||||||
|
for ast in fast {
|
||||||
|
let mut ast = 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)
|
||||||
@@ -427,9 +397,44 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
let ctoken = &ast.tokens[0];
|
let ctoken = &ast.tokens[0];
|
||||||
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
for (name, var) in variables.clone() {
|
for (name, var) in variables.clone().iter_mut() {
|
||||||
|
if (ctoken.line > var.state.line && var.vtype == VariableType::Var)
|
||||||
|
|| var.vtype != VariableType::Var
|
||||||
|
{
|
||||||
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.peek = String::new();
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
for i in (if is_decl(&ast) { 1 } else { 0 })..ast.tokens.len() {
|
||||||
|
if ast.tokens[i].token_type == TokenType::Identifier {
|
||||||
|
if ast.tokens[i].value.contains(&self.peek) && self.peek != "" {}
|
||||||
|
let x = variables.get_var(ast.tokens[i].value.clone(), self);
|
||||||
|
ast.tokens[i].value = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ast.ast_type == AstType::Other
|
||||||
|
&& ast.tokens[0].token_type == TokenType::Identifier
|
||||||
|
&& ast.tokens[0].value.contains(&self.peek)
|
||||||
|
&& self.peek != ""
|
||||||
|
{
|
||||||
|
let ctoken = &ast.tokens[0];
|
||||||
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
|
for (name, var) in variables.clone().iter_mut() {
|
||||||
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
||||||
self.matched_vars.insert(name, var);
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.peek = String::new();
|
self.peek = String::new();
|
||||||
@@ -530,7 +535,7 @@ impl Transpiler {
|
|||||||
&mut self,
|
&mut self,
|
||||||
input: String,
|
input: String,
|
||||||
state: LexerState,
|
state: LexerState,
|
||||||
variables: HashMap<String, Variable>,
|
variables: Variables,
|
||||||
) -> String {
|
) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, state);
|
let lexer_out = lex(input.as_str(), false, state);
|
||||||
@@ -541,8 +546,10 @@ impl Transpiler {
|
|||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
let fast = full_ast.parse();
|
||||||
let variables = full_ast.variables.clone();
|
let mut variables = full_ast.variables.clone();
|
||||||
|
for ast in fast {
|
||||||
|
let mut ast = 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)
|
||||||
@@ -550,9 +557,44 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
let ctoken = &ast.tokens[0];
|
let ctoken = &ast.tokens[0];
|
||||||
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
for (name, var) in variables.clone() {
|
for (name, var) in variables.clone().iter_mut() {
|
||||||
|
if (ctoken.line > var.state.line && var.vtype == VariableType::Var)
|
||||||
|
|| var.vtype != VariableType::Var
|
||||||
|
{
|
||||||
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.peek = String::new();
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
for i in (if is_decl(&ast) { 1 } else { 0 })..ast.tokens.len() {
|
||||||
|
if ast.tokens[i].token_type == TokenType::Identifier {
|
||||||
|
if ast.tokens[i].value.contains(&self.peek) && self.peek != "" {}
|
||||||
|
let x = variables.get_var(ast.tokens[i].value.clone(), self);
|
||||||
|
ast.tokens[i].value = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ast.ast_type == AstType::Other
|
||||||
|
&& ast.tokens[0].token_type == TokenType::Identifier
|
||||||
|
&& ast.tokens[0].value.contains(&self.peek)
|
||||||
|
&& self.peek != ""
|
||||||
|
{
|
||||||
|
let ctoken = &ast.tokens[0];
|
||||||
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
|
for (name, var) in variables.iter_mut() {
|
||||||
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
||||||
self.matched_vars.insert(name, var);
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.peek = String::new();
|
self.peek = String::new();
|
||||||
@@ -628,6 +670,9 @@ impl Transpiler {
|
|||||||
result += " {";
|
result += " {";
|
||||||
result += ast.tokens[1].value.as_str();
|
result += ast.tokens[1].value.as_str();
|
||||||
result += "}";
|
result += "}";
|
||||||
|
} else if ast.ast_type == AstType::Ref {
|
||||||
|
result += "&mut ";
|
||||||
|
result += ast.tokens[0].value.as_str();
|
||||||
}
|
}
|
||||||
// flp
|
// flp
|
||||||
else {
|
else {
|
||||||
@@ -648,7 +693,7 @@ impl Transpiler {
|
|||||||
&mut self,
|
&mut self,
|
||||||
input: String,
|
input: String,
|
||||||
state: LexerState,
|
state: LexerState,
|
||||||
variables: HashMap<String, Variable>,
|
variables: Variables,
|
||||||
) -> String {
|
) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, state);
|
let lexer_out = lex(input.as_str(), false, state);
|
||||||
@@ -661,8 +706,10 @@ impl Transpiler {
|
|||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
let fast = full_ast.parse();
|
||||||
let variables = full_ast.variables.clone();
|
let mut variables = full_ast.variables.clone();
|
||||||
|
for ast in fast {
|
||||||
|
let mut ast = 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)
|
||||||
@@ -670,9 +717,44 @@ impl Transpiler {
|
|||||||
{
|
{
|
||||||
let ctoken = &ast.tokens[0];
|
let ctoken = &ast.tokens[0];
|
||||||
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
for (name, var) in variables.clone() {
|
for (name, var) in variables.clone().iter_mut() {
|
||||||
|
if (ctoken.line > var.state.line && var.vtype == VariableType::Var)
|
||||||
|
|| var.vtype != VariableType::Var
|
||||||
|
{
|
||||||
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.peek = String::new();
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
for i in (if is_decl(&ast) { 1 } else { 0 })..ast.tokens.len() {
|
||||||
|
if ast.tokens[i].token_type == TokenType::Identifier {
|
||||||
|
if ast.tokens[i].value.contains(&self.peek) && self.peek != "" {}
|
||||||
|
let x = variables.get_var(ast.tokens[i].value.clone(), self);
|
||||||
|
ast.tokens[i].value = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ast.ast_type == AstType::Other
|
||||||
|
&& ast.tokens[0].token_type == TokenType::Identifier
|
||||||
|
&& ast.tokens[0].value.contains(&self.peek)
|
||||||
|
&& self.peek != ""
|
||||||
|
{
|
||||||
|
let ctoken = &ast.tokens[0];
|
||||||
|
// let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
|
for (name, var) in variables.iter_mut() {
|
||||||
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
||||||
self.matched_vars.insert(name, var);
|
self.matched_vars.add(
|
||||||
|
var.vtype.clone(),
|
||||||
|
name.to_string(),
|
||||||
|
var.state,
|
||||||
|
var.desc.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.peek = String::new();
|
self.peek = String::new();
|
||||||
@@ -741,6 +823,9 @@ impl Transpiler {
|
|||||||
variables.clone(),
|
variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
|
} else if ast.ast_type == AstType::Ref {
|
||||||
|
result += "&mut ";
|
||||||
|
result += ast.tokens[0].value.as_str();
|
||||||
} else {
|
} else {
|
||||||
result += ast.tokens[0].value.as_str();
|
result += ast.tokens[0].value.as_str();
|
||||||
}
|
}
|
||||||
@@ -755,16 +840,3 @@ impl Transpiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn clean_incl(input: &str) -> String {
|
|
||||||
// input
|
|
||||||
// .chars()
|
|
||||||
// .map(|c| {
|
|
||||||
// if c.is_alphanumeric() || c == '_' {
|
|
||||||
// c
|
|
||||||
// } else {
|
|
||||||
// '_'
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// .collect()
|
|
||||||
// }
|
|
||||||
|
|||||||
+125
-17
@@ -1,6 +1,12 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::lexer::LexerState;
|
use rand::Rng;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
lexer::LexerState,
|
||||||
|
lspcom::{Problem, ProblemType},
|
||||||
|
transpiler::Transpiler,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub enum VariableType {
|
pub enum VariableType {
|
||||||
@@ -16,52 +22,154 @@ pub struct Variable {
|
|||||||
pub vtype: VariableType,
|
pub vtype: VariableType,
|
||||||
pub desc: String,
|
pub desc: String,
|
||||||
pub state: LexerState,
|
pub state: LexerState,
|
||||||
pub params: HashMap<String, Variable>,
|
pub params: Variables,
|
||||||
|
pub rname: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variable {
|
#[derive(Clone, Debug)]
|
||||||
pub fn new_var(state: LexerState, desc: String) -> Variable {
|
pub struct Variables {
|
||||||
|
pub vars: HashMap<String, Variable>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Variables {
|
||||||
|
pub fn new() -> Variables {
|
||||||
|
Variables {
|
||||||
|
vars: HashMap::from([(
|
||||||
|
"void".to_string(),
|
||||||
|
Variable {
|
||||||
|
vtype: VariableType::Keyword,
|
||||||
|
desc: "".to_string(),
|
||||||
|
state: LexerState { line: 0, column: 0 },
|
||||||
|
params: Variables {
|
||||||
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: "".to_string(),
|
||||||
|
},
|
||||||
|
)]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn new_var(&mut self, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
Variable {
|
Variable {
|
||||||
vtype: VariableType::Var,
|
vtype: VariableType::Var,
|
||||||
desc,
|
desc,
|
||||||
state,
|
state,
|
||||||
params: HashMap::new(),
|
params: Variables {
|
||||||
}
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_namespace(state: LexerState, desc: String) -> Variable {
|
pub fn new_namespace(&mut self, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
Variable {
|
Variable {
|
||||||
vtype: VariableType::Namespace,
|
vtype: VariableType::Namespace,
|
||||||
desc,
|
desc,
|
||||||
state,
|
state,
|
||||||
params: HashMap::new(),
|
params: Variables {
|
||||||
}
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_struct(state: LexerState, desc: String) -> Variable {
|
pub fn new_struct(&mut self, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
Variable {
|
Variable {
|
||||||
vtype: VariableType::Struct,
|
vtype: VariableType::Struct,
|
||||||
desc,
|
desc,
|
||||||
state,
|
state,
|
||||||
params: HashMap::new(),
|
params: Variables {
|
||||||
}
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_keyword(state: LexerState, desc: String) -> Variable {
|
pub fn new_keyword(&mut self, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
Variable {
|
Variable {
|
||||||
vtype: VariableType::Keyword,
|
vtype: VariableType::Keyword,
|
||||||
desc,
|
desc,
|
||||||
state,
|
state,
|
||||||
params: HashMap::new(),
|
params: Variables {
|
||||||
}
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_func(state: LexerState, desc: String) -> Variable {
|
pub fn new_func(&mut self, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
Variable {
|
Variable {
|
||||||
vtype: VariableType::Func,
|
vtype: VariableType::Func,
|
||||||
desc,
|
desc,
|
||||||
state,
|
state,
|
||||||
params: HashMap::new(),
|
params: Variables {
|
||||||
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
pub fn add(&mut self, vtype: VariableType, name: String, state: LexerState, desc: String) {
|
||||||
|
self.vars.insert(
|
||||||
|
name,
|
||||||
|
Variable {
|
||||||
|
vtype,
|
||||||
|
desc,
|
||||||
|
state,
|
||||||
|
params: Variables {
|
||||||
|
vars: HashMap::new(),
|
||||||
|
},
|
||||||
|
rname: generate_varname(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
pub fn get_var(&mut self, name: String, root: &mut Transpiler) -> String {
|
||||||
|
if let Some(x) = self.get_mut(name.clone()) {
|
||||||
|
return x.rname.clone();
|
||||||
|
} else {
|
||||||
|
root.problems.push(Problem {
|
||||||
|
code: 1,
|
||||||
|
problem_type: ProblemType::VariableNotFound,
|
||||||
|
problem_msg: format!("Variable '{}' doesn't exist", &name),
|
||||||
|
});
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn iter_mut(
|
||||||
|
&mut self,
|
||||||
|
) -> std::collections::hash_map::IterMut<'_, std::string::String, Variable> {
|
||||||
|
self.vars.iter_mut()
|
||||||
|
}
|
||||||
|
pub fn get_mut(&mut self, name: String) -> Option<&mut Variable> {
|
||||||
|
self.vars.get_mut(&name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn generate_varname() -> String {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let charset: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
||||||
|
abcdefghijklmnopqrstuvwxyz\
|
||||||
|
0123456789";
|
||||||
|
let mut result = String::with_capacity(19);
|
||||||
|
result.push('_');
|
||||||
|
result.push('0');
|
||||||
|
result.push('x');
|
||||||
|
for _ in 0..14 {
|
||||||
|
let random_char = charset[rng.gen_range(0..charset.len())] as char;
|
||||||
|
result.push(random_char);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user