small changes

This commit is contained in:
2024-06-07 13:55:42 +02:00
parent d3b252fb28
commit 5fd9ab86bc
6 changed files with 122 additions and 51 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
use crate::{
lspcom::{get_completion, request_methods, LspServer, TextDocumentChangeParams},
parser::VariableType,
variable::VariableType,
};
use lsp_types::*;
use once_cell::sync::Lazy;
+1 -2
View File
@@ -1,5 +1,4 @@
use crate::parser::{new_vars, Variable};
use crate::transpiler::Transpiler;
use crate::{parser::new_vars, transpiler::Transpiler, variable::Variable};
use lsp_types::{CompletionParams, CompletionResponse, InitializeResult};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
+2 -1
View File
@@ -1,9 +1,10 @@
mod compile;
mod lexer;
mod lsp;
mod lspcom;
mod parser;
mod transpiler;
mod lspcom;
mod variable;
use clap::Parser;
use parser::new_vars;
use std::{fs, path::Path};
+39 -33
View File
@@ -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 regex::Regex;
use std::{collections::HashMap, fmt};
@@ -67,23 +70,14 @@ pub struct Parser {
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}})
])
HashMap::from([(
"void".to_string(),
Variable {
vtype: VariableType::Keyword,
state: LexerState { line: 0, column: 0 },
},
)])
}
impl Parser {
@@ -186,13 +180,16 @@ impl Parser {
} else {
ast_res.ast_type = AstType::FunctionDeceleration;
}
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
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
}
});
column: self.tokens[index + 1].clone().column,
},
},
);
self.index += 3;
} else if self.tokens.len() - index > 1
&& 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.ast_type = AstType::VariableDeceleration;
self.index += 1;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
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
}
});
column: self.tokens[index + 1].clone().column,
},
},
);
} else if self.tokens.len() - index > 2
&& self.tokens[index + 2].token_type == TokenType::Identifier
&& self.tokens[index + 1].token_type == TokenType::Angle
@@ -230,13 +230,16 @@ impl Parser {
ast_res.tokens[0].value += ">";
ast_res.ast_type = AstType::VariableDeceleration;
self.index += 2;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
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
}
});
column: self.tokens[index + 1].clone().column,
},
},
);
} else if self.tokens.len() - index > 2
&& self.tokens[index + 1].value == "*"
&& 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.ast_type = AstType::PointerDeceleration;
self.index += 2;
self.variables.insert(self.tokens[index + 1].clone().value, Variable {
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
}
});
column: self.tokens[index + 1].clone().column,
},
},
);
}
}
}
+53 -2
View File
@@ -1,5 +1,8 @@
use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{new_vars, Ast, AstType, Parser, Variable, VariableType};
use crate::{
lexer::{lex, LexerState, TokenType},
parser::{new_vars, Ast, AstType, Parser},
variable::{Variable, VariableType},
};
use std::collections::HashMap;
use std::fs;
@@ -383,6 +386,22 @@ impl Transpiler {
tokens: vec![],
};
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 {
let mut fl = 0;
for t in &last_ast.tokens {
@@ -487,6 +506,22 @@ impl Transpiler {
tokens: vec![],
};
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 {
let mut fl = 0;
for t in &last_ast.tokens {
@@ -588,6 +623,22 @@ impl Transpiler {
tokens: vec![],
};
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 {
let mut fl = 0;
for t in &last_ast.tokens {
+14
View File
@@ -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,
}