dll alpha_01
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufReader, Read},
|
||||
};
|
||||
|
||||
use zip::{read::ZipFile, ZipArchive};
|
||||
|
||||
use crate::variable::Variables;
|
||||
|
||||
pub fn read_dll(dll_path: String) -> Variables {
|
||||
let mut vars = Variables::new();
|
||||
let dll_file = File::open(dll_path).expect("err_dll_read");
|
||||
let dll_reader = BufReader::new(dll_file);
|
||||
let mut dll_zip = ZipArchive::new(dll_reader).expect("err_archive");
|
||||
for i in 0..dll_zip.len() {
|
||||
let mut buf = Vec::new();
|
||||
let mut file = dll_zip.by_index(i).expect("err_dll_idx");
|
||||
let filename = file.name();
|
||||
if filename == "map.json" && file.is_file() {
|
||||
file.read_to_end(&mut buf).expect("err_read_zip_file");
|
||||
println!(
|
||||
"Filename: {}\nFileContents: {}",
|
||||
file.name(),
|
||||
String::from_utf8_lossy(&buf)
|
||||
);
|
||||
}
|
||||
}
|
||||
vars
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
use std::fs;
|
||||
|
||||
use crate::{dll_reader::read_dll, transpiler::Transpiler, variable::Variables};
|
||||
use crate::{dllmgr::read_dll, transpiler::Transpiler, variable::Variables};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Module {
|
||||
|
||||
+5
-2
@@ -1,7 +1,8 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use serde::Serialize;
|
||||
use std::fmt;
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Serialize)]
|
||||
pub struct LexerState {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
@@ -88,7 +89,9 @@ const SYNTAX: [Node; 16] = [
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Keyword1,
|
||||
token_regex: Lazy::new(|| Regex::new(r"^(impl|namespace|if|for|while|else *if)\b").unwrap()),
|
||||
token_regex: Lazy::new(|| {
|
||||
Regex::new(r"^(impl|namespace|if|for|while|else *if)\b").unwrap()
|
||||
}),
|
||||
},
|
||||
Node {
|
||||
token_type: TokenType::Keyword2,
|
||||
|
||||
+62
-41
@@ -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 => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand::Rng;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
lexer::LexerState,
|
||||
@@ -8,7 +9,7 @@ use crate::{
|
||||
transpiler::Transpiler,
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
#[derive(Clone, PartialEq, Debug, Serialize)]
|
||||
pub enum VariableType {
|
||||
Var,
|
||||
Func,
|
||||
@@ -17,7 +18,7 @@ pub enum VariableType {
|
||||
Namespace,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Variable {
|
||||
pub vtype: VariableType,
|
||||
pub desc: String,
|
||||
@@ -26,7 +27,7 @@ pub struct Variable {
|
||||
pub rname: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Variables {
|
||||
pub vars: HashMap<String, Variable>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user