fixed the line system

This commit is contained in:
2024-05-07 17:51:31 +02:00
parent b4e6b6ca0b
commit 1e4acdec40
3 changed files with 113 additions and 96 deletions
+106 -90
View File
@@ -115,51 +115,127 @@ fn get_first_char(value: &str) -> String {
value.chars().next().unwrap().to_string()
}
pub fn lex(mut code: &str, use_whitespace: bool) -> Result<Vec<Token>, (LexerState, Vec<Token>)> {
let mut state = LexerState { line: 1, column: 1 };
pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Vec<Token>, (LexerState, Vec<Token>)> {
let mut state = state;
let mut tokens: Vec<Token> = Vec::new();
let mut brstr: String = String::new();
let mut brvct: Vec<u8> = Vec::new();
let mut inside_str: u8 = 0;
let mut br_state: LexerState = LexerState { line: 0, column: 0 };
let mut brtp: Vec<u8> = Vec::new();
while !code.is_empty() {
let mut is_match = false;
let fch = get_first_char(code);
let brln = brtp.len();
match fch.as_str() {
"\"" => {
brstr += "\"";
code = code.strip_prefix(fch.as_str()).expect("");
if inside_str == 1 {
inside_str = 0;
if brvct.len() == 0 {
tokens.push(Token {
token_type: TokenType::String,
value: brstr.clone(),
column: state.column,
line: state.line
});
brstr = String::new();
}
} else if inside_str == 0 {
inside_str = 1;
if brln > 0 && brtp[brln-1] == 0 {
brtp.pop();
tokens.push(Token {
token_type: TokenType::String,
value: brstr.clone(),
column: br_state.column,
line: br_state.line
});
brstr = String::new();
} else if brln == 0 {
brtp.push(0);
br_state.line = state.line;
br_state.column = state.column;
}
}
"'" => {
brstr += "'";
code = code.strip_prefix(fch.as_str()).expect("");
if inside_str == 2 {
inside_str = 0;
if brvct.len() == 0 {
if brln > 0 && brtp[brln-1] == 1 {
brtp.pop();
tokens.push(Token {
token_type: TokenType::String,
value: brstr.clone(),
column: br_state.column,
line: br_state.line
});
brstr = String::new();
} else if brln == 0 {
brtp.push(1);
br_state.line = state.line;
br_state.column = state.column;
}
}
"(" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 {
brstr += fch.as_str();
} else {
br_state.line = state.line;
br_state.column = state.column;
}
brtp.push(2);
}
")" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 2 {
brtp.pop();
if brln == 1 {
tokens.push(Token {
token_type: TokenType::String,
token_type: TokenType::Round,
value: brstr.clone(),
column: state.column,
line: state.line
column: br_state.column,
line: br_state.line
});
brstr = String::new();
}
} else if inside_str == 0 {
inside_str = 2;
} else {brstr += fch.as_str();}
} else {brstr += fch.as_str();}
}
"{" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 {
brstr += fch.as_str();
} else {
br_state.line = state.line;
br_state.column = state.column;
}
brtp.push(3);
}
"}" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 3 {
brtp.pop();
if brln == 1 {
tokens.push(Token {
token_type: TokenType::Curly,
value: brstr.clone(),
column: br_state.column,
line: br_state.line
});
brstr = String::new();
} else {brstr += fch.as_str();}
} else {brstr += fch.as_str();}
}
"[" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 {
brstr += fch.as_str();
} else {
br_state.line = state.line;
br_state.column = state.column;
}
brtp.push(3);
}
"]" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 3 {
brtp.pop();
if brln == 1 {
tokens.push(Token {
token_type: TokenType::Round,
value: brstr.clone(),
column: br_state.column,
line: br_state.line
});
brstr = String::new();
} else {brstr += fch.as_str();}
} else {brstr += fch.as_str();}
}
"\\" => {
brstr += "\\";
@@ -170,76 +246,16 @@ pub fn lex(mut code: &str, use_whitespace: bool) -> Result<Vec<Token>, (LexerSta
brstr += &sch;
}
}
"{" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brvct.len() > 0 {
brstr += "{";
}
brvct.push(0);
}
"}" => {
code = code.strip_prefix(fch.as_str()).expect("");
brvct.pop();
if brvct.len() == 0 {
tokens.push(Token {
token_type: TokenType::Curly,
value: brstr.clone(),
column: state.column,
line: state.line
});
} else {
brstr += "}";
}
}
"(" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brvct.len() > 0 {
brstr += "(";
}
brvct.push(1);
}
")" => {
code = code.strip_prefix(fch.as_str()).expect("");
brvct.pop();
if brvct.len() == 0 {
tokens.push(Token {
token_type: TokenType::Round,
value: brstr.clone(),
column: state.column,
line: state.line
});
} else {
brstr += ")";
}
}
"[" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brvct.len() > 0 {
brstr += "[";
}
brvct.push(2);
}
"]" => {
code = code.strip_prefix(fch.as_str()).expect("");
brvct.pop();
if brvct.len() == 0 {
tokens.push(Token {
token_type: TokenType::Square,
value: brstr.clone(),
column: state.column,
line: state.line
});
} else {
brstr += "]";
}
}
"\n" => {
code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 {
brstr += "\n";
}
state.line += 1;
state.column = 0;
}
_ => {
if inside_str > 0 || brvct.len() > 0 {
if brln > 0 {
code = code.strip_prefix(fch.as_str()).expect("");
brstr += fch.as_str();
} else {
+2 -1
View File
@@ -4,6 +4,7 @@ mod lexer;
mod compile;
use std::fs;
use clap::Parser;
use lexer::LexerState;
// Arguments for Wyst (short and long version)
#[derive(Parser)]
@@ -23,7 +24,7 @@ fn main() {
let file_content = fs::read_to_string(&args.file)
.expect("Error reading file");
let transpiled_code = transpiler::transpile(file_content, 0);
let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 });
match args.rust {
Some(ref rust_file_name) => {
+5 -5
View File
@@ -1,12 +1,12 @@
use crate::lexer::{lex, TokenType};
use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Parser, AstType};
pub fn transpile(input: String, indent: u32) -> String {
pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
let mut result = String::new();
if indent > 0 {
result += " ".repeat((indent as usize)*2).as_str();
}
let lexer_out = lex(input.as_str(), false);
let lexer_out = lex(input.as_str(), false, state);
match lexer_out {
Ok(tokens) => {
let mut full_ast = Parser::new(tokens.clone());
@@ -14,9 +14,9 @@ pub fn transpile(input: String, indent: u32) -> String {
let ast = full_ast.next();
println!("{ast}");
if ast.ast_type == AstType::FunctionDeceleration {
result += format!("fn {}({}) -> {} {}", ast.tokens[1].value, ast.tokens[2].value, ast.tokens[0].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str();
result += format!("fn {}({}) -> {} {}", ast.tokens[1].value, ast.tokens[2].value, ast.tokens[0].value, transpile(ast.tokens[3].value.clone(), indent+1, LexerState{line:ast.tokens[3].line,column:ast.tokens[3].column})).as_str();
} else if ast.ast_type == AstType::VoidFunctionDeceleration {
result += format!("fn {}({}) {}", ast.tokens[1].value, ast.tokens[2].value, transpile(ast.tokens[3].value.clone(), indent+1)).as_str();
result += format!("fn {}({}) {}", ast.tokens[1].value, ast.tokens[2].value, transpile(ast.tokens[3].value.clone(), indent+1, LexerState{line:ast.tokens[3].line,column:ast.tokens[3].column})).as_str();
} else if ast.ast_type == AstType::VariableDeceleration {
result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::MutVariableDeceleration {