alpha test

This commit is contained in:
2024-06-03 20:10:35 +02:00
parent 0b513c49c3
commit 15ff863fbe
3 changed files with 715 additions and 661 deletions
+36 -10
View File
@@ -1,8 +1,14 @@
use std::collections::HashMap;
use std::fs;
use lspower::jsonrpc::Result; use lspower::jsonrpc::Result;
use lspower::lsp::{self, *}; use lspower::lsp::{self, *};
use lspower::{Client, LanguageServer, LspService, Server}; use lspower::{Client, LanguageServer};
use lspower; use lspower;
use crate::lexer::LexerState;
use crate::transpiler::Transpiler;
#[derive(Debug)] #[derive(Debug)]
struct Backend { struct Backend {
client: Client, client: Client,
@@ -44,14 +50,34 @@ impl LanguageServer for Backend {
} }
} }
#[tokio::main] // #[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();
let (service, messages) = LspService::new(|client| Backend { client }); // let (service, messages) = LspService::new(|client| Backend { client });
Server::new(stdin, stdout) // Server::new(stdin, stdout)
.interleave(messages) // .interleave(messages)
.serve(service) // .serve(service)
.await; // .await;
// }
pub fn run_lsp_server() {
let mut options = Transpiler {
auto_mut: true,
auto_macro: true,
auto_pub: false,
macros: vec![String::from("println")],
modnum: 0,
var_match: String::from("x"),
var_state: LexerState { line: 4, column: 4 },
matched_vars: HashMap::new()
};
let file_content = fs::read_to_string("main.wt")
.expect("Error reading file");
let _ = options.transpile(file_content.to_string(),
0,
LexerState { line: 1, column: 0 },
);
println!("{:?}", options.matched_vars);
} }
+3 -2
View File
@@ -6,7 +6,7 @@ mod lsp;
use std::{fs, path::Path}; use std::{fs, path::Path};
use clap::Parser; use clap::Parser;
use lexer::LexerState; use lexer::LexerState;
use transpiler::Options; use transpiler::Transpiler;
use crate::lsp::run_lsp_server; use crate::lsp::run_lsp_server;
@@ -36,7 +36,8 @@ fn main() {
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 transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }, &mut Options::default()); let mut trsp = Transpiler::default();
let transpiled_code = trsp.transpile(file_content, 0, LexerState { line: 1, column: 0 });
match args.rust { match args.rust {
Some(ref rust_file_name) => { Some(ref rust_file_name) => {
+117 -90
View File
@@ -1,65 +1,50 @@
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser}; use crate::parser::{Ast, AstType, Parser};
use std::collections::HashMap;
use std::fs; use std::fs;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Options { pub enum VarTypes {
auto_mut: bool, Var
auto_macro: bool,
auto_pub: bool,
macros: Vec<String>,
modnum: u32,
} }
impl Default for Options { #[derive(Debug, PartialEq, Clone)]
fn default() -> Options { pub struct Var {
Options { vtype: VarTypes,
state: LexerState
}
#[derive(Debug, PartialEq, Clone)]
pub struct Transpiler {
pub auto_mut: bool,
pub auto_macro: bool,
pub auto_pub: bool,
pub macros: Vec<String>,
pub modnum: u32,
pub var_match: String,
pub var_state: LexerState,
pub matched_vars: HashMap<String, Var>,
}
impl Default for Transpiler {
fn default() -> Transpiler {
Transpiler {
auto_mut: true, auto_mut: true,
auto_macro: true, auto_macro: true,
auto_pub: false, auto_pub: false,
macros: vec![String::from("println")], macros: vec![String::from("println")],
modnum: 0, modnum: 0,
var_state: LexerState { line: 0, column: 0 },
var_match: String::new(),
matched_vars: HashMap::new()
} }
} }
} }
fn clean_incl(input: &str) -> String { impl Transpiler {
input pub fn transpile(&mut self, input: String, indent: u32, state: LexerState) -> String {
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '_' {
c
} else {
'_'
}
})
.collect()
}
pub fn transpile_mod(ast: Ast, options: &mut Options, s: &str) -> String {
let modfile = ast.tokens[0].value.as_str();
let modname = format!(
"{}_{}",
clean_incl(modfile.split(".").collect::<Vec<_>>()[0]),
options.clone().modnum
);
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
let transpiled_code = transpile(
file_content,
0,
LexerState { line: 1, column: 0 },
&mut Options::default(),
);
fs::write(
("build/".to_string() + modname.as_str()) + ".rs",
transpiled_code,
)
.expect("Error writing file");
modname
}
pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Options) -> String {
let mut result = String::new(); let mut result = String::new();
let mut variables: HashMap<String, Var> = HashMap::new();
if indent == 0 { if indent == 0 {
// result += "type int = i32;\n"; // result += "type int = i32;\n";
@@ -100,13 +85,13 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
}; };
if ast.ast_type == AstType::FunctionDeceleration { if ast.ast_type == AstType::FunctionDeceleration {
if options.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
result += format!( result += format!(
"fn {}({}) -> {} {}", "fn {}({}) -> {} {}",
ast.tokens[1].value, ast.tokens[1].value,
transpile_round( self.transpile_round(
ast.tokens[2].value.clone(), ast.tokens[2].value.clone(),
LexerState { LexerState {
line: ast.tokens[2].line, line: ast.tokens[2].line,
@@ -114,51 +99,49 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
} }
), ),
ast.tokens[0].value, ast.tokens[0].value,
transpile( self.transpile(
ast.tokens[3].value.clone(), ast.tokens[3].value.clone(),
indent + 1, indent + 1,
LexerState { LexerState {
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
}, }
&mut options.clone()
) )
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::VoidFunctionDeceleration { } else if ast.ast_type == AstType::VoidFunctionDeceleration {
if options.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
result += format!( result += format!(
"fn {}({}) {}", "fn {}({}) {}",
ast.tokens[1].value, ast.tokens[1].value,
transpile_round( self.transpile_round(
ast.tokens[2].value.clone(), ast.tokens[2].value.clone(),
LexerState { LexerState {
line: ast.tokens[2].line, line: ast.tokens[2].line,
column: ast.tokens[2].column column: ast.tokens[2].column
} }
), ),
transpile( self.transpile(
ast.tokens[3].value.clone(), ast.tokens[3].value.clone(),
indent + 1, indent + 1,
LexerState { LexerState {
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
}, },
&mut options.clone()
) )
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::StructDeceleration { } else if ast.ast_type == AstType::StructDeceleration {
if options.auto_pub { if self.auto_pub {
result += "pub "; result += "pub ";
} }
result += format!( result += format!(
"struct {} {} {}", "struct {} {} {}",
ast.tokens[0].value, ast.tokens[0].value,
"{\n", "{\n",
transpile_round( self.transpile_round(
ast.tokens[1].value.clone(), ast.tokens[1].value.clone(),
LexerState { LexerState {
line: ast.tokens[1].line, line: ast.tokens[1].line,
@@ -175,7 +158,11 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
.as_str(); .as_str();
result += "\n}\n"; result += "\n}\n";
} else if ast.ast_type == AstType::VariableDeceleration { } else if ast.ast_type == AstType::VariableDeceleration {
if options.clone().auto_mut { 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 {
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();
@@ -191,7 +178,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
{ {
result += format!( result += format!(
"({})", "({})",
transpile_round( self.transpile_round(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -203,7 +190,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
} 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!(
"[{}]", "[{}]",
transpile_square( self.transpile_square(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -223,7 +210,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
// .as_str(); // .as_str();
// } else { // } else {
// result += ast.tokens[0].value.as_str(); // result += ast.tokens[0].value.as_str();
result += 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,
@@ -251,42 +238,41 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::Include { } else if ast.ast_type == AstType::Include {
let modname = transpile_mod(ast, options, "lib/"); let modname = self.transpile_mod(ast, "lib/");
result += "mod "; result += "mod ";
result += modname.as_str(); result += modname.as_str();
result += ";\n"; result += ";\n";
result += "use "; result += "use ";
result += modname.as_str(); result += modname.as_str();
result += "::*;\n"; result += "::*;\n";
options.modnum += 1; self.modnum += 1;
} else if ast.ast_type == AstType::IncludeLocal { } else if ast.ast_type == AstType::IncludeLocal {
let modname = transpile_mod(ast, options, ""); let modname = self.transpile_mod(ast, "");
result += "mod "; result += "mod ";
result += modname.as_str(); result += modname.as_str();
result += ";\n"; result += ";\n";
result += "use "; result += "use ";
result += modname.as_str(); result += modname.as_str();
result += "::*;\n"; result += "::*;\n";
options.modnum += 1; self.modnum += 1;
} else if ast.ast_type == AstType::State3 { } else if ast.ast_type == AstType::State3 {
result += format!( result += format!(
"{} {} {}", "{} {} {}",
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
transpile_round( self.transpile_round(
ast.tokens[1].value.clone(), ast.tokens[1].value.clone(),
LexerState { LexerState {
line: ast.tokens[1].line, line: ast.tokens[1].line,
column: ast.tokens[1].column column: ast.tokens[1].column
} }
), ),
transpile( self.transpile(
ast.tokens[2].value.clone(), ast.tokens[2].value.clone(),
indent + 1, indent + 1,
LexerState { LexerState {
line: ast.tokens[2].line, line: ast.tokens[2].line,
column: ast.tokens[2].column column: ast.tokens[2].column
}, }
options
), ),
) )
.as_str(); .as_str();
@@ -294,14 +280,13 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
result += format!( result += format!(
"{} {}", "{} {}",
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
transpile( self.transpile(
ast.tokens[1].value.clone(), ast.tokens[1].value.clone(),
indent + 1, indent + 1,
LexerState { LexerState {
line: ast.tokens[1].line, line: ast.tokens[1].line,
column: ast.tokens[1].column column: ast.tokens[1].column
}, },
options
), ),
) )
.as_str(); .as_str();
@@ -310,11 +295,10 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
"mod {} {}{}{}", "mod {} {}{}{}",
&ast.tokens[0].value.clone(), &ast.tokens[0].value.clone(),
"{", "{",
transpile( self.transpile(
ast.tokens[1].value.clone(), ast.tokens[1].value.clone(),
0, 0,
state, state,
options,
), ),
"}" "}"
) )
@@ -324,17 +308,16 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
"impl {} {}{}{}", "impl {} {}{}{}",
&ast.tokens[0].value.clone(), &ast.tokens[0].value.clone(),
"{", "{",
transpile( self.transpile(
ast.tokens[1].value.clone(), ast.tokens[1].value.clone(),
0, 0,
state, state,
options,
), ),
"}" "}"
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::PointerDeceleration { } else if ast.ast_type == AstType::PointerDeceleration {
if options.auto_mut { if self.auto_mut {
result += result +=
format!("let mut {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); format!("let mut {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else { } else {
@@ -364,8 +347,8 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
// } // }
// } // }
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
if options.auto_macro if self.auto_macro
&& options && self
.macros .macros
.contains(&ast.tokens[0].value.as_str().to_string()) .contains(&ast.tokens[0].value.as_str().to_string())
{ {
@@ -377,6 +360,17 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
result = result.trim_end().to_string(); result = result.trim_end().to_string();
if self.var_match != "" && (state.line + input.len()) > self.var_state.line {
for (name, var) in variables {
if (self.var_state.line > var.state.line ||
(self.var_state.line == var.state.line && self.var_state.column > var.state.column)) &&
name.to_lowercase().starts_with(&self.var_match) {
self.matched_vars.insert(name, var);
}
}
self.var_match = String::new();
}
if indent > 0 { if indent > 0 {
result += "\n"; result += "\n";
result += " ".repeat((indent as usize - 1) * 2).as_str(); result += " ".repeat((indent as usize - 1) * 2).as_str();
@@ -390,8 +384,27 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
} }
} }
} }
pub fn transpile_mod(&mut self, ast: Ast, s: &str) -> String {
pub fn transpile_round(input: String, state: LexerState) -> String { let modfile = ast.tokens[0].value.as_str();
let modname = format!(
"{}_{}",
clean_incl(modfile.split(".").collect::<Vec<_>>()[0]),
self.clone().modnum
);
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
let transpiled_code = self.transpile(
file_content,
0,
LexerState { line: 1, column: 0 }
);
fs::write(
("build/".to_string() + modname.as_str()) + ".rs",
transpiled_code,
)
.expect("Error writing file");
modname
}
pub fn transpile_round(&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);
@@ -435,7 +448,7 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
} 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 += format!(
"({})", "({})",
transpile_round( self.transpile_round(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -447,7 +460,7 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
} 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!(
"[{}]", "[{}]",
transpile_square( self.transpile_square(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -459,7 +472,7 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
} 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 += 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,
@@ -491,7 +504,7 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
} }
} }
pub fn transpile_square(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);
@@ -533,7 +546,7 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
} 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 += format!(
"({})", "({})",
transpile_round( self.transpile_round(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -545,7 +558,7 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
} 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!(
"[{}]", "[{}]",
transpile_square( self.transpile_square(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -557,7 +570,7 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
} 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 += 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,
@@ -586,7 +599,7 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
} }
} }
pub fn transpile_json(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);
@@ -631,7 +644,7 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
} 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 += format!(
"({})", "({})",
transpile_round( self.transpile_round(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -643,7 +656,7 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
} 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!(
"[{}]", "[{}]",
transpile_square( self.transpile_square(
ast.tokens[0].value.clone(), ast.tokens[0].value.clone(),
LexerState { LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
@@ -653,7 +666,7 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
) )
.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 += 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,
@@ -674,3 +687,17 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
} }
} }
} }
}
fn clean_incl(input: &str) -> String {
input
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '_' {
c
} else {
'_'
}
})
.collect()
}