diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 52c6fef..837436a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -8,6 +8,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "linked_list_allocator" version = "0.10.5" @@ -34,6 +43,16 @@ dependencies = [ "linked_list_allocator", ] +[[package]] +name = "prettyplease" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" +dependencies = [ + "proc-macro2", + "syn 2.0.72", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -78,6 +97,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "testbed" version = "0.1.0" @@ -92,6 +122,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + [[package]] name = "yellowstone" version = "0.1.0" @@ -99,6 +135,7 @@ dependencies = [ "mammoth", "yunq", "yunq-derive", + "yunqc", ] [[package]] @@ -115,5 +152,16 @@ version = "0.1.0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", +] + +[[package]] +name = "yunqc" +version = "0.1.0" +dependencies = [ + "convert_case", + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.72", ] diff --git a/rust/lib/yellowstone/Cargo.toml b/rust/lib/yellowstone/Cargo.toml index c464a9e..59ea732 100644 --- a/rust/lib/yellowstone/Cargo.toml +++ b/rust/lib/yellowstone/Cargo.toml @@ -8,3 +8,6 @@ mammoth = { path = "../mammoth" } yunq = {path = "../yunq"} yunq-derive = {path = "../yunq-derive"} +[build-dependencies] +yunqc = {path = "../../../yunq/rust"} + diff --git a/rust/lib/yellowstone/build.rs b/rust/lib/yellowstone/build.rs index a072a3a..24472ac 100644 --- a/rust/lib/yellowstone/build.rs +++ b/rust/lib/yellowstone/build.rs @@ -1,18 +1,14 @@ -use std::process::Command; +use std::fs; fn main() { + let input_file = "../../../sys/yellowstone/lib/yellowstone/yellowstone.yunq"; + + println!("cargo::rerun-if-changed={input_file}"); + + let input = fs::read_to_string(input_file).expect("Failed to read input file"); + + let code = yunqc::codegen(&input).expect("Failed to generate yunq code."); + let out = std::env::var("OUT_DIR").unwrap() + "/yunq.rs"; - - let status = Command::new("cargo") - .current_dir("../../../yunq/rust") - .arg("run") - .arg("--") - .arg("--input-path") - .arg("../../sys/yellowstone/lib/yellowstone/yellowstone.yunq") - .arg("--output-path") - .arg(out) - .status() - .expect("Failed to start execution"); - - assert!(status.success()); + fs::write(out, code).expect("Failed to write generated code."); } diff --git a/yunq/rust/Cargo.lock b/yunq/rust/Cargo.lock index 145ddd8..cfaa503 100644 --- a/yunq/rust/Cargo.lock +++ b/yunq/rust/Cargo.lock @@ -255,7 +255,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] -name = "yunq" +name = "yunqc" version = "0.1.0" dependencies = [ "clap", diff --git a/yunq/rust/Cargo.toml b/yunq/rust/Cargo.toml index 2550596..a972f93 100644 --- a/yunq/rust/Cargo.toml +++ b/yunq/rust/Cargo.toml @@ -1,12 +1,20 @@ [package] -name = "yunq" +name = "yunqc" version = "0.1.0" edition = "2021" [dependencies] -clap = { version = "4.5.7", features = ["derive"] } convert_case = "0.6.0" prettyplease = "0.2.20" proc-macro2 = { version = "1.0" } quote = { version = "1.0" } syn = "2.0.72" + +clap = { version = "4.5.7", features = ["derive"], optional = true} + +[features] +build-binary = ["clap"] + +[[bin]] +name = "yunqc" +required-features = ["build-binary"] diff --git a/yunq/rust/src/lib.rs b/yunq/rust/src/lib.rs new file mode 100644 index 0000000..07ecdb9 --- /dev/null +++ b/yunq/rust/src/lib.rs @@ -0,0 +1,15 @@ +mod codegen; +mod lexer; +mod parser; + +use std::error::Error; + +pub fn codegen(input: &str) -> Result> { + let tokens = lexer::lex_input(input)?; + + let mut ast_parser = parser::Parser::new(&tokens); + ast_parser.parse_ast()?; + ast_parser.type_check()?; + + Ok(codegen::generate_code(ast_parser.ast())) +} diff --git a/yunq/rust/src/main.rs b/yunq/rust/src/main.rs index 21ece22..a835985 100644 --- a/yunq/rust/src/main.rs +++ b/yunq/rust/src/main.rs @@ -1,7 +1,3 @@ -mod codegen; -mod lexer; -mod parser; - use std::error::Error; use std::fs; @@ -22,13 +18,8 @@ struct Args { fn main() -> Result<(), Box> { let args = Args::parse(); let input = fs::read_to_string(args.input_path)?; - let tokens = lexer::lex_input(&input)?; - let mut ast_parser = parser::Parser::new(&tokens); - ast_parser.parse_ast()?; - ast_parser.type_check()?; - - let code = codegen::generate_code(ast_parser.ast()); + let code = yunqc::codegen(&input)?; fs::write(args.output_path, code)?;