dll alpha_01

This commit is contained in:
2024-06-21 19:01:51 +02:00
parent 1490a367c2
commit 9a614ef367
5 changed files with 72 additions and 76 deletions
+62 -41
View File
@@ -1,5 +1,5 @@
mod compile;
mod dll_reader;
mod dllmgr;
mod file_writer;
mod lexer;
mod lsp;
@@ -10,19 +10,21 @@ mod variable;
use clap::Parser;
use std::{fs, path::Path};
use transpiler::Transpiler;
use variable::Variables;
use variable::{Variable, Variables};
use crate::lsp::run_lsp_server;
// Arguments for Wyst (short and long version)
#[derive(Parser)]
struct Args {
#[clap(short, long)]
rust: Option<String>,
//#[clap(short, long)]
//rust: Option<String>,
#[clap(short, long)]
compile: Option<String>,
#[clap(short, long)]
dll: Option<String>,
#[clap(long)]
stdio: bool,
}
@@ -34,44 +36,25 @@ fn main() {
run_lsp_server();
}
false => {
let file_content = fs::read_to_string("main.wt").expect("Error reading file");
if Path::new("build").exists() {
fs::remove_dir_all("build").expect("err rm build");
}
fs::create_dir("build").expect("error making build");
let mut trsp = Transpiler::default();
let mut vars = Variables::new();
let mut transpiled_code = trsp.transpile(file_content, 0, &mut vars);
transpiled_code += "\nfn main() {";
transpiled_code += "std::process::exit(";
transpiled_code += vars.get_var("main".to_string(), &mut trsp).as_str();
transpiled_code += "())}";
for problem in trsp.problems {
println!("{}", problem.problem_msg)
}
trsp.writer.write();
match args.rust {
Some(ref rust_file_name) => {
// Write transpiled code to Rust file
compile::write_to_rust_file(
&transpiled_code,
("build/".to_string() + "main.rs").as_str(),
)
.expect("Error writing to Rust file");
println!("Transpiled code written to {}", rust_file_name);
}
None => {
// No Rust file specified, print transpiled code
if !args.compile.is_some() {
println!("{}", transpiled_code);
}
}
}
match args.compile {
Some(ref exe_name) => {
let file_content = fs::read_to_string("main.wt").expect("Error reading file");
if Path::new("build").exists() {
fs::remove_dir_all("build").expect("err rm build");
}
fs::create_dir("build").expect("error making build");
let mut trsp = Transpiler::default();
let mut vars = Variables::new();
let mut transpiled_code = trsp.transpile(file_content, 0, &mut vars);
transpiled_code += "\nfn main() {";
transpiled_code += "std::process::exit(";
transpiled_code += vars.get_var("main".to_string(), &mut trsp).as_str();
transpiled_code += "())}";
for problem in trsp.problems {
println!("{}", problem.problem_msg)
}
trsp.writer.write();
compile::write_to_rust_file(&transpiled_code, "build/main.rs")
.expect("Error writing to temporary Rust file");
std::env::set_current_dir("build").expect("setDir err: ");
@@ -84,6 +67,44 @@ fn main() {
}
None => {}
}
match args.dll {
Some(ref dll_path) => {
let file_content = fs::read_to_string("lib.wt").expect("Error reading file");
if Path::new("build").exists() {
fs::remove_dir_all("build").expect("err rm build");
}
fs::create_dir("build").expect("error making build");
let mut trsp = Transpiler::default();
let mut vars = Variables::new();
let transpiled_code = trsp.transpile(file_content, 0, &mut vars);
for problem in trsp.problems {
println!("{}", problem.problem_msg)
}
trsp.writer.write();
let mut dll_main = String::from("fn call_fn(params: Vec<Param>){");
for (name, var) in vars.vars.clone() {
let mut dparams = String::new();
for i in 0..var.params.vars.len() {
dparams += format!("params.get({}).expect(\"Err_prms\"),", i).as_str();
}
dll_main += format!(
"\"{}\" => {}return {}({dparams});{}",
name, "{", var.rname, "}"
)
.as_str();
}
dll_main += "}";
compile::write_to_rust_file(&transpiled_code, "build/wslib.rs")
.expect("Error writing to temporary Rust file");
compile::write_to_rust_file(&dll_main, "build/main.rs")
.expect("Error writing to main dll");
std::env::set_current_dir("build").expect("setDir err: ");
compile::compile_to_executable("run").expect("Error compiling to executable");
dllmgr::write_dll(vars, "run".to_string(), dll_path.to_string());
// fs::remove_dir_all("build").expect("err rm build");
}
None => {}
}
}
}
}