for, while, if, else

This commit is contained in:
2024-05-24 22:51:25 +02:00
parent 93ba413251
commit 8c32610737
3 changed files with 526 additions and 215 deletions
+98 -56
View File
@@ -1,16 +1,18 @@
use regex::Regex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt; use std::fmt;
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]
pub struct LexerState { pub struct LexerState {
pub line: usize, pub line: usize,
pub column: usize pub column: usize,
} }
// Define token types // Define token types
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]
pub enum TokenType { pub enum TokenType {
Keyword, Keyword,
Keyword1,
Keyword2,
Newline, Newline,
Whitespace, Whitespace,
Number, Number,
@@ -34,12 +36,19 @@ pub struct Token {
pub token_type: TokenType, pub token_type: TokenType,
pub value: String, pub value: String,
pub line: usize, pub line: usize,
pub column: usize pub column: usize,
} }
impl fmt::Debug for Token { impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(TokenType: {:?}, TokenValue: {}, Line: {}, Column {})", self.token_type, self.value.replace("\n", "\\n"), self.line, self.column) write!(
f,
"(TokenType: {:?}, TokenValue: {}, Line: {}, Column {})",
self.token_type,
self.value.replace("\n", "\\n"),
self.line,
self.column
)
} }
} }
@@ -51,73 +60,87 @@ impl fmt::Display for Token {
pub struct Node { pub struct Node {
token_type: TokenType, token_type: TokenType,
token_regex: Lazy<Regex> token_regex: Lazy<Regex>,
} }
const SYNTAX: [Node; 14] = [ const SYNTAX: [Node; 16] = [
Node { Node {
token_type: TokenType::Semicolon, token_type: TokenType::Semicolon,
token_regex: Lazy::new(|| Regex::new(r"^\;").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\;").unwrap()),
}, },
Node { Node {
token_type: TokenType::SecondOperator, token_type: TokenType::SecondOperator,
token_regex: Lazy::new(|| Regex::new(r"^,").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^,").unwrap()),
}, },
Node { Node {
token_type: TokenType::String, token_type: TokenType::String,
token_regex: Lazy::new(|| Regex::new("^\"").unwrap()) token_regex: Lazy::new(|| Regex::new("^\"").unwrap()),
}, },
Node { Node {
token_type: TokenType::Whitespace, token_type: TokenType::Whitespace,
token_regex: Lazy::new(|| Regex::new(r"^\s+").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\s+").unwrap()),
}, },
Node { Node {
token_type: TokenType::Keyword, token_type: TokenType::Keyword,
token_regex: Lazy::new(|| Regex::new(r"^(pub|mut|try|catch|return|fn|let|use|cb|struct|impl|for|in|as)\b").unwrap()) token_regex: Lazy::new(|| {
Regex::new(r"^(pub|mut|try|catch|return|fn|let|use|cb|struct|impl|in|as)\b").unwrap()
}),
},
Node {
token_type: TokenType::Keyword1,
token_regex: Lazy::new(|| Regex::new(r"^(if|for|while|else *if)\b").unwrap()),
},
Node {
token_type: TokenType::Keyword2,
token_regex: Lazy::new(|| Regex::new(r"^(else)\b").unwrap()),
}, },
Node { Node {
token_type: TokenType::Identifier, token_type: TokenType::Identifier,
token_regex: Lazy::new(|| Regex::new(r"^[._a-zA-Z][a-zA-Z0-9_]*").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[._a-zA-Z][a-zA-Z0-9_]*").unwrap()),
}, },
Node { Node {
token_type: TokenType::Number, token_type: TokenType::Number,
token_regex: Lazy::new(|| Regex::new(r"^\d+").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^\d+").unwrap()),
}, },
Node { Node {
token_type: TokenType::Ptr, token_type: TokenType::Ptr,
token_regex: Lazy::new(|| Regex::new(r"^->").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^->").unwrap()),
}, },
Node { Node {
token_type: TokenType::Operator, token_type: TokenType::Operator,
token_regex: Lazy::new(|| Regex::new(r"^[|\-|\+|\*|\=|\!|\&|\:]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[|\-|\+|\*|\=|\!|\&|\:]").unwrap()),
}, },
Node { Node {
token_type: TokenType::Round, token_type: TokenType::Round,
token_regex: Lazy::new(|| Regex::new(r"^[\(|\)]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[\(|\)]").unwrap()),
}, },
Node { Node {
token_type: TokenType::Curly, token_type: TokenType::Curly,
token_regex: Lazy::new(|| Regex::new(r"^[\{|\}]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[\{|\}]").unwrap()),
}, },
Node { Node {
token_type: TokenType::Square, token_type: TokenType::Square,
token_regex: Lazy::new(|| Regex::new(r"^[\[|\]]").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^[\[|\]]").unwrap()),
}, },
Node { Node {
token_type: TokenType::Include, token_type: TokenType::Include,
token_regex: Lazy::new(|| Regex::new(r"^#include *<(.*?)>").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^#include *<(.*?)>").unwrap()),
}, },
Node { Node {
token_type: TokenType::Include, token_type: TokenType::Include,
token_regex: Lazy::new(|| Regex::new(r#"^#include *"(.*?)""#).unwrap()) token_regex: Lazy::new(|| Regex::new(r#"^#include *"(.*?)""#).unwrap()),
} },
]; ];
fn get_first_char(value: &str) -> String { fn get_first_char(value: &str) -> String {
value.chars().next().unwrap().to_string() value.chars().next().unwrap().to_string()
} }
pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Vec<Token>, (LexerState, Vec<Token>)> { pub fn lex(
mut code: &str,
use_whitespace: bool,
state: LexerState,
) -> Result<Vec<Token>, (LexerState, Vec<Token>)> {
let mut state = state; let mut state = state;
let mut tokens: Vec<Token> = Vec::new(); let mut tokens: Vec<Token> = Vec::new();
let mut brstr: String = String::new(); let mut brstr: String = String::new();
@@ -134,7 +157,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
if brln == 0 { if brln == 0 {
if code.len() > 0 { if code.len() > 0 {
let sch = get_first_char(code); let sch = get_first_char(code);
if sch=="/" { if sch == "/" {
code = code.strip_prefix(sch.as_str()).expect(""); code = code.strip_prefix(sch.as_str()).expect("");
brstr += &sch; brstr += &sch;
brtp.push(4); brtp.push(4);
@@ -144,7 +167,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
br_state.line = state.line; br_state.line = state.line;
br_state.column = state.column; br_state.column = state.column;
} }
} else if sch=="*" { } else if sch == "*" {
code = code.strip_prefix(sch.as_str()).expect(""); code = code.strip_prefix(sch.as_str()).expect("");
brstr += &sch; brstr += &sch;
brtp.push(5); brtp.push(5);
@@ -159,7 +182,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: TokenType::Operator, token_type: TokenType::Operator,
value: "/".to_string(), value: "/".to_string(),
column: state.column, column: state.column,
line: state.line line: state.line,
}); });
} }
} else if brln == 0 { } else if brln == 0 {
@@ -167,7 +190,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: TokenType::Operator, token_type: TokenType::Operator,
value: "*".to_string(), value: "*".to_string(),
column: state.column, column: state.column,
line: state.line line: state.line,
}); });
} }
} }
@@ -177,7 +200,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if code.len() > 0 { if code.len() > 0 {
let sch = get_first_char(code); let sch = get_first_char(code);
if sch=="/" && brtp[brln-1]==5 { if sch == "/" && brtp[brln - 1] == 5 {
code = code.strip_prefix(sch.as_str()).expect(""); code = code.strip_prefix(sch.as_str()).expect("");
brstr += &sch; brstr += &sch;
brtp.pop(); brtp.pop();
@@ -185,7 +208,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: TokenType::Comment, token_type: TokenType::Comment,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else if brln == 0 { } else if brln == 0 {
@@ -193,7 +216,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: TokenType::Operator, token_type: TokenType::Operator,
value: "*".to_string(), value: "*".to_string(),
column: state.column, column: state.column,
line: state.line line: state.line,
}); });
} }
} else if brln == 0 { } else if brln == 0 {
@@ -201,20 +224,20 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: TokenType::Operator, token_type: TokenType::Operator,
value: "*".to_string(), value: "*".to_string(),
column: state.column, column: state.column,
line: state.line line: state.line,
}); });
} }
} }
"\"" => { "\"" => {
brstr += "\""; brstr += "\"";
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 0 { if brln > 0 && brtp[brln - 1] == 0 {
brtp.pop(); brtp.pop();
tokens.push(Token { tokens.push(Token {
token_type: TokenType::String, token_type: TokenType::String,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else if brln == 0 { } else if brln == 0 {
@@ -226,13 +249,13 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
"'" => { "'" => {
brstr += "'"; brstr += "'";
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 1 { if brln > 0 && brtp[brln - 1] == 1 {
brtp.pop(); brtp.pop();
tokens.push(Token { tokens.push(Token {
token_type: TokenType::String, token_type: TokenType::String,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else if brln == 0 { } else if brln == 0 {
@@ -253,18 +276,22 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
} }
")" => { ")" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 2 { if brln > 0 && brtp[brln - 1] == 2 {
brtp.pop(); brtp.pop();
if brln == 1 { if brln == 1 {
tokens.push(Token { tokens.push(Token {
token_type: TokenType::Round, token_type: TokenType::Round,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else {brstr += fch.as_str();} } else {
} else {brstr += fch.as_str();} brstr += fch.as_str();
}
} else {
brstr += fch.as_str();
}
} }
"{" => { "{" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
@@ -278,18 +305,22 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
} }
"}" => { "}" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 3 { if brln > 0 && brtp[brln - 1] == 3 {
brtp.pop(); brtp.pop();
if brln == 1 { if brln == 1 {
tokens.push(Token { tokens.push(Token {
token_type: TokenType::Curly, token_type: TokenType::Curly,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else {brstr += fch.as_str();} } else {
} else {brstr += fch.as_str();} brstr += fch.as_str();
}
} else {
brstr += fch.as_str();
}
} }
"[" => { "[" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
@@ -303,18 +334,22 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
} }
"]" => { "]" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 3 { if brln > 0 && brtp[brln - 1] == 3 {
brtp.pop(); brtp.pop();
if brln == 1 { if brln == 1 {
tokens.push(Token { tokens.push(Token {
token_type: TokenType::Square, token_type: TokenType::Square,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else {brstr += fch.as_str();} } else {
} else {brstr += fch.as_str();} brstr += fch.as_str();
}
} else {
brstr += fch.as_str();
}
} }
"<" => { "<" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
@@ -328,18 +363,22 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
} }
">" => { ">" => {
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 && brtp[brln-1] == 6 { if brln > 0 && brtp[brln - 1] == 6 {
brtp.pop(); brtp.pop();
if brln == 1 { if brln == 1 {
tokens.push(Token { tokens.push(Token {
token_type: TokenType::Angle, token_type: TokenType::Angle,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} else {brstr += fch.as_str();} } else {
} else {brstr += fch.as_str();} brstr += fch.as_str();
}
} else {
brstr += fch.as_str();
}
} }
"\\" => { "\\" => {
brstr += "\\"; brstr += "\\";
@@ -354,13 +393,14 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
code = code.strip_prefix(fch.as_str()).expect(""); code = code.strip_prefix(fch.as_str()).expect("");
if brln > 0 { if brln > 0 {
brstr += "\n"; brstr += "\n";
} if brln==1 && brtp[0]==4 { }
if brln == 1 && brtp[0] == 4 {
brtp.pop(); brtp.pop();
tokens.push(Token { tokens.push(Token {
token_type: TokenType::Comment, token_type: TokenType::Comment,
value: brstr.clone(), value: brstr.clone(),
column: br_state.column, column: br_state.column,
line: br_state.line line: br_state.line,
}); });
brstr = String::new(); brstr = String::new();
} }
@@ -376,7 +416,9 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
if let Some(caps) = s.token_regex.captures(code) { if let Some(caps) = s.token_regex.captures(code) {
is_match = true; is_match = true;
code = code.strip_prefix(&caps[0]).unwrap_or(code); code = code.strip_prefix(&caps[0]).unwrap_or(code);
if (!use_whitespace && s.token_type!=TokenType::Whitespace) || use_whitespace { if (!use_whitespace && s.token_type != TokenType::Whitespace)
|| use_whitespace
{
let cap = caps[0].to_string(); let cap = caps[0].to_string();
match cap.as_str() { match cap.as_str() {
"int" => { "int" => {
@@ -384,7 +426,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: s.token_type, token_type: s.token_type,
value: "i32".to_string(), value: "i32".to_string(),
line: state.line, line: state.line,
column: state.column column: state.column,
}); });
} }
"float" => { "float" => {
@@ -392,7 +434,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: s.token_type, token_type: s.token_type,
value: "f32".to_string(), value: "f32".to_string(),
line: state.line, line: state.line,
column: state.column column: state.column,
}); });
} }
_ => { _ => {
@@ -400,7 +442,7 @@ pub fn lex(mut code: &str, use_whitespace: bool, state: LexerState) -> Result<Ve
token_type: s.token_type, token_type: s.token_type,
value: cap, value: cap,
line: state.line, line: state.line,
column: state.column column: state.column,
}); });
} }
} }
+87 -32
View File
@@ -1,7 +1,7 @@
use crate::lexer::{Token, TokenType}; use crate::lexer::{Token, TokenType};
use std::fmt;
use regex::Regex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
@@ -13,16 +13,18 @@ pub enum AstType {
VariableDeceleration, VariableDeceleration,
PointerDeceleration, PointerDeceleration,
MutVariableDeceleration, MutVariableDeceleration,
State3,
State2,
Include, Include,
IncludeLocal, IncludeLocal,
CodeBlock, CodeBlock,
Json, Json,
Other Other,
} }
pub struct Ast { pub struct Ast {
pub tokens: Vec<Token>, pub tokens: Vec<Token>,
pub ast_type: AstType pub ast_type: AstType,
} }
impl fmt::Display for Ast { impl fmt::Display for Ast {
@@ -58,7 +60,7 @@ 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 json: bool pub json: bool,
} }
impl Parser { impl Parser {
@@ -68,56 +70,98 @@ impl Parser {
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()),
json: false json: false,
} }
} }
pub fn next(&mut self) -> Ast { pub fn next(&mut self) -> Ast {
let mut ast_res: Ast = Ast {tokens: vec![], ast_type: AstType::Other}; let mut ast_res: Ast = Ast {
tokens: vec![],
ast_type: AstType::Other,
};
let index = self.index as usize; let index = self.index as usize;
if index == self.tokens.len() {panic!("Reached the end of tokens")} if index == self.tokens.len() {
panic!("Reached the end of tokens")
}
let token = &self.tokens[index]; let token = &self.tokens[index];
if self.json && self.tokens.len() - (self.index as usize) > 2 && self.tokens[index+1].value == ":" { if self.json
&& self.tokens.len() - (self.index as usize) > 2
&& self.tokens[index + 1].value == ":"
{
ast_res.ast_type = AstType::Json; ast_res.ast_type = AstType::Json;
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index+2].clone()); ast_res.tokens.push(self.tokens[index + 2].clone());
self.index += 2; self.index += 2;
} else if self.tokens.len()-index > 2 && self.tokens[index].value == "struct" && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Curly { } else if self.tokens.len() - index > 2
ast_res.tokens.push(self.tokens[index+1].clone()); && self.tokens[index].value == "struct"
ast_res.tokens.push(self.tokens[index+2].clone()); && self.tokens[index + 1].token_type == TokenType::Identifier
&& self.tokens[index + 2].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::StructDeceleration; ast_res.ast_type = AstType::StructDeceleration;
self.index += 2; self.index += 2;
} else if self.tokens.len() - index > 2
&& self.tokens[index].token_type == TokenType::Keyword1
&& self.tokens[index + 1].token_type == TokenType::Round
&& self.tokens[index + 2].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::State3;
self.index += 2;
} else if self.tokens.len() - index > 1
&& self.tokens[index].token_type == TokenType::Keyword2
&& self.tokens[index + 1].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index].clone());
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::State2;
self.index += 1;
} else { } else {
match token.token_type { match token.token_type {
TokenType::Identifier => { TokenType::Identifier => {
ast_res.tokens.push(self.tokens[index].clone()); ast_res.tokens.push(self.tokens[index].clone());
if self.tokens.len()-index > 3 && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Round && self.tokens[index+3].token_type==TokenType::Curly { if self.tokens.len() - index > 3
ast_res.tokens.push(self.tokens[index+1].clone()); && self.tokens[index + 1].token_type == TokenType::Identifier
ast_res.tokens.push(self.tokens[index+2].clone()); && self.tokens[index + 2].token_type == TokenType::Round
ast_res.tokens.push(self.tokens[index+3].clone()); && self.tokens[index + 3].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens.push(self.tokens[index + 3].clone());
if token.value == "void" { if token.value == "void" {
ast_res.ast_type = AstType::VoidFunctionDeceleration; ast_res.ast_type = AstType::VoidFunctionDeceleration;
} else { } else {
ast_res.ast_type = AstType::FunctionDeceleration; ast_res.ast_type = AstType::FunctionDeceleration;
} }
self.index += 3; self.index += 3;
} else if self.tokens.len()-index > 1 && self.tokens[index+1].token_type==TokenType::Curly { } else if self.tokens.len() - index > 1
ast_res.tokens.push(self.tokens[index+1].clone()); && self.tokens[index + 1].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::StructCall; ast_res.ast_type = AstType::StructCall;
self.index += 1; self.index += 1;
} else if self.tokens.len()-index > 2 && self.tokens[index+1].token_type==TokenType::Identifier && self.tokens[index+2].token_type==TokenType::Curly { } else if self.tokens.len() - index > 2
ast_res.tokens.push(self.tokens[index+1].clone()); && self.tokens[index + 1].token_type == TokenType::Identifier
ast_res.tokens.push(self.tokens[index+2].clone()); && self.tokens[index + 2].token_type == TokenType::Curly
{
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.ast_type = AstType::StructVar; ast_res.ast_type = AstType::StructVar;
self.index += 2; self.index += 2;
} else if self.tokens.len()-index > 1 { } else if self.tokens.len() - index > 1 {
if self.tokens[index+1].token_type==TokenType::Identifier { if self.tokens[index + 1].token_type == TokenType::Identifier {
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;
} else if self.tokens.len()-index > 2 && self.tokens[index+2].token_type==TokenType::Identifier && self.tokens[index+1].token_type==TokenType::Angle { } else if self.tokens.len() - index > 2
ast_res.tokens.push(self.tokens[index+2].clone()); && self.tokens[index + 2].token_type == TokenType::Identifier
&& self.tokens[index + 1].token_type == TokenType::Angle
{
ast_res.tokens.push(self.tokens[index + 2].clone());
ast_res.tokens[0].value += "<"; ast_res.tokens[0].value += "<";
ast_res.tokens[0].value += self.tokens[index+1].value.as_str(); ast_res.tokens[0].value += self.tokens[index + 1].value.as_str();
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;
@@ -126,10 +170,20 @@ impl Parser {
} }
TokenType::Include => { TokenType::Include => {
if let Some(caps) = self.include_regex.captures(&token.value) { if let Some(caps) = self.include_regex.captures(&token.value) {
ast_res.tokens.push(Token {token_type: TokenType::String, value: caps[2].to_owned().to_string(), line: 0, column: 0}); ast_res.tokens.push(Token {
token_type: TokenType::String,
value: caps[2].to_owned().to_string(),
line: 0,
column: 0,
});
ast_res.ast_type = AstType::Include; ast_res.ast_type = AstType::Include;
} else if let Some(caps) = self.include_regex_local.captures(&token.value) { } else if let Some(caps) = self.include_regex_local.captures(&token.value) {
ast_res.tokens.push(Token {token_type: TokenType::String, value: caps[2].to_owned().to_string(), line: 0, column: 0}); ast_res.tokens.push(Token {
token_type: TokenType::String,
value: caps[2].to_owned().to_string(),
line: 0,
column: 0,
});
ast_res.ast_type = AstType::IncludeLocal; ast_res.ast_type = AstType::IncludeLocal;
} else { } else {
ast_res.tokens.push(token.clone()); ast_res.tokens.push(token.clone());
@@ -137,8 +191,9 @@ impl Parser {
} }
} }
TokenType::Keyword => { TokenType::Keyword => {
if token.value == "cb" && self.tokens[index+1].token_type == TokenType::Curly { if token.value == "cb" && self.tokens[index + 1].token_type == TokenType::Curly
ast_res.tokens.push(self.tokens[index+1].clone()); {
ast_res.tokens.push(self.tokens[index + 1].clone());
ast_res.ast_type = AstType::CodeBlock; ast_res.ast_type = AstType::CodeBlock;
self.index += 1; self.index += 1;
} else { } else {
+327 -113
View File
@@ -1,38 +1,59 @@
use std::fs;
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser}; use crate::parser::{Ast, AstType, Parser};
use std::fs;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Options { pub struct Options {
auto_mut: bool, auto_mut: bool,
auto_macro: bool, auto_macro: bool,
macros: Vec<String>, macros: Vec<String>,
modnum: u32 modnum: u32,
} }
impl Default for Options { impl Default for Options {
fn default() -> Options { fn default() -> Options {
Options { auto_mut: true, auto_macro: true, macros: vec![String::from("println")], modnum: 0 } Options {
auto_mut: true,
auto_macro: true,
macros: vec![String::from("println")],
modnum: 0,
}
} }
} }
fn clean_incl(input: &str) -> String { fn clean_incl(input: &str) -> String {
input input
.chars() .chars()
.map(|c| if c.is_alphanumeric() || c == '_' { c } else { '_' }) .map(|c| {
if c.is_alphanumeric() || c == '_' {
c
} else {
'_'
}
})
.collect() .collect()
} }
pub fn transpile_mod(ast: Ast, options: &mut Options, s: &str) -> String { pub fn transpile_mod(ast: Ast, options: &mut Options, s: &str) -> String {
let modfile = ast.tokens[0].value.as_str(); let modfile = ast.tokens[0].value.as_str();
let modname = format!("{}_{}", clean_incl(modfile.split(".").collect::<Vec<_>>()[0]), options.clone().modnum); let modname = format!(
println!("{}", s.to_string()+modfile); "{}_{}",
let file_content = fs::read_to_string(s.to_string()+modfile) clean_incl(modfile.split(".").collect::<Vec<_>>()[0]),
.expect("Error reading file"); options.clone().modnum
let transpiled_code = transpile(file_content, 0, LexerState { line: 1, column: 0 }, &mut Options::default()); );
fs::write(("wyst_tmp/".to_string()+modname.as_str())+".rs", println!("{}", s.to_string() + modfile);
transpiled_code) let file_content = fs::read_to_string(s.to_string() + modfile).expect("Error reading file");
.expect("Error writing file"); let transpiled_code = transpile(
file_content,
0,
LexerState { line: 1, column: 0 },
&mut Options::default(),
);
fs::write(
("wyst_tmp/".to_string() + modname.as_str()) + ".rs",
transpiled_code,
)
.expect("Error writing file");
modname modname
} }
@@ -50,31 +71,45 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
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());
let mut last_ast = Ast {ast_type: AstType::Other, tokens: vec![]}; let mut last_ast = Ast {
ast_type: AstType::Other,
tokens: vec![],
};
while full_ast.tokens.len() > full_ast.index as usize { while full_ast.tokens.len() > full_ast.index as usize {
let ast = full_ast.next(); let ast = full_ast.next();
println!("{ast}"); println!("{ast}");
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 { fl+=t.value.len() } for t in &last_ast.tokens {
// if ast.tokens[ast.tokens.len()-1].line > last_ast.tokens[last_ast.tokens.len()-1].line { fl += t.value.len()
// // result += ("\n".to_string() + " ".repeat(((indent+1) as usize)*2).as_str()).repeat(ast.tokens[ast.tokens.len()-1].line - last_ast.tokens[last_ast.tokens.len()-1].line).as_str(); }
// result += "\n".repeat(ast.tokens[ast.tokens.len()-1].line - last_ast.tokens[last_ast.tokens.len()-1].line).as_str(); if ast.tokens[ast.tokens.len() - 1].column
// } > last_ast.tokens[last_ast.tokens.len() - 1].column + fl
if ast.tokens[ast.tokens.len()-1].column > last_ast.tokens[last_ast.tokens.len()-1].column+fl { {
result += " ".repeat(ast.tokens[ast.tokens.len()-1].column - (last_ast.tokens[last_ast.tokens.len()-1].column+fl)).as_str(); result += " "
.repeat(
ast.tokens[ast.tokens.len() - 1].column
- (last_ast.tokens[last_ast.tokens.len() - 1].column + fl),
)
.as_str();
} }
} }
last_ast = Ast {ast_type: ast.ast_type.clone(), tokens: ast.tokens.clone()}; last_ast = Ast {
ast_type: ast.ast_type.clone(),
tokens: ast.tokens.clone(),
};
if ast.ast_type == AstType::FunctionDeceleration { if ast.ast_type == AstType::FunctionDeceleration {
result += format!( result += format!(
"fn {}({}) -> {} {}", "fn {}({}) -> {} {}",
ast.tokens[1].value, ast.tokens[1].value,
transpile_round(ast.tokens[2].value.clone(), LexerState { transpile_round(
line: ast.tokens[2].line, ast.tokens[2].value.clone(),
column: ast.tokens[2].column LexerState {
}), line: ast.tokens[2].line,
column: ast.tokens[2].column
}
),
ast.tokens[0].value, ast.tokens[0].value,
transpile( transpile(
ast.tokens[3].value.clone(), ast.tokens[3].value.clone(),
@@ -91,10 +126,13 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
result += format!( result += format!(
"fn {}({}) {}", "fn {}({}) {}",
ast.tokens[1].value, ast.tokens[1].value,
transpile_round(ast.tokens[2].value.clone(), LexerState { transpile_round(
line: ast.tokens[2].line, ast.tokens[2].value.clone(),
column: ast.tokens[2].column LexerState {
}), line: ast.tokens[2].line,
column: ast.tokens[2].column
}
),
transpile( transpile(
ast.tokens[3].value.clone(), ast.tokens[3].value.clone(),
indent + 1, indent + 1,
@@ -117,61 +155,92 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
line: ast.tokens[1].line, line: ast.tokens[1].line,
column: ast.tokens[1].column column: ast.tokens[1].column
} }
).trim_end() )
).replace("\n", ("\n".to_string() + " ".repeat(((indent+1) as usize)*2).as_str()).as_str()) .trim_end()
)
.replace(
"\n",
("\n".to_string() + " ".repeat(((indent + 1) as usize) * 2).as_str())
.as_str(),
)
.as_str(); .as_str();
result += "\n}\n"; result += "\n}\n";
} else if ast.ast_type == AstType::VariableDeceleration { } else if ast.ast_type == AstType::VariableDeceleration {
if options.clone().auto_mut { if options.clone().auto_mut {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str();
} else { } else {
result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result += format!("let {}: {}", ast.tokens[1].value, ast.tokens[0].value)
.as_str();
} }
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value)
} else if ast.ast_type == AstType::Other && ast.tokens[0].token_type == TokenType::Round { .as_str();
} else if ast.ast_type == AstType::Other
&& ast.tokens[0].token_type == TokenType::Round
{
result += format!( result += format!(
"({})", "({})",
transpile_round(ast.tokens[0].value.clone(), LexerState { transpile_round(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.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 {
result += format!( result += format!(
"[{}]", "[{}]",
transpile_square(ast.tokens[0].value.clone(), LexerState { transpile_square(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::CodeBlock { } else if ast.ast_type == AstType::CodeBlock {
result+="{"; result += "{";
result+=ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result+="}"; result += "}";
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
// if ast.tokens[0].token_type == TokenType::Newline { // if ast.tokens[0].token_type == TokenType::Newline {
// result += (ast.tokens[0].value.as_str().to_owned() // result += (ast.tokens[0].value.as_str().to_owned()
// + (" ".repeat((indent as usize) * 2).as_str())) // + (" ".repeat((indent as usize) * 2).as_str()))
// .as_str(); // .as_str();
// } else { // } else {
// result += ast.tokens[0].value.as_str(); // result += ast.tokens[0].value.as_str();
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState { result += transpile_json(
ast.tokens[0].value.as_str().to_string(),
LexerState {
line: ast.tokens[0].line, line: ast.tokens[0].line,
column: ast.tokens[0].column column: ast.tokens[0].column,
}).as_str(); },
)
.as_str();
// } // }
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
result+="."; result += ".";
} else if ast.ast_type == AstType::StructCall { } else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " {"; result += " {";
result += ast.tokens[1].value.as_str(); result += ast.tokens[1].value.as_str();
result += "}"; result += "}";
} else if ast.ast_type == AstType::StructVar { } else if ast.ast_type == AstType::StructVar {
result += format!("let mut {}: {} = {} {}{}{}", ast.tokens[1].value.as_str(), ast.tokens[0].value.as_str(), ast.tokens[0].value.as_str(), "{", ast.tokens[2].value.as_str(), "}").as_str(); result += format!(
"let mut {}: {} = {} {}{}{}",
ast.tokens[1].value.as_str(),
ast.tokens[0].value.as_str(),
ast.tokens[0].value.as_str(),
"{",
ast.tokens[2].value.as_str(),
"}"
)
.as_str();
} else if ast.ast_type == AstType::Include { } else if ast.ast_type == AstType::Include {
let modname = transpile_mod(ast, options, "lib/"); let modname = transpile_mod(ast, options, "lib/");
result += "mod "; result += "mod ";
@@ -190,7 +259,45 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
result += modname.as_str(); result += modname.as_str();
result += "::*;\n"; result += "::*;\n";
options.modnum += 1; options.modnum += 1;
} // flp } else if ast.ast_type == AstType::State3 {
result += format!(
"{} {} {}",
ast.tokens[0].value.clone(),
transpile_round(
ast.tokens[1].value.clone(),
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
}
),
transpile(
ast.tokens[2].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[2].line,
column: ast.tokens[2].column
},
options
),
)
.as_str();
} else if ast.ast_type == AstType::State2 {
result += format!(
"{} {}",
ast.tokens[0].value.clone(),
transpile(
ast.tokens[1].value.clone(),
indent + 1,
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
},
options
),
)
.as_str();
}
// flp
else { else {
if ast.tokens[0].token_type == TokenType::Newline { if ast.tokens[0].token_type == TokenType::Newline {
result += (ast.tokens[0].value.as_str().to_owned() result += (ast.tokens[0].value.as_str().to_owned()
@@ -200,19 +307,23 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Op
result += ";\n"; result += ";\n";
result += " ".repeat((indent as usize) * 2).as_str(); result += " ".repeat((indent as usize) * 2).as_str();
} else { } else {
// if last_ast.tokens.len() > 0 && ( // if last_ast.tokens.len() > 0 && (
// last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Identifier || // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Identifier ||
// last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Keyword || // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Keyword ||
// last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Number // last_ast.tokens[last_ast.tokens.len()-1].token_type == TokenType::Number
// ) { // ) {
// let ltkn = last_ast.tokens[last_ast.tokens.len()-1].token_type; // let ltkn = last_ast.tokens[last_ast.tokens.len()-1].token_type;
// if ltkn == TokenType::Identifier || // if ltkn == TokenType::Identifier ||
// ltkn == TokenType::Keyword || // ltkn == TokenType::Keyword ||
// ltkn == TokenType::Number { // ltkn == TokenType::Number {
// } // }
// } // }
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
if options.auto_macro && options.macros.contains(&ast.tokens[0].value.as_str().to_string()) { if options.auto_macro
&& options
.macros
.contains(&ast.tokens[0].value.as_str().to_string())
{
result += "!"; result += "!";
} }
} }
@@ -242,48 +353,84 @@ pub fn transpile_round(input: String, state: LexerState) -> String {
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());
let mut last_ast = Ast {
ast_type: AstType::Other,
tokens: vec![],
};
while full_ast.tokens.len() > full_ast.index as usize { while full_ast.tokens.len() > full_ast.index as usize {
let ast = full_ast.next(); let ast = full_ast.next();
if last_ast.tokens.len() > 0 {
let mut fl = 0;
for t in &last_ast.tokens {
fl += t.value.len()
}
if ast.tokens[ast.tokens.len() - 1].column
> last_ast.tokens[last_ast.tokens.len() - 1].column + fl
{
result += " "
.repeat(
ast.tokens[ast.tokens.len() - 1].column
- (last_ast.tokens[last_ast.tokens.len() - 1].column + fl),
)
.as_str();
}
}
last_ast = Ast {
ast_type: ast.ast_type.clone(),
tokens: ast.tokens.clone(),
};
if ast.ast_type == AstType::VariableDeceleration { if ast.ast_type == AstType::VariableDeceleration {
result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::PointerDeceleration { } else if ast.ast_type == AstType::PointerDeceleration {
result += format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} 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 += format!( result += format!(
"({})", "({})",
transpile_round(ast.tokens[0].value.clone(), LexerState { transpile_round(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.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 {
result += format!( result += format!(
"[{}]", "[{}]",
transpile_square(ast.tokens[0].value.clone(), LexerState { transpile_square(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.as_str(); .as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
result+="."; result += ".";
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState { result += transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column LexerState {
}).as_str(); line: ast.tokens[0].line,
column: ast.tokens[0].column,
},
)
.as_str();
} else if ast.ast_type == AstType::StructCall { } else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " {"; result += " {";
result += ast.tokens[1].value.as_str(); result += ast.tokens[1].value.as_str();
result += "}"; result += "}";
} // flp }
// flp
else { else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " ";
} }
} }
@@ -303,51 +450,85 @@ pub fn transpile_square(input: String, state: LexerState) -> String {
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());
let mut last_ast = Ast {
ast_type: AstType::Other,
tokens: vec![],
};
while full_ast.tokens.len() > full_ast.index as usize { while full_ast.tokens.len() > full_ast.index as usize {
let ast = full_ast.next(); let ast = full_ast.next();
println!("{ast}"); if last_ast.tokens.len() > 0 {
let mut fl = 0;
for t in &last_ast.tokens {
fl += t.value.len()
}
if ast.tokens[ast.tokens.len() - 1].column
> last_ast.tokens[last_ast.tokens.len() - 1].column + fl
{
result += " "
.repeat(
ast.tokens[ast.tokens.len() - 1].column
- (last_ast.tokens[last_ast.tokens.len() - 1].column + fl),
)
.as_str();
}
}
last_ast = Ast {
ast_type: ast.ast_type.clone(),
tokens: ast.tokens.clone(),
};
if ast.ast_type == AstType::VariableDeceleration { if ast.ast_type == AstType::VariableDeceleration {
result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result += format!("{}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::MutVariableDeceleration { } else if ast.ast_type == AstType::MutVariableDeceleration {
result += format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} else if ast.ast_type == AstType::PointerDeceleration { } else if ast.ast_type == AstType::PointerDeceleration {
result += format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); result +=
format!("{}: &mut {}", ast.tokens[1].value, ast.tokens[0].value).as_str();
} 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 += format!( result += format!(
"({})", "({})",
transpile_round(ast.tokens[0].value.clone(), LexerState { transpile_round(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.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 {
result += format!( result += format!(
"[{}]", "[{}]",
transpile_square(ast.tokens[0].value.clone(), LexerState { transpile_square(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.as_str(); .as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Ptr {
result+="."; result += ".";
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState { result += transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column LexerState {
}).as_str(); line: ast.tokens[0].line,
column: ast.tokens[0].column,
},
)
.as_str();
} else if ast.ast_type == AstType::StructCall { } else if ast.ast_type == AstType::StructCall {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " {"; result += " {";
result += ast.tokens[1].value.as_str(); result += ast.tokens[1].value.as_str();
result += "}"; result += "}";
} // flp }
// flp
else { else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " ";
} }
} }
@@ -369,9 +550,33 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
let mut full_ast = Parser::new(tokens.clone()); 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 {
ast_type: AstType::Other,
tokens: vec![],
};
while full_ast.tokens.len() > full_ast.index as usize { while full_ast.tokens.len() > full_ast.index as usize {
let ast = full_ast.next(); let ast = full_ast.next();
println!("{ast}"); if last_ast.tokens.len() > 0 {
let mut fl = 0;
for t in &last_ast.tokens {
fl += t.value.len()
}
if ast.tokens[ast.tokens.len() - 1].column
> last_ast.tokens[last_ast.tokens.len() - 1].column + fl
{
result += " "
.repeat(
ast.tokens[ast.tokens.len() - 1].column
- (last_ast.tokens[last_ast.tokens.len() - 1].column + fl),
)
.as_str();
}
}
last_ast = Ast {
ast_type: ast.ast_type.clone(),
tokens: ast.tokens.clone(),
};
if ast.ast_type == AstType::Json { if ast.ast_type == AstType::Json {
result += "("; result += "(";
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
@@ -381,29 +586,38 @@ pub fn transpile_json(input: String, state: LexerState) -> String {
} 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 += format!( result += format!(
"({})", "({})",
transpile_round(ast.tokens[0].value.clone(), LexerState { transpile_round(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.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 {
result += format!( result += format!(
"[{}]", "[{}]",
transpile_square(ast.tokens[0].value.clone(), LexerState { transpile_square(
line: ast.tokens[0].line, ast.tokens[0].value.clone(),
column: ast.tokens[0].column LexerState {
}) line: ast.tokens[0].line,
column: ast.tokens[0].column
}
)
) )
.as_str(); .as_str();
} else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly { } else if ast.tokens.len() == 1 && ast.tokens[0].token_type == TokenType::Curly {
result += transpile_json(ast.tokens[0].value.as_str().to_string(), LexerState { result += transpile_json(
line: ast.tokens[0].line, ast.tokens[0].value.as_str().to_string(),
column: ast.tokens[0].column LexerState {
}).as_str(); line: ast.tokens[0].line,
column: ast.tokens[0].column,
},
)
.as_str();
} else { } else {
result += ast.tokens[0].value.as_str(); result += ast.tokens[0].value.as_str();
result += " ";
} }
} }
result += "])"; result += "])";