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);
} }
+12 -11
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,22 +27,24 @@ 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(
.expect("Error writing to Rust file"); &transpiled_code,
("build/".to_string() + rust_file_name).as_str(),
)
.expect("Error writing to Rust file");
println!("Transpiled code written to {}", rust_file_name); println!("Transpiled code written to {}", rust_file_name);
} }
None => { None => {
+179 -149
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,181 +64,211 @@ 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 ast_res: Ast = Ast { let mut full_ast: Vec<Ast> = Vec::new();
tokens: vec![], while self.tokens.len() > self.index as usize {
ast_type: AstType::Other, let mut ast_res: Ast = Ast {
}; tokens: vec![],
let index = self.index as usize; ast_type: AstType::Other,
if index == self.tokens.len() { };
panic!("Reached the end of tokens") let index = self.index as usize;
} if index == self.tokens.len() {
let token = &self.tokens[index]; panic!("Reached the end of tokens")
if self.json }
&& self.tokens.len() - (self.index as usize) > 2 let token = &self.tokens[index];
&& self.tokens[index + 1].value == ":" if self.json
{ && self.tokens.len() - (self.index as usize) > 2
ast_res.ast_type = AstType::Json; && self.tokens[index + 1].value == ":"
ast_res.tokens.push(self.tokens[index].clone()); {
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.ast_type = AstType::Json;
self.index += 2; ast_res.tokens.push(self.tokens[index].clone());
} else if self.tokens.len() - index > 1 ast_res.tokens.push(self.tokens[index + 2].clone());
&& self.tokens[index].value == "&" self.index += 2;
&& self.tokens[index + 1].token_type == TokenType::Identifier { } else if self.tokens.len() - index > 1
&& self.tokens[index].value == "&"
&& 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;
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index].value == "struct" && self.tokens[index].value == "struct"
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly && self.tokens[index + 2].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::StructDeceleration; ast_res.ast_type = AstType::StructDeceleration;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index].value == "namespace" && self.tokens[index].value == "namespace"
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly && self.tokens[index + 2].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::Namespace; ast_res.ast_type = AstType::Namespace;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index].value == "impl" && self.tokens[index].value == "impl"
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly && self.tokens[index + 2].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::Impl; ast_res.ast_type = AstType::Impl;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 2 } else if self.tokens.len() - index > 2
&& self.tokens[index].token_type == TokenType::Keyword1 && self.tokens[index].token_type == TokenType::Keyword1
&& self.tokens[index + 1].token_type == TokenType::Round && self.tokens[index + 1].token_type == TokenType::Round
&& self.tokens[index + 2].token_type == TokenType::Curly && self.tokens[index + 2].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::State3; ast_res.ast_type = AstType::State3;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 1 } else if self.tokens.len() - index > 1
&& self.tokens[index].token_type == TokenType::Keyword2 && self.tokens[index].token_type == TokenType::Keyword2
&& self.tokens[index + 1].token_type == TokenType::Curly && self.tokens[index + 1].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index + 1].clone()); ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::State2; ast_res.ast_type = AstType::State2;
self.index += 1; self.index += 1;
} else { } else {
match token.token_type { match token.token_type {
TokenType::Identifier => { TokenType::Identifier => {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
if self.tokens.len() - index > 3 if self.tokens.len() - index > 3
&& self.tokens[index + 1].token_type == TokenType::Identifier && self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Round && self.tokens[index + 2].token_type == TokenType::Round
&& self.tokens[index + 3].token_type == TokenType::Curly && self.tokens[index + 3].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens.push(self.tokens[index + 3].clone());
if token.value == "void" {
ast_res.ast_type = AstType::VoidFunctionDeceleration;
} else {
ast_res.ast_type = AstType::FunctionDeceleration;
}
self.index += 3;
} else if self.tokens.len() - index > 1
&& self.tokens[index + 1].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::StructCall;
self.index += 1;
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::StructVar;
self.index += 2;
} else if self.tokens.len() - index > 1 {
if 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::VariableDeceleration; ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens.push(self.tokens[index + 3].clone());
if token.value == "void" {
ast_res.ast_type = AstType::VoidFunctionDeceleration;
} else {
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;
} else if self.tokens.len() - index > 1
&& self.tokens[index + 1].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::StructCall;
self.index += 1; self.index += 1;
} 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 + 1].token_type == TokenType::Identifier
&& self.tokens[index + 1].token_type == TokenType::Angle && self.tokens[index + 2].token_type == TokenType::Curly
{ {
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens[0].value += "<"; ast_res.ast_type = AstType::StructVar;
ast_res.tokens[0].value += self.tokens[index + 1].value.as_str();
ast_res.tokens[0].value += ">";
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 2;
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*"
&& self.tokens[index + 2].token_type == TokenType::Identifier {
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::PointerDeceleration;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 1 {
if self.tokens[index + 1].token_type == TokenType::Identifier {
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 1;
} else if self.tokens.len() - index > 2
&& self.tokens[index + 2].token_type == TokenType::Identifier
&& self.tokens[index + 1].token_type == TokenType::Angle
{
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens[0].value += "<";
ast_res.tokens[0].value += self.tokens[index + 1].value.as_str();
ast_res.tokens[0].value += ">";
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 2;
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*"
&& self.tokens[index + 2].token_type == TokenType::Identifier
{
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::PointerDeceleration;
self.index += 2;
}
} }
} }
} TokenType::Include => {
TokenType::Include => { if let Some(caps) = self.include_regex.captures(&token.value) {
if let Some(caps) = self.include_regex.captures(&token.value) { ast_res.tokens.push(Token {
ast_res.tokens.push(Token { token_type: TokenType::String,
token_type: TokenType::String, value: caps[2].to_owned().to_string(),
value: caps[2].to_owned().to_string(), line: 0,
line: 0, column: 0,
column: 0, });
}); ast_res.ast_type = AstType::Include;
ast_res.ast_type = AstType::Include; } else if let Some(caps) = self.include_regex_local.captures(&token.value) {
} else if let Some(caps) = self.include_regex_local.captures(&token.value) { ast_res.tokens.push(Token {
ast_res.tokens.push(Token { token_type: TokenType::String,
token_type: TokenType::String, value: caps[2].to_owned().to_string(),
value: caps[2].to_owned().to_string(), line: 0,
line: 0, column: 0,
column: 0, });
}); ast_res.ast_type = AstType::IncludeLocal;
ast_res.ast_type = AstType::IncludeLocal; } else {
} else { ast_res.tokens.push(token.clone());
ast_res.tokens.push(token.clone()); ast_res.ast_type = AstType::Include;
ast_res.ast_type = AstType::Include; }
} }
} TokenType::Keyword => {
TokenType::Keyword => { if token.value == "cb"
if token.value == "cb" && self.tokens[index + 1].token_type == TokenType::Curly && 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;
self.index += 1; self.index += 1;
} else { } else {
ast_res.tokens.push(token.clone());
}
}
_ => {
ast_res.tokens.push(token.clone()); ast_res.tokens.push(token.clone());
} }
} }
_ => {
ast_res.tokens.push(token.clone());
}
} }
self.index += 1;
full_ast.push(ast_res);
} }
self.index += 1; full_ast
ast_res
} }
} }
+154 -236
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,14 +106,8 @@ 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(), .trim_end()
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
}
)
.trim_end()
) )
.replace( .replace(
"\n", "\n",
@@ -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 +=
.as_str(); format!("let {}: {}", 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 += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value) result +=
.as_str(); format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.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( .as_str();
ast.tokens[0].value.clone(), } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
LexerState { {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.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(
@@ -203,21 +153,23 @@ 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
ast.tokens[0].value.as_str().to_string(), .transpile_json(
LexerState { ast.tokens[0].value.as_str().to_string(),
line: ast.tokens[0].line, LexerState {
column: ast.tokens[0].column, 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::Ptr { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
result += "."; result += ".";
@@ -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(); .as_str();
} else if ast.ast_type == AstType::PointerDeceleration { } 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 += result +=
format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round { .as_str();
result += format!( } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
"({})", {
self.transpile_round(
ast.tokens[0].value.clone(),
LexerState {
line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
)
.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,15 +394,17 @@ 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( {
ast.tokens[0].value.as_str().to_string(), result += self
LexerState { .transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column, LexerState {
}, line: ast.tokens[0].line,
) column: ast.tokens[0].column,
.as_str(); },
)
.as_str();
} else if ast.ast_type == AstType::StructCall { } else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " {"; result += " {";
@@ -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)
"({})", .as_str();
self.transpile_round( } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Round
ast.tokens[0].value.clone(), {
LexerState { result +=
line: ast.tokens[0].line, format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
column: ast.tokens[0].column .as_str();
} } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
) {
)
.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,15 +489,17 @@ 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( {
ast.tokens[0].value.as_str().to_string(), result += self
LexerState { .transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column, LexerState {
}, line: ast.tokens[0].line,
) column: ast.tokens[0].column,
.as_str(); },
)
.as_str();
} else if ast.ast_type == AstType::StructCall { } else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " {"; result += " {";
@@ -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(), .as_str();
LexerState { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Square
line: ast.tokens[0].line, {
column: ast.tokens[0].column
}
)
)
.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(
@@ -665,15 +581,17 @@ 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( {
ast.tokens[0].value.as_str().to_string(), result += self
LexerState { .transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column, LexerState {
}, line: ast.tokens[0].line,
) column: ast.tokens[0].column,
.as_str(); },
)
.as_str();
} else { } else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
} }
@@ -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()
} // }