This commit is contained in:
2024-05-15 19:57:55 +02:00
parent e7142f6d80
commit 9dc8712364
4 changed files with 45 additions and 7 deletions
+1 -1
View File
@@ -72,7 +72,7 @@ const SYNTAX: [Node; 14] = [
}, },
Node { Node {
token_type: TokenType::Keyword, token_type: TokenType::Keyword,
token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use|cb)\b").unwrap()) token_regex: Lazy::new(|| Regex::new(r"^(mut|try|catch|return|fn|let|use|cb|struct|impl|for|in)\b").unwrap())
}, },
Node { Node {
token_type: TokenType::Identifier, token_type: TokenType::Identifier,
+2 -1
View File
@@ -5,6 +5,7 @@ mod compile;
use std::fs; use std::fs;
use clap::Parser; use clap::Parser;
use lexer::LexerState; use lexer::LexerState;
use transpiler::Options;
// Arguments for Wyst (short and long version) // Arguments for Wyst (short and long version)
#[derive(Parser)] #[derive(Parser)]
@@ -24,7 +25,7 @@ fn main() {
let file_content = fs::read_to_string(&args.file) let file_content = fs::read_to_string(&args.file)
.expect("Error reading file"); .expect("Error reading file");
let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }); let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }, Options::default());
match args.rust { match args.rust {
Some(ref rust_file_name) => { Some(ref rust_file_name) => {
+6
View File
@@ -6,6 +6,7 @@ use once_cell::sync::Lazy;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstType { pub enum AstType {
FunctionDeceleration, FunctionDeceleration,
StructDeceleration,
VoidFunctionDeceleration, VoidFunctionDeceleration,
VariableDeceleration, VariableDeceleration,
PointerDeceleration, PointerDeceleration,
@@ -78,6 +79,11 @@ impl Parser {
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 {
ast_res.tokens.push(self.tokens[index+1].clone());
ast_res.tokens.push(self.tokens[index+2].clone());
ast_res.ast_type = AstType::StructDeceleration;
self.index += 2;
} else { } else {
match token.token_type { match token.token_type {
TokenType::Identifier => { TokenType::Identifier => {
+36 -5
View File
@@ -1,7 +1,18 @@
use crate::lexer::{lex, LexerState, TokenType}; use crate::lexer::{lex, LexerState, TokenType};
use crate::parser::{Ast, AstType, Parser}; use crate::parser::{Ast, AstType, Parser};
pub fn transpile(input: String, indent: u32, state: LexerState) -> String { #[derive(Debug, PartialEq, Clone, Copy)]
pub struct Options {
auto_mut: bool
}
impl Default for Options {
fn default() -> Options {
Options { auto_mut: true }
}
}
pub fn transpile(input: String, indent: u32, state: LexerState, options: Options) -> String {
let mut result = String::new(); let mut result = String::new();
if indent == 0 { if indent == 0 {
@@ -35,7 +46,8 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
LexerState { LexerState {
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
} },
options
) )
) )
.as_str(); .as_str();
@@ -53,13 +65,32 @@ pub fn transpile(input: String, indent: u32, state: LexerState) -> String {
LexerState { LexerState {
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
} },
options.clone()
) )
) )
.as_str(); .as_str();
} else if ast.ast_type == AstType::StructDeceleration {
result += format!(
"struct {} {} {}",
ast.tokens[0].value,
"{\n",
transpile_round(
ast.tokens[1].value.clone(),
LexerState {
line: ast.tokens[1].line,
column: ast.tokens[1].column
}
).trim_end()
).replace("\n", ("\n".to_string() + " ".repeat(((indent+1) as usize)*2).as_str()).as_str())
.as_str();
result += "\n}\n";
} else if ast.ast_type == AstType::VariableDeceleration { } else if ast.ast_type == AstType::VariableDeceleration {
result += format!("let mut {}: {}", ast.tokens[1].value, ast.tokens[0].value).as_str(); if options.auto_mut {
// result += format!("let {}: {}", 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 {
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).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 {