alpha test
This commit is contained in:
+36
-10
@@ -1,8 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
use lspower::jsonrpc::Result;
|
||||
use lspower::lsp::{self, *};
|
||||
use lspower::{Client, LanguageServer, LspService, Server};
|
||||
use lspower::{Client, LanguageServer};
|
||||
use lspower;
|
||||
|
||||
use crate::lexer::LexerState;
|
||||
use crate::transpiler::Transpiler;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Backend {
|
||||
client: Client,
|
||||
@@ -44,14 +50,34 @@ impl LanguageServer for Backend {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn run_lsp_server() {
|
||||
let stdin = tokio::io::stdin();
|
||||
let stdout = tokio::io::stdout();
|
||||
// #[tokio::main]
|
||||
// pub async fn run_lsp_server() {
|
||||
// let stdin = tokio::io::stdin();
|
||||
// let stdout = tokio::io::stdout();
|
||||
|
||||
let (service, messages) = LspService::new(|client| Backend { client });
|
||||
Server::new(stdin, stdout)
|
||||
.interleave(messages)
|
||||
.serve(service)
|
||||
.await;
|
||||
// let (service, messages) = LspService::new(|client| Backend { client });
|
||||
// Server::new(stdin, stdout)
|
||||
// .interleave(messages)
|
||||
// .serve(service)
|
||||
// .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
@@ -6,7 +6,7 @@ mod lsp;
|
||||
use std::{fs, path::Path};
|
||||
use clap::Parser;
|
||||
use lexer::LexerState;
|
||||
use transpiler::Options;
|
||||
use transpiler::Transpiler;
|
||||
|
||||
use crate::lsp::run_lsp_server;
|
||||
|
||||
@@ -36,7 +36,8 @@ fn main() {
|
||||
fs::remove_dir_all("build").expect("err rm 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 {
|
||||
Some(ref rust_file_name) => {
|
||||
|
||||
+120
-93
@@ -1,65 +1,50 @@
|
||||
use crate::lexer::{lex, LexerState, TokenType};
|
||||
use crate::parser::{Ast, AstType, Parser};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Options {
|
||||
auto_mut: bool,
|
||||
auto_macro: bool,
|
||||
auto_pub: bool,
|
||||
macros: Vec<String>,
|
||||
modnum: u32,
|
||||
pub enum VarTypes {
|
||||
Var
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
fn default() -> Options {
|
||||
Options {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Var {
|
||||
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_macro: true,
|
||||
auto_pub: false,
|
||||
macros: vec![String::from("println")],
|
||||
modnum: 0,
|
||||
var_state: LexerState { line: 0, column: 0 },
|
||||
var_match: String::new(),
|
||||
matched_vars: HashMap::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clean_incl(input: &str) -> String {
|
||||
input
|
||||
.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 {
|
||||
impl Transpiler {
|
||||
pub fn transpile(&mut self, input: String, indent: u32, state: LexerState) -> String {
|
||||
let mut result = String::new();
|
||||
let mut variables: HashMap<String, Var> = HashMap::new();
|
||||
|
||||
if indent == 0 {
|
||||
// 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 options.auto_pub {
|
||||
if self.auto_pub {
|
||||
result += "pub ";
|
||||
}
|
||||
result += format!(
|
||||
"fn {}({}) -> {} {}",
|
||||
ast.tokens[1].value,
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[2].value.clone(),
|
||||
LexerState {
|
||||
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,
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[3].value.clone(),
|
||||
indent + 1,
|
||||
LexerState {
|
||||
line: ast.tokens[3].line,
|
||||
column: ast.tokens[3].column
|
||||
},
|
||||
&mut options.clone()
|
||||
}
|
||||
)
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
|
||||
if options.auto_pub {
|
||||
if self.auto_pub {
|
||||
result += "pub ";
|
||||
}
|
||||
result += format!(
|
||||
"fn {}({}) {}",
|
||||
ast.tokens[1].value,
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[2].value.clone(),
|
||||
LexerState {
|
||||
line: ast.tokens[2].line,
|
||||
column: ast.tokens[2].column
|
||||
}
|
||||
),
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[3].value.clone(),
|
||||
indent + 1,
|
||||
LexerState {
|
||||
line: ast.tokens[3].line,
|
||||
column: ast.tokens[3].column
|
||||
},
|
||||
&mut options.clone()
|
||||
)
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.ast_type == AstType::StructDeceleration {
|
||||
if options.auto_pub {
|
||||
if self.auto_pub {
|
||||
result += "pub ";
|
||||
}
|
||||
result += format!(
|
||||
"struct {} {} {}",
|
||||
ast.tokens[0].value,
|
||||
"{\n",
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[1].value.clone(),
|
||||
LexerState {
|
||||
line: ast.tokens[1].line,
|
||||
@@ -175,7 +158,11 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
.as_str();
|
||||
result += "\n}\n";
|
||||
} 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 +=
|
||||
format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
|
||||
.as_str();
|
||||
@@ -191,7 +178,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
{
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(
|
||||
self.transpile_square(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -223,7 +210,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
// .as_str();
|
||||
// } else {
|
||||
// result += ast.tokens[0].value.as_str();
|
||||
result += transpile_json(
|
||||
result += self.transpile_json(
|
||||
ast.tokens[0].value.as_str().to_string(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -251,42 +238,41 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.ast_type == AstType::Include {
|
||||
let modname = transpile_mod(ast, options, "lib/");
|
||||
let modname = self.transpile_mod(ast, "lib/");
|
||||
result += "mod ";
|
||||
result += modname.as_str();
|
||||
result += ";\n";
|
||||
result += "use ";
|
||||
result += modname.as_str();
|
||||
result += "::*;\n";
|
||||
options.modnum += 1;
|
||||
self.modnum += 1;
|
||||
} else if ast.ast_type == AstType::IncludeLocal {
|
||||
let modname = transpile_mod(ast, options, "");
|
||||
let modname = self.transpile_mod(ast, "");
|
||||
result += "mod ";
|
||||
result += modname.as_str();
|
||||
result += ";\n";
|
||||
result += "use ";
|
||||
result += modname.as_str();
|
||||
result += "::*;\n";
|
||||
options.modnum += 1;
|
||||
self.modnum += 1;
|
||||
} else if ast.ast_type == AstType::State3 {
|
||||
result += format!(
|
||||
"{} {} {}",
|
||||
ast.tokens[0].value.clone(),
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[1].value.clone(),
|
||||
LexerState {
|
||||
line: ast.tokens[1].line,
|
||||
column: ast.tokens[1].column
|
||||
}
|
||||
),
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[2].value.clone(),
|
||||
indent + 1,
|
||||
LexerState {
|
||||
line: ast.tokens[2].line,
|
||||
column: ast.tokens[2].column
|
||||
},
|
||||
options
|
||||
}
|
||||
),
|
||||
)
|
||||
.as_str();
|
||||
@@ -294,14 +280,13 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
result += format!(
|
||||
"{} {}",
|
||||
ast.tokens[0].value.clone(),
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[1].value.clone(),
|
||||
indent + 1,
|
||||
LexerState {
|
||||
line: ast.tokens[1].line,
|
||||
column: ast.tokens[1].column
|
||||
},
|
||||
options
|
||||
),
|
||||
)
|
||||
.as_str();
|
||||
@@ -310,11 +295,10 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
"mod {} {}{}{}",
|
||||
&ast.tokens[0].value.clone(),
|
||||
"{",
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[1].value.clone(),
|
||||
0,
|
||||
state,
|
||||
options,
|
||||
),
|
||||
"}"
|
||||
)
|
||||
@@ -324,17 +308,16 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
"impl {} {}{}{}",
|
||||
&ast.tokens[0].value.clone(),
|
||||
"{",
|
||||
transpile(
|
||||
self.transpile(
|
||||
ast.tokens[1].value.clone(),
|
||||
0,
|
||||
state,
|
||||
options,
|
||||
),
|
||||
"}"
|
||||
)
|
||||
.as_str();
|
||||
} else if ast.ast_type == AstType::PointerDeceleration {
|
||||
if options.auto_mut {
|
||||
if self.auto_mut {
|
||||
result +=
|
||||
format!("let mut {}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
|
||||
} else {
|
||||
@@ -364,8 +347,8 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
// }
|
||||
// }
|
||||
result += ast.tokens[0].value.as_str();
|
||||
if options.auto_macro
|
||||
&& options
|
||||
if self.auto_macro
|
||||
&& self
|
||||
.macros
|
||||
.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();
|
||||
|
||||
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 {
|
||||
result += "\n";
|
||||
result += " ".repeat((indent as usize - 1) * 2).as_str();
|
||||
@@ -389,9 +383,28 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
}
|
||||
pub fn transpile_mod(&mut self, ast: Ast, s: &str) -> 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 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 {
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(
|
||||
self.transpile_square(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += ".";
|
||||
} 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(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -489,9 +502,9 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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 {
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(
|
||||
self.transpile_square(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += ".";
|
||||
} 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(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -584,9 +597,9 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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 {
|
||||
result += format!(
|
||||
"({})",
|
||||
transpile_round(
|
||||
self.transpile_round(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
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 {
|
||||
result += format!(
|
||||
"[{}]",
|
||||
transpile_square(
|
||||
self.transpile_square(
|
||||
ast.tokens[0].value.clone(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -653,7 +666,7 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
|
||||
)
|
||||
.as_str();
|
||||
} 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(),
|
||||
LexerState {
|
||||
line: ast.tokens[0].line,
|
||||
@@ -673,4 +686,18 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
|
||||
panic!("Invalid syntax at code.ws:{}:{}", state.line, state.column);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clean_incl(input: &str) -> String {
|
||||
input
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_alphanumeric() || c == '_' {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user