2024-03-27 19:27:11 +01:00
|
|
|
//! The `ShulkerScript` language.
|
|
|
|
//!
|
|
|
|
//! `ShulkerScript` is a simple, imperative scripting language for creating Minecraft data packs.
|
|
|
|
|
|
|
|
#![deny(
|
|
|
|
missing_debug_implementations,
|
|
|
|
missing_copy_implementations,
|
|
|
|
clippy::nursery,
|
|
|
|
rustdoc::broken_intra_doc_links,
|
|
|
|
clippy::missing_errors_doc
|
|
|
|
)]
|
2024-03-27 21:39:56 +01:00
|
|
|
#![warn(missing_docs, clippy::all, clippy::pedantic)]
|
2024-03-27 19:27:11 +01:00
|
|
|
#![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)]
|
|
|
|
|
2024-04-06 14:42:44 +02:00
|
|
|
pub use shulkerbox;
|
|
|
|
|
2024-03-27 19:27:11 +01:00
|
|
|
pub mod base;
|
|
|
|
pub mod lexical;
|
2024-03-27 21:39:56 +01:00
|
|
|
pub mod syntax;
|
2024-04-03 01:27:02 +02:00
|
|
|
pub mod transpile;
|
2024-03-27 19:27:11 +01:00
|
|
|
|
2024-04-01 20:42:38 +02:00
|
|
|
use std::{cell::Cell, fmt::Display, path::Path};
|
2024-03-27 19:27:11 +01:00
|
|
|
|
|
|
|
use base::{source_file::SourceFile, Handler, Result};
|
2024-04-06 21:49:25 +02:00
|
|
|
use syntax::syntax_tree::program::ProgramFile;
|
2024-04-05 12:59:21 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "shulkerbox")]
|
2024-04-03 01:27:02 +02:00
|
|
|
use transpile::transpiler::Transpiler;
|
2024-03-27 19:27:11 +01:00
|
|
|
|
2024-04-03 00:45:34 +02:00
|
|
|
#[cfg(feature = "shulkerbox")]
|
|
|
|
use shulkerbox::{datapack::Datapack, util::compile::CompileOptions, virtual_fs::VFolder};
|
|
|
|
|
2024-03-27 21:39:56 +01:00
|
|
|
use crate::{base::Error, lexical::token_stream::TokenStream, syntax::parser::Parser};
|
2024-03-27 19:27:11 +01:00
|
|
|
|
2024-04-01 20:42:38 +02:00
|
|
|
/// Converts the given source code to tokens.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// - If an error occurs while reading the file.
|
|
|
|
pub fn tokenize(path: &Path) -> Result<TokenStream> {
|
|
|
|
let source_file = SourceFile::load(path)?;
|
|
|
|
|
|
|
|
let printer = Printer::new();
|
|
|
|
|
|
|
|
Ok(TokenStream::tokenize(&source_file, &printer))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the given source code.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// - If an error occurs while reading the file.
|
|
|
|
/// - If an error occurs while parsing the source code.
|
2024-04-06 21:49:25 +02:00
|
|
|
pub fn parse(path: &Path) -> Result<ProgramFile> {
|
2024-04-01 20:42:38 +02:00
|
|
|
let source_file = SourceFile::load(path)?;
|
|
|
|
|
|
|
|
let printer = Printer::new();
|
|
|
|
|
|
|
|
let tokens = TokenStream::tokenize(&source_file, &printer);
|
|
|
|
|
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while tokenizing the source code.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parser = Parser::new(&tokens);
|
|
|
|
let program = parser.parse_program(&printer).ok_or(Error::Other(
|
|
|
|
"An error occured while parsing the source code.",
|
|
|
|
))?;
|
|
|
|
|
2024-04-03 00:45:34 +02:00
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while parsing the source code.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:42:38 +02:00
|
|
|
Ok(program)
|
|
|
|
}
|
|
|
|
|
2024-04-03 00:45:34 +02:00
|
|
|
/// Transpiles the given source code into a shulkerbox [`Datapack`].
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// - If an error occurs while reading the file.
|
|
|
|
/// - If an error occurs while parsing the source code.
|
|
|
|
/// - If an error occurs while transpiling the source code.
|
|
|
|
#[cfg(feature = "shulkerbox")]
|
2024-06-09 21:22:48 +02:00
|
|
|
pub fn transpile<P>(script_paths: &[(String, P)]) -> Result<Datapack>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
2024-04-03 00:45:34 +02:00
|
|
|
let printer = Printer::new();
|
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let programs = script_paths
|
|
|
|
.iter()
|
|
|
|
.map(|(program_identifier, path)| {
|
|
|
|
let source_file = SourceFile::load(path.as_ref())?;
|
2024-04-03 00:45:34 +02:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let tokens = TokenStream::tokenize(&source_file, &printer);
|
|
|
|
|
|
|
|
// println!("tokens: {tokens:#?}");
|
|
|
|
|
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while tokenizing the source code.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parser = Parser::new(&tokens);
|
|
|
|
let program = parser.parse_program(&printer).ok_or(Error::Other(
|
|
|
|
"An error occured while parsing the source code.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while parsing the source code.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((program_identifier, program))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if programs.iter().any(Result::is_err) {
|
|
|
|
return Err(programs.into_iter().find_map(Result::err).unwrap());
|
2024-04-03 00:45:34 +02:00
|
|
|
}
|
2024-06-09 21:22:48 +02:00
|
|
|
let programs = programs
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.collect::<Vec<_>>();
|
2024-04-03 00:45:34 +02:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let mut transpiler = Transpiler::new(27);
|
|
|
|
transpiler.transpile(&programs, &printer)?;
|
|
|
|
let datapack = transpiler.into_datapack();
|
|
|
|
|
|
|
|
// println!("datapack: {datapack:#?}");
|
2024-04-03 00:45:34 +02:00
|
|
|
|
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
2024-06-09 21:22:48 +02:00
|
|
|
"An error occurred while transpiling the source code.",
|
2024-04-03 00:45:34 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(datapack)
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:27:11 +01:00
|
|
|
/// Compiles the given source code.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// - If an error occurs while reading the file.
|
2024-04-03 00:45:34 +02:00
|
|
|
/// - If an error occurs while parsing the source code.
|
|
|
|
/// - If an error occurs while transpiling the source code.
|
|
|
|
#[cfg(feature = "shulkerbox")]
|
2024-06-09 21:22:48 +02:00
|
|
|
pub fn compile<P>(script_paths: &[(String, P)]) -> Result<VFolder>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
2024-03-27 19:27:11 +01:00
|
|
|
let printer = Printer::new();
|
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let programs = script_paths
|
|
|
|
.iter()
|
|
|
|
.map(|(program_identifier, path)| {
|
|
|
|
let source_file = SourceFile::load(path.as_ref())?;
|
2024-03-27 19:27:11 +01:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let tokens = TokenStream::tokenize(&source_file, &printer);
|
2024-03-29 18:26:43 +01:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
// println!("tokens: {tokens:#?}");
|
2024-03-27 19:27:11 +01:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while tokenizing the source code.",
|
|
|
|
));
|
|
|
|
}
|
2024-03-27 21:39:56 +01:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let mut parser = Parser::new(&tokens);
|
|
|
|
let program = parser.parse_program(&printer).ok_or(Error::Other(
|
|
|
|
"An error occured while parsing the source code.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
|
|
|
"An error occurred while parsing the source code.",
|
|
|
|
));
|
|
|
|
}
|
2024-04-03 00:45:34 +02:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
Ok((program_identifier, program))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if programs.iter().any(Result::is_err) {
|
|
|
|
return Err(programs.into_iter().find_map(Result::err).unwrap());
|
|
|
|
}
|
|
|
|
let programs = programs
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.collect::<Vec<_>>();
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-06-09 21:22:48 +02:00
|
|
|
let mut transpiler = Transpiler::new(27);
|
|
|
|
transpiler.transpile(&programs, &printer)?;
|
2024-04-05 12:59:21 +02:00
|
|
|
let datapack = transpiler.into_datapack();
|
2024-03-27 21:39:56 +01:00
|
|
|
|
2024-04-03 00:45:34 +02:00
|
|
|
// println!("datapack: {datapack:#?}");
|
|
|
|
|
2024-03-27 21:39:56 +01:00
|
|
|
if printer.has_printed() {
|
|
|
|
return Err(Error::Other(
|
2024-04-03 00:45:34 +02:00
|
|
|
"An error occurred while transpiling the source code.",
|
2024-03-27 21:39:56 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-03-28 01:17:17 +01:00
|
|
|
Ok(datapack.compile(&CompileOptions::default()))
|
2024-03-27 19:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Printer {
|
|
|
|
printed: Cell<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Printer {
|
|
|
|
/// Creates a new [`Printer`].
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
printed: Cell::new(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_printed(&self) -> bool {
|
|
|
|
self.printed.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Display> Handler<E> for Printer {
|
|
|
|
fn receive(&self, error: E) {
|
|
|
|
eprintln!("{error}");
|
|
|
|
self.printed.set(true);
|
|
|
|
}
|
|
|
|
}
|