var on parser

This commit is contained in:
2024-06-04 14:35:34 +02:00
parent 15ff863fbe
commit 5c367e6b44
5 changed files with 376 additions and 435 deletions
+2
View File
@@ -12,3 +12,5 @@ clap = { version = "4.0", features = ["derive"] }
lspower = "1.5.0" lspower = "1.5.0"
tokio = { version = "1.38.0", features = ["full"] } tokio = { version = "1.38.0", features = ["full"] }
tokio-macros = "2.3.0" tokio-macros = "2.3.0"
rand = "0.8.5"
lsp-client-rs = "0.1.0"
+11 -21
View File
@@ -1,12 +1,13 @@
use std::collections::HashMap; // use std::collections::HashMap;
// use std::fs;
use std::fs; use std::fs;
use lspower;
use lspower::jsonrpc::Result; use lspower::jsonrpc::Result;
use lspower::lsp::{self, *}; use lspower::lsp::{self, *};
use lspower::{Client, LanguageServer}; use lspower::{Client, LanguageServer};
use lspower;
use crate::lexer::LexerState;
use crate::transpiler::Transpiler; use crate::transpiler::Transpiler;
#[derive(Debug)] #[derive(Debug)]
@@ -41,7 +42,7 @@ impl LanguageServer for Backend {
_params: lsp::CompletionParams, _params: lsp::CompletionParams,
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> { ) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
Ok(Some(CompletionResponse::Array(vec![ Ok(Some(CompletionResponse::Array(vec![
CompletionItem::new_simple("mylabel".to_string(), "mydetail".to_string()) CompletionItem::new_simple("mylabel".to_string(), "mydetail".to_string()),
]))) ])))
} }
@@ -50,7 +51,6 @@ impl LanguageServer for Backend {
} }
} }
// #[tokio::main]
// pub async fn run_lsp_server() { // pub async fn run_lsp_server() {
// let stdin = tokio::io::stdin(); // let stdin = tokio::io::stdin();
// let stdout = tokio::io::stdout(); // let stdout = tokio::io::stdout();
@@ -62,22 +62,12 @@ impl LanguageServer for Backend {
// .await; // .await;
// } // }
pub fn run_lsp_server() { #[tokio::main]
let mut options = Transpiler { pub async fn run_lsp_server() {
auto_mut: true, let mut transpiler = Transpiler {
auto_macro: true,
auto_pub: false,
macros: vec![String::from("println")],
modnum: 0,
var_match: String::from("x"), var_match: String::from("x"),
var_state: LexerState { line: 4, column: 4 }, ..Default::default()
matched_vars: HashMap::new()
}; };
let file_content = fs::read_to_string("main.wt") let file_content = fs::read_to_string("main.wt").expect("Error reading file");
.expect("Error reading file"); transpiler.transpile(file_content, 0);
let _ = options.transpile(file_content.to_string(),
0,
LexerState { line: 1, column: 0 },
);
println!("{:?}", options.matched_vars);
} }
+11 -10
View File
@@ -1,11 +1,10 @@
mod transpiler;
mod parser;
mod lexer;
mod compile; mod compile;
mod lexer;
mod lsp; mod lsp;
use std::{fs, path::Path}; mod parser;
mod transpiler;
use clap::Parser; use clap::Parser;
use lexer::LexerState; use std::{fs, path::Path};
use transpiler::Transpiler; use transpiler::Transpiler;
use crate::lsp::run_lsp_server; use crate::lsp::run_lsp_server;
@@ -28,21 +27,23 @@ fn main() {
match args.stdio { match args.stdio {
true => { true => {
run_lsp_server(); run_lsp_server();
}, }
false => { false => {
let file_content = fs::read_to_string("main.wt") let file_content = fs::read_to_string("main.wt").expect("Error reading file");
.expect("Error reading file");
if Path::new("build").exists() { if Path::new("build").exists() {
fs::remove_dir_all("build").expect("err rm build"); fs::remove_dir_all("build").expect("err rm build");
} }
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, LexerState { line: 1, column: 0 }); let transpiled_code = trsp.transpile(file_content, 0);
match args.rust { match args.rust {
Some(ref rust_file_name) => { Some(ref rust_file_name) => {
// Write transpiled code to Rust file // Write transpiled code to Rust file
compile::write_to_rust_file(&transpiled_code, ("build/".to_string()+rust_file_name).as_str()) compile::write_to_rust_file(
&transpiled_code,
("build/".to_string() + rust_file_name).as_str(),
)
.expect("Error writing to Rust file"); .expect("Error writing to Rust file");
println!("Transpiled code written to {}", rust_file_name); println!("Transpiled code written to {}", rust_file_name);
} }
+37 -7
View File
@@ -1,7 +1,7 @@
use crate::lexer::{Token, TokenType}; use crate::lexer::{Token, TokenType};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use std::fmt; use std::{collections::HashMap, fmt};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
@@ -64,19 +64,35 @@ pub struct Parser {
pub include_regex: Lazy<Regex>, pub include_regex: Lazy<Regex>,
pub include_regex_local: Lazy<Regex>, pub include_regex_local: Lazy<Regex>,
pub json: bool, pub json: bool,
pub variables: HashMap<String, Variable>,
}
#[derive(Debug, PartialEq, Clone)]
pub enum VariableType {
Func,
Var,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Variable {
pub vtype: VariableType,
pub indent: u32,
pub line: usize,
pub column: usize,
} }
impl Parser { impl Parser {
pub fn new(tokens: Vec<Token>) -> Parser { pub fn new(tokens: Vec<Token>, variables: HashMap<String, Variable>) -> Parser {
Parser { Parser {
tokens: tokens, tokens: tokens,
index: 0, index: 0,
include_regex: Lazy::new(|| Regex::new(r"^(#include *)<(.*?)>").unwrap()), include_regex: Lazy::new(|| Regex::new(r"^(#include *)<(.*?)>").unwrap()),
include_regex_local: Lazy::new(|| Regex::new(r#"^(#include *)"(.*?)""#).unwrap()), include_regex_local: Lazy::new(|| Regex::new(r#"^(#include *)"(.*?)""#).unwrap()),
variables: variables,
json: false, json: false,
} }
} }
pub fn next(&mut self) -> Ast { pub fn parse(&mut self) -> Vec<Ast> {
let mut full_ast: Vec<Ast> = Vec::new();
while self.tokens.len() > self.index as usize {
let mut ast_res: Ast = Ast { let mut ast_res: Ast = Ast {
tokens: vec![], tokens: vec![],
ast_type: AstType::Other, ast_type: AstType::Other,
@@ -96,7 +112,8 @@ impl Parser {
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 1 } else if self.tokens.len() - index > 1
&& self.tokens[index].value == "&" && self.tokens[index].value == "&"
&& self.tokens[index + 1].token_type == TokenType::Identifier { && 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());
ast_res.ast_type = AstType::Ref; ast_res.ast_type = AstType::Ref;
self.index += 1; self.index += 1;
@@ -162,6 +179,15 @@ impl Parser {
} else { } else {
ast_res.ast_type = AstType::FunctionDeceleration; ast_res.ast_type = AstType::FunctionDeceleration;
} }
self.variables.insert(
self.tokens[index + 1].value.clone(),
Variable {
vtype: VariableType::Func,
indent: 0,
line: self.tokens[index + 1].line.clone(),
column: self.tokens[index + 1].column.clone(),
},
);
self.index += 3; self.index += 3;
} 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
@@ -194,7 +220,8 @@ impl Parser {
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*" && self.tokens[index + 1].value == "*"
&& self.tokens[index + 2].token_type == TokenType::Identifier { && self.tokens[index + 2].token_type == TokenType::Identifier
{
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::PointerDeceleration; ast_res.ast_type = AstType::PointerDeceleration;
self.index += 2; self.index += 2;
@@ -224,7 +251,8 @@ impl Parser {
} }
} }
TokenType::Keyword => { TokenType::Keyword => {
if token.value == "cb" && self.tokens[index + 1].token_type == TokenType::Curly if token.value == "cb"
&& self.tokens[index + 1].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::CodeBlock; ast_res.ast_type = AstType::CodeBlock;
@@ -239,6 +267,8 @@ impl Parser {
} }
} }
self.index += 1; self.index += 1;
ast_res full_ast.push(ast_res);
}
full_ast
} }
} }
+121 -203
View File
@@ -1,21 +1,11 @@
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser}; use crate::parser::{Ast, AstType, Parser, Variable, VariableType};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
#[derive(Debug, PartialEq, Clone)]
pub enum VarTypes {
Var
}
#[derive(Debug, PartialEq, Clone)]
pub struct Var {
vtype: VarTypes,
state: LexerState
}
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Transpiler { pub struct Transpiler {
pub state: LexerState,
pub auto_mut: bool, pub auto_mut: bool,
pub auto_macro: bool, pub auto_macro: bool,
pub auto_pub: bool, pub auto_pub: bool,
@@ -23,12 +13,13 @@ pub struct Transpiler {
pub modnum: u32, pub modnum: u32,
pub var_match: String, pub var_match: String,
pub var_state: LexerState, pub var_state: LexerState,
pub matched_vars: HashMap<String, Var>, pub matched_vars: HashMap<String, Variable>,
} }
impl Default for Transpiler { impl Default for Transpiler {
fn default() -> Transpiler { fn default() -> Transpiler {
Transpiler { Transpiler {
state: LexerState { line: 1, column: 0 },
auto_mut: true, auto_mut: true,
auto_macro: true, auto_macro: true,
auto_pub: false, auto_pub: false,
@@ -36,33 +27,33 @@ impl Default for Transpiler {
modnum: 0, modnum: 0,
var_state: LexerState { line: 0, column: 0 }, var_state: LexerState { line: 0, column: 0 },
var_match: String::new(), var_match: String::new(),
matched_vars: HashMap::new() matched_vars: HashMap::new(),
} }
} }
} }
impl Transpiler { impl Transpiler {
pub fn transpile(&mut self, input: String, indent: u32, state: LexerState) -> String { pub fn transpile(&mut self, input: String, indent: u32) -> String {
let mut result = String::new(); let mut result = String::new();
let mut variables: HashMap<String, Var> = HashMap::new(); let mut variables: HashMap<String, Variable> = HashMap::new();
if indent == 0 { if indent == 0 {
// result += "type int = i32;\n"; // result += "type int = i32;\n";
} else { } else {
result += " ".repeat((indent as usize) * 2).as_str(); result += " ".repeat((indent as usize) * 2).as_str();
} }
let lexer_out = lex(input.as_str(), false, self.state);
let lexer_out = lex(input.as_str(), false, state);
match lexer_out { match lexer_out {
Ok(tokens) => { Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone()); let mut full_ast = Parser::new(tokens.clone(), variables);
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
}; };
while full_ast.tokens.len() > full_ast.index as usize { for ast in full_ast.parse() {
let ast = full_ast.next(); let variables = full_ast.variables.clone();
println!("{:?}", variables);
if last_ast.tokens.len() > 0 { if last_ast.tokens.len() > 0 {
let mut fl = 0; let mut fl = 0;
for t in &last_ast.tokens { for t in &last_ast.tokens {
@@ -91,22 +82,9 @@ impl Transpiler {
result += format!( result += format!(
"fn {}({}) -> {} {}", "fn {}({}) -> {} {}",
ast.tokens[1].value, ast.tokens[1].value,
self.transpile_round( self.transpile_round(ast.tokens[2].value.clone(),),
ast.tokens[2].value.clone(),
LexerState {
line: ast.tokens[2].line,
column: ast.tokens[2].column
}
),
ast.tokens[0].value, ast.tokens[0].value,
self.transpile( self.transpile(ast.tokens[3].value.clone(), indent + 1)
ast.tokens[3].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[3].line,
column: ast.tokens[3].column
}
)
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::VoidFunctionDeceleration { } else if ast.ast_type == AstType::VoidFunctionDeceleration {
@@ -116,21 +94,8 @@ impl Transpiler {
result += format!( result += format!(
"fn {}({}) {}", "fn {}({}) {}",
ast.tokens[1].value, ast.tokens[1].value,
self.transpile_round( self.transpile_round(ast.tokens[2].value.clone(),),
ast.tokens[2].value.clone(), self.transpile(ast.tokens[3].value.clone(), indent + 1,)
LexerState {
line: ast.tokens[2].line,
column: ast.tokens[2].column
}
),
self.transpile(
ast.tokens[3].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[3].line,
column: ast.tokens[3].column
},
)
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::StructDeceleration { } else if ast.ast_type == AstType::StructDeceleration {
@@ -141,13 +106,7 @@ impl Transpiler {
"struct {} {} {}", "struct {} {} {}",
ast.tokens[0].value, ast.tokens[0].value,
"{\n", "{\n",
self.transpile_round( self.transpile_round(ast.tokens[1].value.clone(),)
ast.tokens[1].value.clone(),
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
}
)
.trim_end() .trim_end()
) )
.replace( .replace(
@@ -158,36 +117,27 @@ impl Transpiler {
.as_str(); .as_str();
result += "\n}\n"; result += "\n}\n";
} else if ast.ast_type == AstType::VariableDeceleration { } else if ast.ast_type == AstType::VariableDeceleration {
variables.insert(ast.tokens[1].value.clone(), Var {vtype: VarTypes::Var, state: LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column,
}});
if self.clone().auto_mut { if self.clone().auto_mut {
result += result +=
format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value) format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str(); .as_str();
} else { } else {
result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value) result +=
format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str(); .as_str();
} }
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value) result +=
format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str(); .as_str();
} else if ast.ast_type == AstType::Other } else if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Round && ast.tokens[0].token_type == TokenType::Round
{ {
result += format!( result +=
"({})", format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
self.transpile_round(
ast.tokens[0].value.clone(),
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.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
{
result += format!( result += format!(
"[{}]", "[{}]",
self.transpile_square( self.transpile_square(
@@ -203,14 +153,16 @@ impl Transpiler {
result += "{"; result += "{";
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
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 { // if ast.tokens[0].token_type == TokenType::Newline {
// result += (ast.tokens[0].value.as_str().to_owned() // result += (ast.tokens[0].value.as_str().to_owned()
// + (" ".repeat((indent as usize) * 2).as_str())) // + (" ".repeat((indent as usize) * 2).as_str()))
// .as_str(); // .as_str();
// } else { // } else {
// result += ast.tokens[0].value.as_str(); // result += ast.tokens[0].value.as_str();
result += self.transpile_json( result += self
.transpile_json(
ast.tokens[0].value.as_str().to_string(), ast.tokens[0].value.as_str().to_string(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -259,35 +211,15 @@ impl Transpiler {
result += format!( result += format!(
"{} {} {}", "{} {} {}",
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
self.transpile_round( self.transpile_round(ast.tokens[1].value.clone(),),
ast.tokens[1].value.clone(), self.transpile(ast.tokens[2].value.clone(), indent + 1,),
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
}
),
self.transpile(
ast.tokens[2].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[2].line,
column: ast.tokens[2].column
}
),
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::State2 { } else if ast.ast_type == AstType::State2 {
result += format!( result += format!(
"{} {}", "{} {}",
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
self.transpile( self.transpile(ast.tokens[1].value.clone(), indent + 1,),
ast.tokens[1].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
},
),
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::Namespace { } else if ast.ast_type == AstType::Namespace {
@@ -295,11 +227,7 @@ impl Transpiler {
"mod {} {}{}{}", "mod {} {}{}{}",
&ast.tokens[0].value.clone(), &ast.tokens[0].value.clone(),
"{", "{",
self.transpile( self.transpile(ast.tokens[1].value.clone(), 0),
ast.tokens[1].value.clone(),
0,
state,
),
"}" "}"
) )
.as_str(); .as_str();
@@ -308,21 +236,23 @@ impl Transpiler {
"impl {} {}{}{}", "impl {} {}{}{}",
&ast.tokens[0].value.clone(), &ast.tokens[0].value.clone(),
"{", "{",
self.transpile( self.transpile(ast.tokens[1].value.clone(), 0,),
ast.tokens[1].value.clone(),
0,
state,
),
"}" "}"
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::PointerDeceleration { } else if ast.ast_type == AstType::PointerDeceleration {
if self.auto_mut { if self.auto_mut {
result += result += format!(
format!("let mut {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); "let mut {}: &mut {}",
ast.tokens[1].value, ast.tokens[0].value
)
.as_str();
} else { } else {
result += result += format!(
format!("let {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); "let {}: &mut {}",
ast.tokens[1].value, ast.tokens[0].value
)
.as_str();
} }
} }
// flp // flp
@@ -360,14 +290,16 @@ impl Transpiler {
result = result.trim_end().to_string(); result = result.trim_end().to_string();
if self.var_match != "" && (state.line + input.len()) > self.var_state.line { if self.var_match != "" && (self.state.line + input.len()) > self.var_state.line {
for (name, var) in variables { // for (name, var) in variables {
if (self.var_state.line > var.state.line || // if (self.var_state.line > var.line
(self.var_state.line == var.state.line && self.var_state.column > var.state.column)) && // || (self.var_state.line == var.line
name.to_lowercase().starts_with(&self.var_match) { // && self.var_state.column > var.column))
self.matched_vars.insert(name, var); // && name.to_lowercase().starts_with(&self.var_match)
} // {
} // self.matched_vars.insert(name, var);
// }
// }
self.var_match = String::new(); self.var_match = String::new();
} }
@@ -387,16 +319,12 @@ impl Transpiler {
pub fn transpile_mod(&mut self, ast: Ast, s: &str) -> String { pub fn transpile_mod(&mut self, ast: Ast, s: &str) -> String {
let modfile = ast.tokens[0].value.as_str(); let modfile = ast.tokens[0].value.as_str();
let modname = format!( let modname = format!(
"{}_{}", "mod_{}",
clean_incl(modfile.split(".").collect::<Vec<_>>()[0]), // clean_incl(modfile.split(".").collect::<Vec<_>>()[0]),
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( let transpiled_code = self.transpile(file_content, 0);
file_content,
0,
LexerState { line: 1, column: 0 }
);
fs::write( fs::write(
("build/".to_string() + modname.as_str()) + ".rs", ("build/".to_string() + modname.as_str()) + ".rs",
transpiled_code, transpiled_code,
@@ -404,19 +332,19 @@ impl Transpiler {
.expect("Error writing file"); .expect("Error writing file");
modname modname
} }
pub fn transpile_round(&mut self, input: String, state: LexerState) -> String { pub fn transpile_round(&mut self, input: 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, self.state);
let mut variables: HashMap<String, Variable> = HashMap::new();
match lexer_out { match lexer_out {
Ok(tokens) => { Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone()); let mut full_ast = Parser::new(tokens.clone(), variables);
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
}; };
while full_ast.tokens.len() > full_ast.index as usize { for ast in full_ast.parse() {
let ast = full_ast.next();
if last_ast.tokens.len() > 0 { if last_ast.tokens.len() > 0 {
let mut fl = 0; let mut fl = 0;
for t in &last_ast.tokens { for t in &last_ast.tokens {
@@ -438,26 +366,21 @@ impl Transpiler {
tokens: ast.tokens.clone(), tokens: ast.tokens.clone(),
}; };
if ast.ast_type == AstType::VariableDeceleration { if ast.ast_type == AstType::VariableDeceleration {
result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::PointerDeceleration {
result +=
format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round {
result += format!(
"({})",
self.transpile_round(
ast.tokens[0].value.clone(),
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.as_str(); .as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square { } else if ast.ast_type == AstType::PointerDeceleration {
result += format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
result += format!( result += format!(
"[{}]", "[{}]",
self.transpile_square( self.transpile_square(
@@ -471,8 +394,10 @@ impl Transpiler {
.as_str(); .as_str();
} 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.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly
result += self.transpile_json( {
result += self
.transpile_json(
ast.tokens[0].value.as_str().to_string(), ast.tokens[0].value.as_str().to_string(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -507,16 +432,16 @@ impl Transpiler {
pub fn transpile_square(&mut self, input: String, state: LexerState) -> String { pub fn transpile_square(&mut self, input: String, state: LexerState) -> 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);
let mut variables: HashMap<String, Variable> = HashMap::new();
match lexer_out { match lexer_out {
Ok(tokens) => { Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone()); let mut full_ast = Parser::new(tokens.clone(), variables);
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
}; };
while full_ast.tokens.len() > full_ast.index as usize { for ast in full_ast.parse() {
let ast = full_ast.next();
if last_ast.tokens.len() > 0 { if last_ast.tokens.len() > 0 {
let mut fl = 0; let mut fl = 0;
for t in &last_ast.tokens { for t in &last_ast.tokens {
@@ -539,23 +464,18 @@ impl Transpiler {
}; };
if ast.ast_type == AstType::VariableDeceleration { if ast.ast_type == AstType::VariableDeceleration {
result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::MutVariableDeceleration {
result += result +=
format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += format!( result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
"({})",
self.transpile_round(
ast.tokens[0].value.clone(),
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.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::Round
{
result +=
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
.as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
{
result += format!( result += format!(
"[{}]", "[{}]",
self.transpile_square( self.transpile_square(
@@ -569,8 +489,10 @@ impl Transpiler {
.as_str(); .as_str();
} 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.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly
result += self.transpile_json( {
result += self
.transpile_json(
ast.tokens[0].value.as_str().to_string(), ast.tokens[0].value.as_str().to_string(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -602,18 +524,18 @@ impl Transpiler {
pub fn transpile_json(&mut self, input: String, state: LexerState) -> String { pub fn transpile_json(&mut self, input: String, state: LexerState) -> 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);
let mut variables: HashMap<String, Variable> = HashMap::new();
match lexer_out { match lexer_out {
Ok(tokens) => { Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone()); let mut full_ast = Parser::new(tokens.clone(), variables);
full_ast.json = true; full_ast.json = true;
result += "HashMap::from(["; result += "HashMap::from([";
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
}; };
while full_ast.tokens.len() > full_ast.index as usize { for ast in full_ast.parse() {
let ast = full_ast.next();
if last_ast.tokens.len() > 0 { if last_ast.tokens.len() > 0 {
let mut fl = 0; let mut fl = 0;
for t in &last_ast.tokens { for t in &last_ast.tokens {
@@ -641,19 +563,13 @@ impl Transpiler {
result += ","; result += ",";
result += ast.tokens[1].value.as_str(); result += ast.tokens[1].value.as_str();
result += ")"; result += ")";
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
result += format!( {
"({})", result +=
self.transpile_round( format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
ast.tokens[0].value.clone(),
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.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
{
result += format!( result += format!(
"[{}]", "[{}]",
self.transpile_square( self.transpile_square(
@@ -665,8 +581,10 @@ impl Transpiler {
) )
) )
.as_str(); .as_str();
} 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
result += self.transpile_json( {
result += self
.transpile_json(
ast.tokens[0].value.as_str().to_string(), ast.tokens[0].value.as_str().to_string(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -689,15 +607,15 @@ impl Transpiler {
} }
} }
fn clean_incl(input: &str) -> String { // fn clean_incl(input: &str) -> String {
input // input
.chars() // .chars()
.map(|c| { // .map(|c| {
if c.is_alphanumeric() || c == '_' { // if c.is_alphanumeric() || c == '_' {
c // c
} else { // } else {
'_' // '_'
} // }
}) // })
.collect() // .collect()
} // }