get_completion works but lsp doesn't
This commit is contained in:
@@ -13,4 +13,3 @@ 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"
|
rand = "0.8.5"
|
||||||
lsp-client-rs = "0.1.0"
|
|
||||||
|
|||||||
+74
-17
@@ -1,17 +1,41 @@
|
|||||||
// use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
// use std::fs;
|
// use std::fs;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
|
|
||||||
use lspower::{self, LspService, Server};
|
use lspower::{self, LspService, Server};
|
||||||
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 rand::{thread_rng, Rng};
|
||||||
|
use crate::parser::{new_vars, Variable};
|
||||||
|
use crate::transpiler::Transpiler;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Backend {
|
struct Backend {
|
||||||
client: Client,
|
client: Client
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct DocumentManager {
|
||||||
|
documents: HashMap<String, String>, // Key: Document URI, Value: Document content
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DocumentManager {
|
||||||
|
fn new() -> DocumentManager {
|
||||||
|
DocumentManager {
|
||||||
|
documents: HashMap::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn update_document(&mut self, params: DidChangeTextDocumentParams) {
|
||||||
|
let uri = params.text_document.uri.to_string(); // Convert URI to string
|
||||||
|
let content = params.content_changes.last().unwrap().text.clone(); // Access latest content
|
||||||
|
self.documents.insert(uri, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_document(&self, uri: &str) -> Option<&str> {
|
||||||
|
self.documents.get(uri).map(|x| x.as_str())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[lspower::async_trait]
|
#[lspower::async_trait]
|
||||||
@@ -38,9 +62,23 @@ impl LanguageServer for Backend {
|
|||||||
|
|
||||||
async fn completion(
|
async fn completion(
|
||||||
&self,
|
&self,
|
||||||
_params: lsp::CompletionParams,
|
params: lsp::CompletionParams,
|
||||||
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
|
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
|
||||||
let completion_items: Vec<CompletionItem> = Vec::new();
|
// let document_manager = &self.document;
|
||||||
|
// let uri = params.text_document_position.text_document.uri.to_string();
|
||||||
|
// let document_content = document_manager.get_document(&uri).unwrap_or_default(); // Handle missing document
|
||||||
|
|
||||||
|
// let variables = get_completion(
|
||||||
|
// document_content.to_string(),
|
||||||
|
// params.text_document_position.position.line as usize + 1,
|
||||||
|
// params.text_document_position.position.character as usize,
|
||||||
|
// );
|
||||||
|
|
||||||
|
let mut completion_items: Vec<CompletionItem> = Vec::new();
|
||||||
|
// for (name, _) in variables {
|
||||||
|
// completion_items.push(CompletionItem::new_simple(name, "".to_string()));
|
||||||
|
// }
|
||||||
|
|
||||||
Ok(Some(CompletionResponse::Array(completion_items)))
|
Ok(Some(CompletionResponse::Array(completion_items)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,21 +92,40 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[tokio::main]
|
pub fn place_at(input: String, in2: String, line_goal: usize, column_goal: usize) -> String {
|
||||||
// pub async fn run_lsp_server() {
|
let mut line: usize = 1;
|
||||||
// let mut transpiler = Transpiler {
|
let mut column: usize = 0;
|
||||||
// var_match: String::from("x"),
|
let mut out = String::new();
|
||||||
// var_state: LexerState { line: 6, column: 9 },
|
for c in input.chars() {
|
||||||
// ..Default::default()
|
out+=c.to_string().as_str();
|
||||||
// };
|
column += 1;
|
||||||
// let file_content = fs::read_to_string("main.wt").expect("Error reading file");
|
if c == '\n' {
|
||||||
// transpiler.transpile(file_content, 0, HashMap::new());
|
line += 1;
|
||||||
// println!("{:?}", transpiler.matched_vars);
|
column = 0;
|
||||||
// }
|
} else if line == line_goal && column == column_goal {
|
||||||
|
out += in2.as_str();
|
||||||
|
column+=1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_completion(input: String, line: usize, column: usize) -> HashMap<String, Variable> {
|
||||||
|
let rand_id: u32 = thread_rng().gen();
|
||||||
|
let rand_id: String = rand_id.to_string();
|
||||||
|
let mut transpiler = Transpiler {
|
||||||
|
peek: rand_id.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
transpiler.transpile(
|
||||||
|
place_at(input, rand_id, line, column)
|
||||||
|
, 0, new_vars());
|
||||||
|
transpiler.matched_vars
|
||||||
|
}
|
||||||
+2
-1
@@ -4,6 +4,7 @@ mod lsp;
|
|||||||
mod parser;
|
mod parser;
|
||||||
mod transpiler;
|
mod transpiler;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use parser::new_vars;
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
use transpiler::Transpiler;
|
use transpiler::Transpiler;
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
fs::create_dir("build").expect("error making build");
|
fs::create_dir("build").expect("error making build");
|
||||||
let mut trsp = Transpiler::default();
|
let mut trsp = Transpiler::default();
|
||||||
let transpiled_code = trsp.transpile(file_content, 0);
|
let transpiled_code = trsp.transpile(file_content, 0, new_vars());
|
||||||
|
|
||||||
match args.rust {
|
match args.rust {
|
||||||
Some(ref rust_file_name) => {
|
Some(ref rust_file_name) => {
|
||||||
|
|||||||
+52
-4
@@ -1,7 +1,7 @@
|
|||||||
use crate::lexer::{Token, TokenType};
|
use crate::lexer::{LexerState, 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 {
|
||||||
@@ -63,17 +63,37 @@ pub struct Parser {
|
|||||||
pub index: u32,
|
pub index: u32,
|
||||||
pub include_regex: Lazy<Regex>,
|
pub include_regex: Lazy<Regex>,
|
||||||
pub include_regex_local: Lazy<Regex>,
|
pub include_regex_local: Lazy<Regex>,
|
||||||
|
pub variables: HashMap<String, Variable>,
|
||||||
pub json: bool,
|
pub json: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
|
pub enum VariableType {
|
||||||
|
Var,
|
||||||
|
Func,
|
||||||
|
Keyword,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Variable {
|
||||||
|
pub vtype: VariableType,
|
||||||
|
pub state: LexerState,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_vars() -> HashMap<String, Variable> {
|
||||||
|
HashMap::from([
|
||||||
|
("void".to_string(), Variable { vtype: VariableType::Keyword, state: LexerState {line:0,column:0}})
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
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,
|
variables: variables,
|
||||||
json: false,
|
json: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,6 +186,13 @@ impl Parser {
|
|||||||
} else {
|
} else {
|
||||||
ast_res.ast_type = AstType::FunctionDeceleration;
|
ast_res.ast_type = AstType::FunctionDeceleration;
|
||||||
}
|
}
|
||||||
|
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
|
||||||
|
vtype: VariableType::Func,
|
||||||
|
state: LexerState {
|
||||||
|
line: self.tokens[index + 1].clone().line,
|
||||||
|
column: self.tokens[index + 1].clone().column
|
||||||
|
}
|
||||||
|
});
|
||||||
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
|
||||||
@@ -186,6 +213,13 @@ impl Parser {
|
|||||||
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.ast_type = AstType::VariableDeceleration;
|
||||||
self.index += 1;
|
self.index += 1;
|
||||||
|
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
|
||||||
|
vtype: VariableType::Var,
|
||||||
|
state: LexerState {
|
||||||
|
line: self.tokens[index + 1].clone().line,
|
||||||
|
column: self.tokens[index + 1].clone().column
|
||||||
|
}
|
||||||
|
});
|
||||||
} else if self.tokens.len() - index > 2
|
} else if self.tokens.len() - index > 2
|
||||||
&& self.tokens[index + 2].token_type == TokenType::Identifier
|
&& self.tokens[index + 2].token_type == TokenType::Identifier
|
||||||
&& self.tokens[index + 1].token_type == TokenType::Angle
|
&& self.tokens[index + 1].token_type == TokenType::Angle
|
||||||
@@ -196,6 +230,13 @@ impl Parser {
|
|||||||
ast_res.tokens[0].value += ">";
|
ast_res.tokens[0].value += ">";
|
||||||
ast_res.ast_type = AstType::VariableDeceleration;
|
ast_res.ast_type = AstType::VariableDeceleration;
|
||||||
self.index += 2;
|
self.index += 2;
|
||||||
|
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
|
||||||
|
vtype: VariableType::Var,
|
||||||
|
state: LexerState {
|
||||||
|
line: self.tokens[index + 1].clone().line,
|
||||||
|
column: self.tokens[index + 1].clone().column
|
||||||
|
}
|
||||||
|
});
|
||||||
} 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
|
||||||
@@ -203,6 +244,13 @@ impl Parser {
|
|||||||
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;
|
||||||
|
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
|
||||||
|
vtype: VariableType::Var,
|
||||||
|
state: LexerState {
|
||||||
|
line: self.tokens[index + 1].clone().line,
|
||||||
|
column: self.tokens[index + 1].clone().column
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-33
@@ -1,8 +1,9 @@
|
|||||||
use crate::lexer::{lex, LexerState, TokenType};
|
use crate::lexer::{lex, LexerState, TokenType};
|
||||||
use crate::parser::{Ast, AstType, Parser};
|
use crate::parser::{new_vars, Ast, AstType, Parser, Variable, VariableType};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Transpiler {
|
pub struct Transpiler {
|
||||||
pub state: LexerState,
|
pub state: LexerState,
|
||||||
pub auto_mut: bool,
|
pub auto_mut: bool,
|
||||||
@@ -10,6 +11,8 @@ pub struct Transpiler {
|
|||||||
pub auto_pub: bool,
|
pub auto_pub: bool,
|
||||||
pub macros: Vec<String>,
|
pub macros: Vec<String>,
|
||||||
pub modnum: u32,
|
pub modnum: u32,
|
||||||
|
pub peek: String,
|
||||||
|
pub matched_vars: HashMap<String, Variable>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Transpiler {
|
impl Default for Transpiler {
|
||||||
@@ -21,12 +24,15 @@ impl Default for Transpiler {
|
|||||||
auto_pub: false,
|
auto_pub: false,
|
||||||
macros: vec![String::from("println")],
|
macros: vec![String::from("println")],
|
||||||
modnum: 0,
|
modnum: 0,
|
||||||
|
peek: String::new(),
|
||||||
|
matched_vars: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Transpiler {
|
impl Transpiler {
|
||||||
pub fn transpile(&mut self, input: String, indent: u32) -> String {
|
pub fn transpile(&mut self, input: String, indent: u32, variables: HashMap<String, Variable>) -> String {
|
||||||
|
let mut variables = variables;
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
if indent == 0 {
|
if indent == 0 {
|
||||||
// result += "type int = i32;\n";
|
// result += "type int = i32;\n";
|
||||||
@@ -37,12 +43,26 @@ impl Transpiler {
|
|||||||
|
|
||||||
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.clone());
|
||||||
let mut last_ast = Ast {
|
let mut last_ast = Ast {
|
||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
|
variables = full_ast.variables.clone();
|
||||||
|
if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Identifier
|
||||||
|
&& ast.tokens[0].value.contains(&self.peek) && self.peek!="" {
|
||||||
|
|
||||||
|
let ctoken = &ast.tokens[0];
|
||||||
|
let pname = ctoken.value.split(&self.peek).next().unwrap();
|
||||||
|
for (name, var) in variables.clone() {
|
||||||
|
if ctoken.line > var.state.line || var.vtype != VariableType::Var {
|
||||||
|
self.matched_vars.insert(name, var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.peek = String::new();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
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 {
|
||||||
@@ -71,9 +91,9 @@ impl Transpiler {
|
|||||||
result += format!(
|
result += format!(
|
||||||
"fn {}({}) -> {} {}",
|
"fn {}({}) -> {} {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(),),
|
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
||||||
ast.tokens[0].value,
|
ast.tokens[0].value,
|
||||||
self.transpile(ast.tokens[3].value.clone(), indent + 1)
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone())
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
|
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
|
||||||
@@ -83,8 +103,8 @@ impl Transpiler {
|
|||||||
result += format!(
|
result += format!(
|
||||||
"fn {}({}) {}",
|
"fn {}({}) {}",
|
||||||
ast.tokens[1].value,
|
ast.tokens[1].value,
|
||||||
self.transpile_round(ast.tokens[2].value.clone(),),
|
self.transpile_round(ast.tokens[2].value.clone(), variables.clone()),
|
||||||
self.transpile(ast.tokens[3].value.clone(), indent + 1,)
|
self.transpile(ast.tokens[3].value.clone(), indent + 1, variables.clone())
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructDeceleration {
|
} else if ast.ast_type == AstType::StructDeceleration {
|
||||||
@@ -95,7 +115,7 @@ impl Transpiler {
|
|||||||
"struct {} {} {}",
|
"struct {} {} {}",
|
||||||
ast.tokens[0].value,
|
ast.tokens[0].value,
|
||||||
"{\n",
|
"{\n",
|
||||||
self.transpile_round(ast.tokens[1].value.clone(),)
|
self.transpile_round(ast.tokens[1].value.clone(), variables.clone())
|
||||||
.trim_end()
|
.trim_end()
|
||||||
)
|
)
|
||||||
.replace(
|
.replace(
|
||||||
@@ -123,7 +143,7 @@ impl Transpiler {
|
|||||||
&& ast.tokens[0].token_type == TokenType::Round
|
&& ast.tokens[0].token_type == TokenType::Round
|
||||||
{
|
{
|
||||||
result +=
|
result +=
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
|
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
||||||
.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
|
||||||
{
|
{
|
||||||
@@ -134,7 +154,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}
|
}, variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -156,7 +176,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
},
|
}, variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
// }
|
// }
|
||||||
@@ -200,15 +220,15 @@ impl Transpiler {
|
|||||||
result += format!(
|
result += format!(
|
||||||
"{} {} {}",
|
"{} {} {}",
|
||||||
ast.tokens[0].value.clone(),
|
ast.tokens[0].value.clone(),
|
||||||
self.transpile_round(ast.tokens[1].value.clone(),),
|
self.transpile_round(ast.tokens[1].value.clone(), variables.clone()),
|
||||||
self.transpile(ast.tokens[2].value.clone(), indent + 1,),
|
self.transpile(ast.tokens[2].value.clone(), indent + 1, variables.clone()),
|
||||||
)
|
)
|
||||||
.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(ast.tokens[1].value.clone(), indent + 1,),
|
self.transpile(ast.tokens[1].value.clone(), indent + 1, variables.clone()),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::Namespace {
|
} else if ast.ast_type == AstType::Namespace {
|
||||||
@@ -216,7 +236,7 @@ impl Transpiler {
|
|||||||
"mod {} {}{}{}",
|
"mod {} {}{}{}",
|
||||||
&ast.tokens[0].value.clone(),
|
&ast.tokens[0].value.clone(),
|
||||||
"{",
|
"{",
|
||||||
self.transpile(ast.tokens[1].value.clone(), 0),
|
self.transpile(ast.tokens[1].value.clone(), 0, variables.clone()),
|
||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -225,7 +245,7 @@ impl Transpiler {
|
|||||||
"impl {} {}{}{}",
|
"impl {} {}{}{}",
|
||||||
&ast.tokens[0].value.clone(),
|
&ast.tokens[0].value.clone(),
|
||||||
"{",
|
"{",
|
||||||
self.transpile(ast.tokens[1].value.clone(), 0,),
|
self.transpile(ast.tokens[1].value.clone(), 0, variables.clone()),
|
||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -313,7 +333,7 @@ impl Transpiler {
|
|||||||
self.clone().modnum
|
self.clone().modnum
|
||||||
);
|
);
|
||||||
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
|
let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
|
||||||
let transpiled_code = self.transpile(file_content, 0);
|
let transpiled_code = self.transpile(file_content, 0, new_vars());
|
||||||
fs::write(
|
fs::write(
|
||||||
("build/".to_string() + modname.as_str()) + ".rs",
|
("build/".to_string() + modname.as_str()) + ".rs",
|
||||||
transpiled_code,
|
transpiled_code,
|
||||||
@@ -321,12 +341,12 @@ impl Transpiler {
|
|||||||
.expect("Error writing file");
|
.expect("Error writing file");
|
||||||
modname
|
modname
|
||||||
}
|
}
|
||||||
pub fn transpile_round(&mut self, input: String) -> String {
|
pub fn transpile_round(&mut self, input: String, variables: HashMap<String, Variable>) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let lexer_out = lex(input.as_str(), false, self.state);
|
let lexer_out = lex(input.as_str(), false, self.state);
|
||||||
match lexer_out {
|
match lexer_out {
|
||||||
Ok(tokens) => {
|
Ok(tokens) => {
|
||||||
let mut full_ast = Parser::new(tokens.clone());
|
let mut full_ast = Parser::new(tokens.clone(), variables.clone());
|
||||||
let mut last_ast = Ast {
|
let mut last_ast = Ast {
|
||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
@@ -364,7 +384,7 @@ impl Transpiler {
|
|||||||
} 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 +=
|
result +=
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
|
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
||||||
.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
|
||||||
{
|
{
|
||||||
@@ -375,7 +395,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}
|
}, variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -389,7 +409,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
},
|
}, variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructCall {
|
} else if ast.ast_type == AstType::StructCall {
|
||||||
@@ -416,12 +436,12 @@ impl Transpiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transpile_square(&mut self, input: String, state: LexerState) -> String {
|
pub fn transpile_square(&mut self, input: String, state: LexerState, variables: HashMap<String, Variable>) -> 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);
|
||||||
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.clone());
|
||||||
let mut last_ast = Ast {
|
let mut last_ast = Ast {
|
||||||
ast_type: AstType::Other,
|
ast_type: AstType::Other,
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
@@ -457,7 +477,7 @@ impl Transpiler {
|
|||||||
} 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 +=
|
result +=
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
|
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
||||||
.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
|
||||||
{
|
{
|
||||||
@@ -468,7 +488,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}
|
}, variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -482,7 +502,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
},
|
}, variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else if ast.ast_type == AstType::StructCall {
|
} else if ast.ast_type == AstType::StructCall {
|
||||||
@@ -506,12 +526,12 @@ impl Transpiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transpile_json(&mut self, input: String, state: LexerState) -> String {
|
pub fn transpile_json(&mut self, input: String, state: LexerState, variables: HashMap<String, Variable>) -> 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);
|
||||||
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.clone());
|
||||||
full_ast.json = true;
|
full_ast.json = true;
|
||||||
result += "HashMap::from([";
|
result += "HashMap::from([";
|
||||||
let mut last_ast = Ast {
|
let mut last_ast = Ast {
|
||||||
@@ -549,7 +569,7 @@ impl Transpiler {
|
|||||||
} 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 +=
|
result +=
|
||||||
format!("({})", self.transpile_round(ast.tokens[0].value.clone(),))
|
format!("({})", self.transpile_round(ast.tokens[0].value.clone(), variables.clone()))
|
||||||
.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
|
||||||
{
|
{
|
||||||
@@ -560,7 +580,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column
|
column: ast.tokens[0].column
|
||||||
}
|
}, variables.clone()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
@@ -572,7 +592,7 @@ impl Transpiler {
|
|||||||
LexerState {
|
LexerState {
|
||||||
line: ast.tokens[0].line,
|
line: ast.tokens[0].line,
|
||||||
column: ast.tokens[0].column,
|
column: ast.tokens[0].column,
|
||||||
},
|
}, variables.clone(),
|
||||||
)
|
)
|
||||||
.as_str();
|
.as_str();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user