Split yunq compliation to library and use it in build process.

This commit is contained in:
Drew Galbraith 2024-07-28 18:08:34 -07:00
parent 370ba9ae40
commit 1e073d5060
7 changed files with 89 additions and 28 deletions

50
rust/Cargo.lock generated
View File

@ -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",
]

View File

@ -8,3 +8,6 @@ mammoth = { path = "../mammoth" }
yunq = {path = "../yunq"}
yunq-derive = {path = "../yunq-derive"}
[build-dependencies]
yunqc = {path = "../../../yunq/rust"}

View File

@ -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.");
}

2
yunq/rust/Cargo.lock generated
View File

@ -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",

View File

@ -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"]

15
yunq/rust/src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
mod codegen;
mod lexer;
mod parser;
use std::error::Error;
pub fn codegen(input: &str) -> Result<String, Box<dyn Error>> {
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()))
}

View File

@ -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<dyn Error>> {
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)?;