2024-04-06 14:30:11 +02:00
|
|
|
//! Transpiler for `ShulkerScript`
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-04-06 13:26:34 +02:00
|
|
|
use chksum_md5 as md5;
|
2024-04-05 16:16:12 +02:00
|
|
|
use std::{collections::HashMap, sync::RwLock};
|
2024-03-28 01:17:17 +01:00
|
|
|
|
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},
|
2024-04-05 16:16:12 +02:00
|
|
|
syntax::syntax_tree::{
|
|
|
|
declaration::Declaration,
|
2024-04-06 15:12:20 +02:00
|
|
|
expression::{Expression, FunctionCall, Primary},
|
2024-04-06 21:49:25 +02:00
|
|
|
program::{Namespace, ProgramFile},
|
2024-04-05 16:16:12 +02:00
|
|
|
statement::{Conditional, Statement},
|
|
|
|
},
|
2024-03-28 01:17:17 +01:00
|
|
|
};
|
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
use super::error::{TranspileError, TranspileResult};
|
2024-03-28 01:17:17 +01:00
|
|
|
|
2024-04-03 01:27:02 +02:00
|
|
|
/// A transpiler for `ShulkerScript`.
|
2024-04-05 16:16:12 +02:00
|
|
|
#[derive(Debug)]
|
2024-04-03 01:27:02 +02:00
|
|
|
pub struct Transpiler {
|
2024-04-05 12:59:21 +02:00
|
|
|
datapack: shulkerbox::datapack::Datapack,
|
2024-04-05 16:16:12 +02:00
|
|
|
functions: RwLock<HashMap<String, FunctionData>>,
|
|
|
|
function_locations: RwLock<HashMap<String, String>>,
|
2024-04-05 12:59:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-04-05 16:16:12 +02:00
|
|
|
functions: RwLock::new(HashMap::new()),
|
|
|
|
function_locations: RwLock::new(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-06 14:42:44 +02:00
|
|
|
/// - [`TranspileError::MissingFunctionDeclaration`] If a called function is missing
|
2024-04-03 01:27:02 +02:00
|
|
|
pub fn transpile(
|
2024-03-28 01:17:17 +01:00
|
|
|
&mut self,
|
2024-04-06 21:49:25 +02:00
|
|
|
program: &ProgramFile,
|
2024-04-06 14:30:11 +02:00
|
|
|
handler: &impl Handler<TranspileError>,
|
2024-04-05 12:59:21 +02:00
|
|
|
) -> Result<(), TranspileError> {
|
2024-04-06 21:49:25 +02:00
|
|
|
let namespace = program.namespace();
|
|
|
|
|
2024-03-28 01:17:17 +01:00
|
|
|
for declaration in program.declarations() {
|
2024-04-06 21:49:25 +02:00
|
|
|
self.transpile_declaration(declaration, namespace, handler);
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 13:26:34 +02:00
|
|
|
let mut always_transpile_functions = Vec::new();
|
|
|
|
|
|
|
|
#[allow(clippy::significant_drop_in_scrutinee)]
|
|
|
|
{
|
|
|
|
let functions = self.functions.read().unwrap();
|
|
|
|
for (name, data) in functions.iter() {
|
|
|
|
let always_transpile_function = data.annotations.contains_key("tick")
|
|
|
|
|| data.annotations.contains_key("load")
|
|
|
|
|| data.annotations.contains_key("deobfuscate");
|
|
|
|
if always_transpile_function {
|
|
|
|
always_transpile_functions.push(name.clone());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name in always_transpile_functions {
|
2024-04-06 14:30:11 +02:00
|
|
|
self.get_or_transpile_function(&name, handler)?;
|
2024-04-06 13:26:34 +02:00
|
|
|
}
|
2024-04-05 12:59:21 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transpiles the given declaration.
|
2024-04-06 14:30:11 +02:00
|
|
|
fn transpile_declaration(
|
|
|
|
&mut self,
|
|
|
|
declaration: &Declaration,
|
2024-04-06 21:49:25 +02:00
|
|
|
namespace: &Namespace,
|
2024-04-06 14:30:11 +02:00
|
|
|
_handler: &impl Handler<TranspileError>,
|
|
|
|
) {
|
2024-04-05 12:59:21 +02:00
|
|
|
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();
|
2024-04-06 13:26:34 +02:00
|
|
|
self.functions.write().unwrap().insert(
|
2024-04-05 12:59:21 +02:00
|
|
|
name,
|
|
|
|
FunctionData {
|
2024-04-06 21:49:25 +02:00
|
|
|
namespace: namespace.namespace_name().str_content().to_string(),
|
2024-04-05 12:59:21 +02:00
|
|
|
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.
|
2024-04-05 16:16:12 +02:00
|
|
|
/// Returns the location of the function or None if the function does not exist.
|
|
|
|
#[allow(clippy::significant_drop_tightening)]
|
2024-04-06 14:30:11 +02:00
|
|
|
fn get_or_transpile_function(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
handler: &impl Handler<TranspileError>,
|
|
|
|
) -> TranspileResult<String> {
|
2024-04-05 16:16:12 +02:00
|
|
|
let already_transpiled = {
|
|
|
|
let locations = self.function_locations.read().unwrap();
|
2024-04-06 13:26:34 +02:00
|
|
|
locations.get(name).is_some()
|
2024-04-05 16:16:12 +02:00
|
|
|
};
|
|
|
|
if !already_transpiled {
|
|
|
|
let statements = {
|
|
|
|
let functions = self.functions.read().unwrap();
|
2024-04-06 14:30:11 +02:00
|
|
|
let function_data = functions.get(name).ok_or_else(|| {
|
|
|
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
|
|
|
handler.receive(error.clone());
|
|
|
|
error
|
|
|
|
})?;
|
2024-04-05 16:16:12 +02:00
|
|
|
function_data.statements.clone()
|
|
|
|
};
|
2024-04-06 14:30:11 +02:00
|
|
|
let commands = self.transpile_function(&statements, handler)?;
|
2024-04-05 16:16:12 +02:00
|
|
|
|
|
|
|
let functions = self.functions.read().unwrap();
|
2024-04-06 14:30:11 +02:00
|
|
|
let function_data = functions.get(name).ok_or_else(|| {
|
|
|
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
|
|
|
handler.receive(error.clone());
|
|
|
|
error
|
|
|
|
})?;
|
2024-04-06 13:26:34 +02:00
|
|
|
|
|
|
|
let modified_name = function_data
|
|
|
|
.annotations
|
|
|
|
.get("deobfuscate")
|
|
|
|
.map_or_else(
|
|
|
|
|| Some("shu/".to_string() + &md5::hash(name).to_hex_lowercase()[..16]),
|
|
|
|
Clone::clone,
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|| name.to_string());
|
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)
|
2024-04-06 13:26:34 +02:00
|
|
|
.function_mut(&modified_name);
|
2024-04-05 12:59:21 +02:00
|
|
|
function.get_commands_mut().extend(commands);
|
|
|
|
|
2024-04-06 13:26:34 +02:00
|
|
|
let function_location = format!(
|
|
|
|
"{namespace}:{modified_name}",
|
|
|
|
namespace = function_data.namespace
|
|
|
|
);
|
2024-04-05 12:59:21 +02:00
|
|
|
|
|
|
|
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
|
2024-04-05 16:16:12 +02:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
2024-04-06 13:26:34 +02:00
|
|
|
.insert(name.to_string(), function_location);
|
2024-04-03 01:27:02 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 16:16:12 +02:00
|
|
|
let locations = self.function_locations.read().unwrap();
|
2024-04-06 14:30:11 +02:00
|
|
|
locations
|
|
|
|
.get(name)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
|
|
|
handler.receive(error.clone());
|
|
|
|
error
|
|
|
|
})
|
|
|
|
.map(String::to_owned)
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
fn transpile_function(
|
|
|
|
&mut self,
|
|
|
|
statements: &[Statement],
|
|
|
|
handler: &impl Handler<TranspileError>,
|
|
|
|
) -> TranspileResult<Vec<Command>> {
|
|
|
|
let mut errors = Vec::new();
|
|
|
|
let commands = statements
|
|
|
|
.iter()
|
|
|
|
.filter_map(|statement| {
|
|
|
|
self.transpile_statement(statement, handler)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
errors.push(err);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors.remove(0));
|
2024-04-05 16:16:12 +02:00
|
|
|
}
|
2024-04-06 14:30:11 +02:00
|
|
|
|
|
|
|
Ok(commands)
|
2024-03-29 18:26:43 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
fn transpile_statement(
|
|
|
|
&mut self,
|
|
|
|
statement: &Statement,
|
|
|
|
handler: &impl Handler<TranspileError>,
|
|
|
|
) -> TranspileResult<Option<Command>> {
|
2024-04-05 16:16:12 +02:00
|
|
|
match statement {
|
|
|
|
Statement::LiteralCommand(literal_command) => {
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(Some(literal_command.clean_command().into()))
|
2024-04-05 16:16:12 +02:00
|
|
|
}
|
2024-04-06 15:12:20 +02:00
|
|
|
Statement::Run(run) => match run.expression() {
|
|
|
|
Expression::Primary(Primary::FunctionCall(func)) => {
|
|
|
|
self.transpile_function_call(func, handler).map(Some)
|
|
|
|
}
|
|
|
|
Expression::Primary(Primary::StringLiteral(string)) => {
|
|
|
|
Ok(Some(Command::Raw(string.str_content().to_string())))
|
|
|
|
}
|
2024-04-06 17:23:20 +02:00
|
|
|
Expression::Primary(Primary::Lua(code)) => {
|
|
|
|
Ok(Some(Command::Raw(code.eval_string(handler)?)))
|
|
|
|
}
|
2024-04-06 15:12:20 +02:00
|
|
|
},
|
2024-04-05 16:16:12 +02:00
|
|
|
Statement::Block(_) => {
|
|
|
|
unreachable!("Only literal commands are allowed in functions at this time.")
|
|
|
|
}
|
2024-04-06 14:30:11 +02:00
|
|
|
Statement::Conditional(cond) => self.transpile_conditional(cond, handler),
|
2024-04-05 16:16:12 +02:00
|
|
|
Statement::DocComment(doccomment) => {
|
|
|
|
let content = doccomment.content();
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(Some(Command::Comment(content.to_string())))
|
2024-04-05 16:16:12 +02:00
|
|
|
}
|
|
|
|
Statement::Grouping(group) => {
|
|
|
|
let statements = group.block().statements();
|
2024-04-06 14:30:11 +02:00
|
|
|
let mut errors = Vec::new();
|
2024-04-05 16:16:12 +02:00
|
|
|
let commands = statements
|
|
|
|
.iter()
|
2024-04-06 14:30:11 +02:00
|
|
|
.filter_map(|statement| {
|
|
|
|
self.transpile_statement(statement, handler)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
errors.push(err);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
2024-04-05 16:16:12 +02:00
|
|
|
.collect::<Vec<_>>();
|
2024-04-06 14:30:11 +02:00
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors.remove(0));
|
|
|
|
}
|
2024-04-05 16:16:12 +02:00
|
|
|
if commands.is_empty() {
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(None)
|
2024-03-29 18:26:43 +01:00
|
|
|
} else {
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(Some(Command::Group(commands)))
|
2024-03-29 18:26:43 +01:00
|
|
|
}
|
2024-04-05 16:16:12 +02:00
|
|
|
}
|
2024-04-06 15:12:20 +02:00
|
|
|
#[allow(clippy::match_wildcard_for_single_variants)]
|
2024-04-05 16:16:12 +02:00
|
|
|
Statement::Semicolon(semi) => match semi.expression() {
|
2024-04-06 15:12:20 +02:00
|
|
|
Expression::Primary(Primary::FunctionCall(func)) => {
|
|
|
|
self.transpile_function_call(func, handler).map(Some)
|
|
|
|
}
|
|
|
|
unexpected => {
|
|
|
|
let error = TranspileError::UnexpectedExpression(unexpected.clone());
|
|
|
|
handler.receive(error.clone());
|
|
|
|
Err(error)
|
|
|
|
}
|
2024-04-05 16:16:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 15:12:20 +02:00
|
|
|
fn transpile_function_call(
|
|
|
|
&mut self,
|
|
|
|
func: &FunctionCall,
|
|
|
|
handler: &impl Handler<TranspileError>,
|
|
|
|
) -> TranspileResult<Command> {
|
|
|
|
let identifier = func.identifier().span();
|
|
|
|
let identifier_name = identifier.str();
|
|
|
|
let location = self.get_or_transpile_function(identifier_name, handler)?;
|
|
|
|
Ok(Command::Raw(format!("function {location}")))
|
|
|
|
}
|
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
fn transpile_conditional(
|
|
|
|
&mut self,
|
|
|
|
cond: &Conditional,
|
|
|
|
handler: &impl Handler<TranspileError>,
|
|
|
|
) -> TranspileResult<Option<Command>> {
|
2024-04-05 16:16:12 +02:00
|
|
|
let (_, cond, block, el) = cond.clone().dissolve();
|
|
|
|
let (_, cond, _) = cond.dissolve();
|
|
|
|
let statements = block.statements();
|
2024-04-06 14:30:11 +02:00
|
|
|
let mut errors = Vec::new();
|
2024-04-05 16:16:12 +02:00
|
|
|
|
|
|
|
let el = el
|
|
|
|
.and_then(|el| {
|
|
|
|
let (_, block) = el.dissolve();
|
|
|
|
let statements = block.statements();
|
|
|
|
if statements.is_empty() {
|
|
|
|
None
|
|
|
|
} else if statements.len() == 1 {
|
2024-04-06 14:30:11 +02:00
|
|
|
self.transpile_statement(&statements[0], handler)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
errors.push(err);
|
|
|
|
None
|
|
|
|
})
|
2024-04-05 16:16:12 +02:00
|
|
|
.map(|cmd| Execute::Run(Box::new(cmd)))
|
2024-03-29 18:26:43 +01:00
|
|
|
} else {
|
2024-04-05 16:16:12 +02:00
|
|
|
let commands = statements
|
|
|
|
.iter()
|
2024-04-06 14:30:11 +02:00
|
|
|
.filter_map(|statement| {
|
|
|
|
self.transpile_statement(statement, handler)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
errors.push(err);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
2024-04-05 16:16:12 +02:00
|
|
|
.collect();
|
|
|
|
Some(Execute::Runs(commands))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(Box::new);
|
2024-03-29 18:26:43 +01:00
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors.remove(0));
|
|
|
|
}
|
|
|
|
|
2024-04-05 16:16:12 +02:00
|
|
|
if statements.is_empty() {
|
|
|
|
if el.is_none() {
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(None)
|
2024-04-05 16:16:12 +02:00
|
|
|
} else {
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(Some(Command::Execute(Execute::If(
|
2024-04-03 13:46:57 +02:00
|
|
|
datapack::Condition::from(cond),
|
2024-04-05 16:16:12 +02:00
|
|
|
Box::new(Execute::Runs(Vec::new())),
|
2024-03-29 18:26:43 +01:00
|
|
|
el,
|
2024-04-06 14:30:11 +02:00
|
|
|
))))
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|
2024-04-05 16:16:12 +02:00
|
|
|
} else {
|
|
|
|
let run = if statements.len() > 1 {
|
|
|
|
let commands = statements
|
|
|
|
.iter()
|
2024-04-06 14:30:11 +02:00
|
|
|
.filter_map(|statement| {
|
|
|
|
self.transpile_statement(statement, handler)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
errors.push(err);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
2024-04-05 16:16:12 +02:00
|
|
|
.collect();
|
2024-04-06 14:30:11 +02:00
|
|
|
Some(Execute::Runs(commands))
|
2024-04-03 01:09:13 +02:00
|
|
|
} else {
|
2024-04-06 14:30:11 +02:00
|
|
|
self.transpile_statement(&statements[0], handler)?
|
|
|
|
.map(|cmd| Execute::Run(Box::new(cmd)))
|
2024-04-05 16:16:12 +02:00
|
|
|
};
|
|
|
|
|
2024-04-06 14:30:11 +02:00
|
|
|
Ok(run.map(|run| {
|
|
|
|
Command::Execute(Execute::If(
|
|
|
|
datapack::Condition::from(cond),
|
|
|
|
Box::new(run),
|
|
|
|
el,
|
|
|
|
))
|
|
|
|
}))
|
2024-04-05 16:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 01:17:17 +01:00
|
|
|
}
|