diff --git a/Cargo.toml b/Cargo.toml index e2ca43b..5f466cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ zip = "2.1.3" dirs = "5.0.1" pest = "2" pest_derive = "2.1" -clap = "3" \ No newline at end of file +clap = "3" +hex = "0.4" diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 0000000..d41d038 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,4 @@ +# Wyst stdlib + +## > [!NOTE] +> any changes made here should be documented before pushed to master diff --git a/lib/stdio.rs b/lib/stdio.rs index 2a7ac7d..1e2f3de 100644 --- a/lib/stdio.rs +++ b/lib/stdio.rs @@ -1,6 +1,7 @@ #[macro_export] -macro_rules! printf { + +macro_rules! _0x7072696e7466 { ($($arg:tt)*) => { - println!($($arg)*); + print!($($arg)*); }; -} \ No newline at end of file +} diff --git a/src/compiler.rs b/src/compiler.rs index c79f8ab..b4270d9 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,5 +1,5 @@ -use std::{fs, path::Path, process::Command}; use std::str; +use std::{fs, path::Path, process::Command}; use crate::{ transpiler::{State, Transpiler}, @@ -15,8 +15,11 @@ pub fn transpile_file(input_file: String, output_file: String, is_main: bool) -> let mut module_code = String::new(); let mut output_code = String::new(); match trsp.transpile_code(file_contents, 0) { - Ok(rcode) => output_code+=rcode.as_str(), - Err(e) => {ecode=1;println!("{e}")} + Ok(rcode) => output_code += rcode.as_str(), + Err(e) => { + ecode = 1; + println!("{e}") + } } if is_main { for module in trsp.clone().mod_manager.modules { @@ -25,7 +28,12 @@ pub fn transpile_file(input_file: String, output_file: String, is_main: bool) -> module_code += ";\n"; } } - output_code = format!("{module_code}\n{output_code}"); + output_code = format!( + "{module_code}\n{output_code}\nfn main() {}{}{}", + "{_0x", + hex::encode("main"), + "();}" + ); if let Ok(_) = fs::write(Path::new("build").join(output_file), output_code) { trsp.problems.append(&mut trsp.mod_manager.write()); } @@ -74,6 +82,9 @@ pub fn compile_rust(exe_file: String) { if &output.stderr.len() == &0 { println!("Compiled successfully: \x1b[34;4m{}", exe_file.clone()); } else { - println!("{}", str::from_utf8(&output.stderr).expect("Error encoding stderr")); + println!( + "{}", + str::from_utf8(&output.stderr).expect("Error encoding stderr") + ); } } diff --git a/src/transpiler.rs b/src/transpiler.rs index 8718927..ef83d54 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -1,9 +1,9 @@ -use pest::iterators::{Pair, Pairs}; use crate::utils::{ModManager, ProblemCap}; +use pest::iterators::{Pair, Pairs}; use serde::{Deserialize, Serialize}; // The Parser -use pest::{Parser, error::Error}; +use pest::{error::Error, Parser}; use pest_derive::Parser; #[derive(Parser)] #[grammar = "wyst.pest"] @@ -61,16 +61,17 @@ impl Default for Transpiler { pub fn to_indexable(pairs: Pairs) -> Vec> { let mut arr: Vec> = Vec::new(); - for pair in pairs {arr.push(pair)} + for pair in pairs { + arr.push(pair) + } arr - } impl Transpiler { pub fn transpile_pairs(&mut self, pairs: Pairs) -> String { let mut res = String::new(); for pair in pairs { - res+=self.transpile(pair).as_str(); + res += self.transpile(pair).as_str(); } res } @@ -79,11 +80,15 @@ impl Transpiler { let tokens = to_indexable(pair.clone().into_inner()); match pair.as_rule() { Rule::func_def => { - res += format!("pub fn {}({}) -> {} {}\n", self.transpile(tokens[1].clone()), - self.transpile_pairs(tokens[2].clone().into_inner()).as_str(), - self.transpile(tokens[0].clone()), - self.transpile(tokens[3].clone()) - ).as_str(); + res += format!( + "pub fn {}({}) -> {} {}\n", + self.transpile(tokens[1].clone()), + self.transpile_pairs(tokens[2].clone().into_inner()) + .as_str(), + self.transpile(tokens[0].clone()), + self.transpile(tokens[3].clone()) + ) + .as_str(); } Rule::var_def => { res += format!("let mut {}: {}", tokens[1].as_str(), tokens[0].as_str()).as_str(); @@ -99,36 +104,35 @@ impl Transpiler { Rule::include_global => { res += "#[allow(unused_imports)]\nuse "; - res += self.mod_manager.add(tokens[0].as_str().to_string(), true).as_str(); + res += self + .mod_manager + .add(tokens[0].as_str().to_string(), true) + .as_str(); res += "::*;\n"; } Rule::include => { res += "#[allow(unused_imports)]\nuse "; - res += self.mod_manager.add(tokens[0].as_str().to_string(), false).as_str(); + res += self + .mod_manager + .add(tokens[0].as_str().to_string(), false) + .as_str(); res += "::*;\n"; } - Rule::identifier => { + Rule::identifier | Rule::def_identifier => { let ident = pair.as_str().trim(); - if self.mod_manager.macros.contains(&ident.to_string()) { - res+=ident; - res+="!"; + let ident_rs = format!("_0x{}", hex::encode(ident.as_bytes())); + if self.mod_manager.macros.contains(&ident_rs) { + res += ident_rs.as_str(); + res += "!"; } else { - res+=match ident { - "void" => {"()"} - _ => {ident} + res += match ident { + "void" => "()", + _ => ident_rs.as_str(), }; } } - Rule::def_identifier => { - let ident = pair.as_str().trim(); - res+=match ident { - "void" => {"()"} - _ => {ident} - }; - } - Rule::call => { res += " "; res += self.transpile(tokens[0].clone()).as_str(); diff --git a/src/wyst.pest b/src/wyst.pest index ff665ab..761a618 100644 --- a/src/wyst.pest +++ b/src/wyst.pest @@ -72,4 +72,4 @@ top_expr = { include_global | func_def )* ~ !ANY -} \ No newline at end of file +}