add example compiler to demonstrate basic functionality and usage

This commit is contained in:
Moritz Hölting 2025-02-17 23:05:51 +01:00
parent 96fe865ac1
commit 19c55b78f4
2 changed files with 31 additions and 0 deletions

View File

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Macro strings
- Function parameters/arguments
- Example: barebones compiler
### Changed

30
examples/compiler.rs Normal file
View File

@ -0,0 +1,30 @@
//! This example demonstrates how to compile a shulkerscript file into a datapack using the shulkerscript compiler.
//! Most basic version of a shulkerscript compiler, which takes a single input file and places the resulting datapack in the specified output directory.
//!
//! For a ready-to-use compiler, see the `shulkerscript-cli` crate.
use shulkerscript::{
base::{FsProvider, PrintHandler},
compile,
};
#[cfg(not(feature = "shulkerbox"))]
compile_error!("Need feature 'shulkerbox' to compile this example");
fn main() {
let mut args = std::env::args();
let _ = args.next().unwrap();
let input = args.next().expect("Expect path to shulkerscript file");
let output = args.next().expect("Expect path to output directory");
let code = compile(
&PrintHandler::new(),
&FsProvider::default(),
shulkerbox::datapack::Datapack::LATEST_FORMAT,
&[("main".to_string(), &input)],
)
.expect("failed to compile");
code.place(output).expect("failed to place datapack");
}