global incl

This commit is contained in:
2024-05-24 16:47:02 +02:00
parent ae281d70d1
commit 5ca3722150
4 changed files with 45 additions and 15 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
use regex::Regex; use regex::Regex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::fmt; use std::fmt;
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct LexerState { pub struct LexerState {
pub line: usize, pub line: usize,
pub column: usize pub column: usize
@@ -72,7 +73,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|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|for|in|as)\b").unwrap())
}, },
Node { Node {
token_type: TokenType::Identifier, token_type: TokenType::Identifier,
+6 -7
View File
@@ -2,7 +2,7 @@ mod transpiler;
mod parser; mod parser;
mod lexer; mod lexer;
mod compile; mod compile;
use std::fs; use std::{env::set_current_dir, fs};
use clap::Parser; use clap::Parser;
use lexer::LexerState; use lexer::LexerState;
use transpiler::Options; use transpiler::Options;
@@ -25,7 +25,8 @@ 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 }, Options::default()); fs::create_dir("wyst_tmp").expect("error making wyst_tmp");
let transpiled_code = transpiler::transpile(file_content, 0, LexerState { line: 1, column: 0 }, &mut Options::default());
match args.rust { match args.rust {
Some(ref rust_file_name) => { Some(ref rust_file_name) => {
@@ -43,13 +44,11 @@ fn main() {
match args.compile { match args.compile {
Some(ref exe_name) => { Some(ref exe_name) => {
// Compile transpiled Rust code into executable // Compile transpiled Rust code into executable
compile::write_to_rust_file(&transpiled_code, "temp.rs") compile::write_to_rust_file(&transpiled_code, "wyst_tmp/temp.rs")
.expect("Error writing to temporary Rust file"); .expect("Error writing to temporary Rust file");
compile::compile_to_executable("temp.rs", exe_name) compile::compile_to_executable("wyst_tmp/temp.rs", exe_name)
.expect("Error compiling to executable"); .expect("Error compiling to executable");
fs::remove_file("temp.rs") fs::remove_dir_all("wyst_tmp").expect("err rm wyst_tmp");
.expect("Error removing temporary Rust file");
println!("Compiled executable: {}", exe_name);
} }
None => {} None => {}
} }
+36 -6
View File
@@ -1,3 +1,4 @@
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};
@@ -5,16 +6,36 @@ use crate::parser::{Ast, AstType, Parser};
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
} }
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")] } Options { auto_mut: true, auto_macro: true, macros: vec![String::from("println")], modnum: 0 }
} }
} }
pub fn transpile(input: String, indent: u32, state: LexerState, options: Options) -> String { fn clean_incl(input: &str) -> String {
input
.chars()
.map(|c| if c.is_alphanumeric() || c == '_' { c } else { '_' })
.collect()
}
pub fn transpile_mod(ast: Ast, options: &mut Options) -> String {
let modfile = ast.tokens[0].value.as_str();
let modname = format!("{}_{}", clean_incl(modfile.split(".").collect::<Vec<_>>()[0]), options.clone().modnum);
let file_content = fs::read_to_string("lib/".to_string()+modfile)
.expect("Error reading 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
}
pub fn transpile(input: String, indent: u32, state: LexerState, options: &mut Options) -> String {
let mut result = String::new(); let mut result = String::new();
if indent == 0 { if indent == 0 {
@@ -61,7 +82,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
}, },
options.clone() &mut options.clone()
) )
) )
.as_str(); .as_str();
@@ -80,7 +101,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
line: ast.tokens[3].line, line: ast.tokens[3].line,
column: ast.tokens[3].column column: ast.tokens[3].column
}, },
options.clone() &mut options.clone()
) )
) )
.as_str(); .as_str();
@@ -100,7 +121,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
.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.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();
@@ -150,6 +171,15 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
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 {
let modname = transpile_mod(ast, options);
result += "mod ";
result += modname.as_str();
result += ";\n";
result += "use ";
result += modname.as_str();
result += "::*;\n";
options.modnum += 1;
} // flp } // flp
else { else {
if ast.tokens[0].token_type == TokenType::Newline { if ast.tokens[0].token_type == TokenType::Newline {