2024-03-28 01:17:17 +01:00
|
|
|
//! Compiler for `ShulkerScript`
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-04-03 13:46:57 +02:00
|
|
|
use shulkerbox::datapack::{self, Command, Datapack, Execute};
|
2024-03-28 01:17:17 +01:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
base::{source_file::SourceElement, Handler},
|
|
|
|
syntax::syntax_tree::{declaration::Declaration, program::Program, statement::Statement},
|
|
|
|
};
|
|
|
|
|
2024-04-03 01:27:02 +02:00
|
|
|
use super::error::{self, TranspileError};
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-04-03 01:27:02 +02:00
|
|
|
/// A transpiler for `ShulkerScript`.
|
2024-04-05 12:59:21 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-04-03 01:27:02 +02:00
|
|
|
pub struct Transpiler {
|
2024-04-05 12:59:21 +02:00
|
|
|
datapack: shulkerbox::datapack::Datapack,
|
|
|
|
functions: HashMap<String, FunctionData>,
|
|
|
|
function_locations: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct FunctionData {
|
|
|
|
namespace: String,
|
|
|
|
statements: Vec<Statement>,
|
|
|
|
annotations: HashMap<String, Option<String>>,
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-03 01:27:02 +02:00
|
|
|
impl Transpiler {
|
2024-04-03 13:46:57 +02:00
|
|
|
/// Creates a new transpiler.
|
2024-03-28 01:17:17 +01:00
|
|
|
#[must_use]
|
2024-04-05 12:59:21 +02:00
|
|
|
pub fn new(pack_name: &str, pack_format: u8) -> Self {
|
2024-03-28 01:17:17 +01:00
|
|
|
Self {
|
2024-04-05 12:59:21 +02:00
|
|
|
datapack: shulkerbox::datapack::Datapack::new(pack_name, pack_format),
|
2024-03-28 01:17:17 +01:00
|
|
|
functions: HashMap::new(),
|
2024-04-05 12:59:21 +02:00
|
|
|
function_locations: HashMap::new(),
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:59:21 +02:00
|
|
|
/// Consumes the transpiler and returns the resulting datapack.
|
|
|
|
#[must_use]
|
|
|
|
pub fn into_datapack(self) -> Datapack {
|
|
|
|
self.datapack
|
|
|
|
}
|
|
|
|
|
2024-04-03 13:46:57 +02:00
|
|
|
/// Transpiles the given program.
|
2024-03-28 01:17:17 +01:00
|
|
|
///
|
|
|
|
/// # Errors
|
2024-04-03 01:27:02 +02:00
|
|
|
/// - [`TranspileError::MissingMainFunction`] If the main function is missing.
|
|
|
|
pub fn transpile(
|
2024-03-28 01:17:17 +01:00
|
|
|
&mut self,
|
|
|
|
program: &Program,
|
2024-04-03 01:27:02 +02:00
|
|
|
handler: &impl Handler<error::TranspileError>,
|
2024-04-05 12:59:21 +02:00
|
|
|
) -> Result<(), TranspileError> {
|
2024-03-28 01:17:17 +01:00
|
|
|
for declaration in program.declarations() {
|
2024-04-05 12:59:21 +02:00
|
|
|
self.transpile_declaration(declaration);
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-05 12:59:21 +02:00
|
|
|
self.get_or_transpile_function("main").ok_or_else(|| {
|
2024-04-03 01:27:02 +02:00
|
|
|
handler.receive(TranspileError::MissingMainFunction);
|
2024-04-05 12:59:21 +02:00
|
|
|
TranspileError::MissingMainFunction
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transpiles the given declaration.
|
|
|
|
fn transpile_declaration(&mut self, declaration: &Declaration) {
|
|
|
|
match declaration {
|
|
|
|
Declaration::Function(function) => {
|
|
|
|
let name = function.identifier().span().str().to_string();
|
|
|
|
let statements = function.block().statements().clone();
|
|
|
|
let annotations = function
|
|
|
|
.annotations()
|
|
|
|
.iter()
|
|
|
|
.map(|annotation| {
|
|
|
|
let key = annotation.identifier();
|
|
|
|
let value = annotation.value();
|
|
|
|
(
|
|
|
|
key.span().str().to_string(),
|
|
|
|
value.as_ref().map(|(_, ref v)| v.str_content().to_string()),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
self.functions.insert(
|
|
|
|
name,
|
|
|
|
FunctionData {
|
|
|
|
namespace: "shulkerscript".to_string(),
|
|
|
|
statements,
|
|
|
|
annotations,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-03-28 01:17:17 +01:00
|
|
|
};
|
2024-04-05 12:59:21 +02:00
|
|
|
}
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-04-05 12:59:21 +02:00
|
|
|
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
|
|
|
fn get_or_transpile_function(&mut self, path: &str) -> Option<&str> {
|
|
|
|
let already_transpiled = self.function_locations.get(path);
|
|
|
|
if already_transpiled.is_none() {
|
|
|
|
let function_data = self.functions.get(path)?;
|
|
|
|
let commands = compile_function(&function_data.statements);
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-04-05 12:59:21 +02:00
|
|
|
let function = self
|
|
|
|
.datapack
|
|
|
|
.namespace_mut(&function_data.namespace)
|
|
|
|
.function_mut(path);
|
|
|
|
function.get_commands_mut().extend(commands);
|
|
|
|
|
|
|
|
let function_location =
|
|
|
|
format!("{namespace}:{path}", namespace = function_data.namespace);
|
|
|
|
|
|
|
|
if function_data.annotations.contains_key("tick") {
|
|
|
|
self.datapack.add_tick(&function_location);
|
|
|
|
}
|
|
|
|
if function_data.annotations.contains_key("load") {
|
|
|
|
self.datapack.add_load(&function_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.function_locations
|
|
|
|
.insert(path.to_string(), function_location);
|
2024-04-03 01:27:02 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 12:59:21 +02:00
|
|
|
self.function_locations.get(path).map(String::as_str)
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_function(statements: &[Statement]) -> Vec<Command> {
|
|
|
|
let mut commands = Vec::new();
|
|
|
|
for statement in statements {
|
2024-03-29 18:26:43 +01:00
|
|
|
commands.extend(compile_statement(statement));
|
|
|
|
}
|
|
|
|
commands
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_statement(statement: &Statement) -> Option<Command> {
|
|
|
|
match statement {
|
|
|
|
Statement::LiteralCommand(literal_command) => Some(literal_command.clean_command().into()),
|
|
|
|
Statement::Block(_) => {
|
|
|
|
unreachable!("Only literal commands are allowed in functions at this time.")
|
|
|
|
}
|
|
|
|
Statement::Conditional(cond) => {
|
|
|
|
let (_, cond, block, el) = cond.clone().dissolve();
|
|
|
|
let (_, cond, _) = cond.dissolve();
|
|
|
|
let statements = block.statements();
|
|
|
|
|
|
|
|
let el = el
|
|
|
|
.and_then(|el| {
|
|
|
|
let (_, block) = el.dissolve();
|
|
|
|
let statements = block.statements();
|
|
|
|
if statements.is_empty() {
|
|
|
|
None
|
|
|
|
} else if statements.len() == 1 {
|
|
|
|
compile_statement(&statements[0]).map(|cmd| Execute::Run(Box::new(cmd)))
|
|
|
|
} else {
|
|
|
|
let commands = statements.iter().filter_map(compile_statement).collect();
|
|
|
|
Some(Execute::Runs(commands))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(Box::new);
|
|
|
|
|
|
|
|
if statements.is_empty() {
|
|
|
|
if el.is_none() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Command::Execute(Execute::If(
|
2024-04-03 13:46:57 +02:00
|
|
|
datapack::Condition::from(cond),
|
2024-03-29 18:26:43 +01:00
|
|
|
Box::new(Execute::Runs(Vec::new())),
|
|
|
|
el,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let run = if statements.len() > 1 {
|
|
|
|
let commands = statements.iter().filter_map(compile_statement).collect();
|
|
|
|
Execute::Runs(commands)
|
|
|
|
} else {
|
|
|
|
Execute::Run(Box::new(compile_statement(&statements[0])?))
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Command::Execute(Execute::If(
|
2024-04-03 13:46:57 +02:00
|
|
|
datapack::Condition::from(cond),
|
2024-03-29 18:26:43 +01:00
|
|
|
Box::new(run),
|
|
|
|
el,
|
|
|
|
)))
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-03 00:45:34 +02:00
|
|
|
Statement::DocComment(doccomment) => {
|
|
|
|
let content = doccomment.content();
|
|
|
|
Some(Command::Comment(content.to_string()))
|
|
|
|
}
|
2024-04-03 01:09:13 +02:00
|
|
|
Statement::Grouping(group) => {
|
|
|
|
let statements = group.block().statements();
|
|
|
|
let commands = statements
|
|
|
|
.iter()
|
|
|
|
.filter_map(compile_statement)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if commands.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Command::Group(commands))
|
|
|
|
}
|
|
|
|
}
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
}
|