still no fix

This commit is contained in:
2024-06-04 17:29:16 +02:00
parent 5c367e6b44
commit 35653cff1f
3 changed files with 35 additions and 73 deletions
+24 -23
View File
@@ -1,14 +1,13 @@
// use std::collections::HashMap; // use std::collections::HashMap;
// use std::fs; // use std::fs;
use std::fs; use std::vec;
use lspower; 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 crate::transpiler::Transpiler;
#[derive(Debug)] #[derive(Debug)]
struct Backend { struct Backend {
@@ -41,9 +40,8 @@ impl LanguageServer for Backend {
&self, &self,
_params: lsp::CompletionParams, _params: lsp::CompletionParams,
) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> { ) -> lspower::jsonrpc::Result<Option<lsp::CompletionResponse>> {
Ok(Some(CompletionResponse::Array(vec![ let completion_items: Vec<CompletionItem> = Vec::new();
CompletionItem::new_simple("mylabel".to_string(), "mydetail".to_string()), Ok(Some(CompletionResponse::Array(completion_items)))
])))
} }
async fn shutdown(&self) -> Result<()> { async fn shutdown(&self) -> Result<()> {
@@ -51,23 +49,26 @@ impl LanguageServer for Backend {
} }
} }
// 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;
// }
#[tokio::main] #[tokio::main]
pub async fn run_lsp_server() { pub async fn run_lsp_server() {
let mut transpiler = Transpiler { let stdin = tokio::io::stdin();
var_match: String::from("x"), let stdout = tokio::io::stdout();
..Default::default()
}; let (service, messages) = LspService::new(|client| Backend { client });
let file_content = fs::read_to_string("main.wt").expect("Error reading file"); Server::new(stdin, stdout)
transpiler.transpile(file_content, 0); .interleave(messages)
.serve(service)
.await;
} }
// #[tokio::main]
// pub async fn run_lsp_server() {
// let mut transpiler = Transpiler {
// var_match: String::from("x"),
// var_state: LexerState { line: 6, column: 9 },
// ..Default::default()
// };
// let file_content = fs::read_to_string("main.wt").expect("Error reading file");
// transpiler.transpile(file_content, 0, HashMap::new());
// println!("{:?}", transpiler.matched_vars);
// }
+3 -25
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::{collections::HashMap, fmt}; use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
@@ -64,29 +64,16 @@ 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>, variables: HashMap<String, Variable>) -> Parser { pub fn new(tokens: Vec<Token>) -> 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,
} }
} }
@@ -179,15 +166,6 @@ impl Parser {
} else { } else {
ast_res.ast_type = AstType::FunctionDeceleration; ast_res.ast_type = AstType::FunctionDeceleration;
} }
self.variables.insert(
self.tokens[index + 1].value.clone(),
Variable {
vtype: VariableType::Func,
indent: 0,
line: self.tokens[index + 1].line.clone(),
column: self.tokens[index + 1].column.clone(),
},
);
self.index += 3; self.index += 3;
} else if self.tokens.len() - index > 1 } else if self.tokens.len() - index > 1
&& self.tokens[index + 1].token_type == TokenType::Curly && self.tokens[index + 1].token_type == TokenType::Curly
+8 -25
View File
@@ -1,6 +1,5 @@
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser, Variable, VariableType}; use crate::parser::{Ast, AstType, Parser};
use std::collections::HashMap;
use std::fs; use std::fs;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@@ -11,9 +10,6 @@ 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 var_match: String,
pub var_state: LexerState,
pub matched_vars: HashMap<String, Variable>,
} }
impl Default for Transpiler { impl Default for Transpiler {
@@ -25,9 +21,6 @@ impl Default for Transpiler {
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(),
} }
} }
} }
@@ -35,8 +28,6 @@ impl Default for Transpiler {
impl Transpiler { impl Transpiler {
pub fn transpile(&mut self, input: String, indent: u32) -> 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, Variable> = HashMap::new();
if indent == 0 { if indent == 0 {
// result += "type int = i32;\n"; // result += "type int = i32;\n";
} else { } else {
@@ -46,14 +37,12 @@ impl Transpiler {
match lexer_out { match lexer_out {
Ok(tokens) => { Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone(), variables); let mut full_ast = Parser::new(tokens.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() {
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 {
@@ -290,7 +279,7 @@ impl Transpiler {
result = result.trim_end().to_string(); result = result.trim_end().to_string();
if self.var_match != "" && (self.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.line // if (self.var_state.line > var.line
// || (self.var_state.line == var.line // || (self.var_state.line == var.line
@@ -300,8 +289,8 @@ impl Transpiler {
// self.matched_vars.insert(name, var); // self.matched_vars.insert(name, var);
// } // }
// } // }
self.var_match = String::new(); // self.var_match = String::new();
} // }
if indent > 0 { if indent > 0 {
result += "\n"; result += "\n";
@@ -335,11 +324,9 @@ impl Transpiler {
pub fn transpile_round(&mut self, input: String) -> 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, self.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(), variables); let mut full_ast = Parser::new(tokens.clone());
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
@@ -432,11 +419,9 @@ 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(), variables); let mut full_ast = Parser::new(tokens.clone());
let mut last_ast = Ast { let mut last_ast = Ast {
ast_type: AstType::Other, ast_type: AstType::Other,
tokens: vec![], tokens: vec![],
@@ -524,11 +509,9 @@ 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(), variables); let mut full_ast = Parser::new(tokens.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 {