small changes
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
lspcom::{get_completion, request_methods, LspServer, TextDocumentChangeParams},
|
lspcom::{get_completion, request_methods, LspServer, TextDocumentChangeParams},
|
||||||
parser::VariableType,
|
variable::VariableType,
|
||||||
};
|
};
|
||||||
use lsp_types::*;
|
use lsp_types::*;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|||||||
+1
-2
@@ -1,5 +1,4 @@
|
|||||||
use crate::parser::{new_vars, Variable};
|
use crate::{parser::new_vars, transpiler::Transpiler, variable::Variable};
|
||||||
use crate::transpiler::Transpiler;
|
|
||||||
use lsp_types::{CompletionParams, CompletionResponse, InitializeResult};
|
use lsp_types::{CompletionParams, CompletionResponse, InitializeResult};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|||||||
+2
-1
@@ -1,9 +1,10 @@
|
|||||||
mod compile;
|
mod compile;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod lsp;
|
mod lsp;
|
||||||
|
mod lspcom;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod transpiler;
|
mod transpiler;
|
||||||
mod lspcom;
|
mod variable;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use parser::new_vars;
|
use parser::new_vars;
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
|
|||||||
+51
-45
@@ -1,4 +1,7 @@
|
|||||||
use crate::lexer::{LexerState, Token, TokenType};
|
use crate::{
|
||||||
|
lexer::{LexerState, Token, TokenType},
|
||||||
|
variable::{Variable, VariableType},
|
||||||
|
};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{collections::HashMap, fmt};
|
use std::{collections::HashMap, fmt};
|
||||||
@@ -67,23 +70,14 @@ pub struct Parser {
|
|||||||
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> {
|
pub fn new_vars() -> HashMap<String, Variable> {
|
||||||
HashMap::from([
|
HashMap::from([(
|
||||||
("void".to_string(), Variable { vtype: VariableType::Keyword, state: LexerState {line:0,column:0}})
|
"void".to_string(),
|
||||||
])
|
Variable {
|
||||||
|
vtype: VariableType::Keyword,
|
||||||
|
state: LexerState { line: 0, column: 0 },
|
||||||
|
},
|
||||||
|
)])
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parser {
|
impl Parser {
|
||||||
@@ -186,13 +180,16 @@ 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 {
|
self.variables.insert(
|
||||||
vtype: VariableType::Func,
|
self.tokens[index + 1].clone().value,
|
||||||
state: LexerState {
|
Variable {
|
||||||
line: self.tokens[index + 1].clone().line,
|
vtype: VariableType::Func,
|
||||||
column: self.tokens[index + 1].clone().column
|
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
|
||||||
@@ -213,13 +210,16 @@ 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 {
|
self.variables.insert(
|
||||||
vtype: VariableType::Var,
|
self.tokens[index + 1].clone().value,
|
||||||
state: LexerState {
|
Variable {
|
||||||
line: self.tokens[index + 1].clone().line,
|
vtype: VariableType::Var,
|
||||||
column: self.tokens[index + 1].clone().column
|
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
|
||||||
@@ -230,13 +230,16 @@ 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 {
|
self.variables.insert(
|
||||||
vtype: VariableType::Var,
|
self.tokens[index + 1].clone().value,
|
||||||
state: LexerState {
|
Variable {
|
||||||
line: self.tokens[index + 1].clone().line,
|
vtype: VariableType::Var,
|
||||||
column: self.tokens[index + 1].clone().column
|
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
|
||||||
@@ -244,13 +247,16 @@ 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 {
|
self.variables.insert(
|
||||||
vtype: VariableType::Var,
|
self.tokens[index + 1].clone().value,
|
||||||
state: LexerState {
|
Variable {
|
||||||
line: self.tokens[index + 1].clone().line,
|
vtype: VariableType::Var,
|
||||||
column: self.tokens[index + 1].clone().column
|
state: LexerState {
|
||||||
}
|
line: self.tokens[index + 1].clone().line,
|
||||||
});
|
column: self.tokens[index + 1].clone().column,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-2
@@ -1,5 +1,8 @@
|
|||||||
use crate::lexer::{lex, LexerState, TokenType};
|
use crate::{
|
||||||
use crate::parser::{new_vars, Ast, AstType, Parser, Variable, VariableType};
|
lexer::{lex, LexerState, TokenType},
|
||||||
|
parser::{new_vars, Ast, AstType, Parser},
|
||||||
|
variable::{Variable, VariableType},
|
||||||
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
@@ -383,6 +386,22 @@ impl Transpiler {
|
|||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
|
let 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 {
|
||||||
@@ -487,6 +506,22 @@ impl Transpiler {
|
|||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
|
let 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 {
|
||||||
@@ -588,6 +623,22 @@ impl Transpiler {
|
|||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
};
|
};
|
||||||
for ast in full_ast.parse() {
|
for ast in full_ast.parse() {
|
||||||
|
let 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 {
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use crate::lexer::LexerState;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
|
pub enum VariableType {
|
||||||
|
Var,
|
||||||
|
Func,
|
||||||
|
Keyword,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Variable {
|
||||||
|
pub vtype: VariableType,
|
||||||
|
pub state: LexerState,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user