global incl
This commit is contained in:
+2
-1
@@ -1,6 +1,7 @@
|
||||
use regex::Regex;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::fmt;
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub struct LexerState {
|
||||
pub line: usize,
|
||||
pub column: usize
|
||||
@@ -72,7 +73,7 @@ const SYNTAX: [Node; 14] = [
|
||||
},
|
||||
Node {
|
||||
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 {
|
||||
token_type: TokenType::Identifier,
|
||||
|
||||
+6
-7
@@ -2,7 +2,7 @@ mod transpiler;
|
||||
mod parser;
|
||||
mod lexer;
|
||||
mod compile;
|
||||
use std::fs;
|
||||
use std::{env::set_current_dir, fs};
|
||||
use clap::Parser;
|
||||
use lexer::LexerState;
|
||||
use transpiler::Options;
|
||||
@@ -25,7 +25,8 @@ fn main() {
|
||||
let file_content = fs::read_to_string(&args.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 {
|
||||
Some(ref rust_file_name) => {
|
||||
@@ -43,13 +44,11 @@ fn main() {
|
||||
match args.compile {
|
||||
Some(ref exe_name) => {
|
||||
// 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");
|
||||
compile::compile_to_executable("temp.rs", exe_name)
|
||||
compile::compile_to_executable("wyst_tmp/temp.rs", exe_name)
|
||||
.expect("Error compiling to executable");
|
||||
fs::remove_file("temp.rs")
|
||||
.expect("Error removing temporary Rust file");
|
||||
println!("Compiled executable: {}", exe_name);
|
||||
fs::remove_dir_all("wyst_tmp").expect("err rm wyst_tmp");
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
use crate::lexer::{Token, TokenType};
|
||||
use crate::lexer::{Token, TokenType};
|
||||
use std::fmt;
|
||||
use regex::Regex;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
+36
-6
@@ -1,3 +1,4 @@
|
||||
use std::fs;
|
||||
use crate::lexer::{lex, LexerState, TokenType};
|
||||
use crate::parser::{Ast, AstType, Parser};
|
||||
|
||||
@@ -5,16 +6,36 @@ use crate::parser::{Ast, AstType, Parser};
|
||||
pub struct Options {
|
||||
auto_mut: bool,
|
||||
auto_macro: bool,
|
||||
macros: Vec<String>
|
||||
macros: Vec<String>,
|
||||
modnum: u32
|
||||
}
|
||||
|
||||
impl Default for 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();
|
||||
|
||||
if indent == 0 {
|
||||
@@ -61,7 +82,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
|
||||
line: ast.tokens[3].line,
|
||||
column: ast.tokens[3].column
|
||||
},
|
||||
options.clone()
|
||||
&mut options.clone()
|
||||
)
|
||||
)
|
||||
.as_str();
|
||||
@@ -80,7 +101,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
|
||||
line: ast.tokens[3].line,
|
||||
column: ast.tokens[3].column
|
||||
},
|
||||
options.clone()
|
||||
&mut options.clone()
|
||||
)
|
||||
)
|
||||
.as_str();
|
||||
@@ -100,7 +121,7 @@ pub fn transpile(input: String, indent: u32, state: LexerState, options: Options
|
||||
.as_str();
|
||||
result += "\n}\n";
|
||||
} 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();
|
||||
} else {
|
||||
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 += "}";
|
||||
} 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();
|
||||
} 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
|
||||
else {
|
||||
if ast.tokens[0].token_type == TokenType::Newline {
|
||||
|
||||
Reference in New Issue
Block a user