Update TranspileError enum and add TranspileResult type
This commit is contained in:
parent
c4023e0b64
commit
34076f9842
|
@ -2,8 +2,11 @@
|
||||||
|
|
||||||
/// Errors that can occur during transpilation.
|
/// Errors that can occur during transpilation.
|
||||||
#[allow(clippy::module_name_repetitions, missing_docs)]
|
#[allow(clippy::module_name_repetitions, missing_docs)]
|
||||||
#[derive(Debug, thiserror::Error, Clone, Copy)]
|
#[derive(Debug, thiserror::Error, Clone)]
|
||||||
pub enum TranspileError {
|
pub enum TranspileError {
|
||||||
#[error("No main function was found in the source code.")]
|
#[error("Function {} was called but never declared.", .0)]
|
||||||
MissingMainFunction,
|
MissingFunctionDeclaration(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result of a transpilation operation.
|
||||||
|
pub type TranspileResult<T> = Result<T, TranspileError>;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//! Compiler for `ShulkerScript`
|
//! Transpiler for `ShulkerScript`
|
||||||
|
|
||||||
use chksum_md5 as md5;
|
use chksum_md5 as md5;
|
||||||
use std::{collections::HashMap, sync::RwLock};
|
use std::{collections::HashMap, sync::RwLock};
|
||||||
|
@ -15,7 +15,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::error::TranspileError;
|
use super::error::{TranspileError, TranspileResult};
|
||||||
|
|
||||||
/// A transpiler for `ShulkerScript`.
|
/// A transpiler for `ShulkerScript`.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -56,10 +56,10 @@ impl Transpiler {
|
||||||
pub fn transpile(
|
pub fn transpile(
|
||||||
&mut self,
|
&mut self,
|
||||||
program: &Program,
|
program: &Program,
|
||||||
_handler: &impl Handler<TranspileError>,
|
handler: &impl Handler<TranspileError>,
|
||||||
) -> Result<(), TranspileError> {
|
) -> Result<(), TranspileError> {
|
||||||
for declaration in program.declarations() {
|
for declaration in program.declarations() {
|
||||||
self.transpile_declaration(declaration);
|
self.transpile_declaration(declaration, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut always_transpile_functions = Vec::new();
|
let mut always_transpile_functions = Vec::new();
|
||||||
|
@ -78,14 +78,18 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
for name in always_transpile_functions {
|
for name in always_transpile_functions {
|
||||||
self.get_or_transpile_function(&name);
|
self.get_or_transpile_function(&name, handler)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transpiles the given declaration.
|
/// Transpiles the given declaration.
|
||||||
fn transpile_declaration(&mut self, declaration: &Declaration) {
|
fn transpile_declaration(
|
||||||
|
&mut self,
|
||||||
|
declaration: &Declaration,
|
||||||
|
_handler: &impl Handler<TranspileError>,
|
||||||
|
) {
|
||||||
match declaration {
|
match declaration {
|
||||||
Declaration::Function(function) => {
|
Declaration::Function(function) => {
|
||||||
let name = function.identifier().span().str().to_string();
|
let name = function.identifier().span().str().to_string();
|
||||||
|
@ -117,7 +121,11 @@ impl Transpiler {
|
||||||
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
||||||
/// Returns the location of the function or None if the function does not exist.
|
/// Returns the location of the function or None if the function does not exist.
|
||||||
#[allow(clippy::significant_drop_tightening)]
|
#[allow(clippy::significant_drop_tightening)]
|
||||||
fn get_or_transpile_function(&mut self, name: &str) -> Option<String> {
|
fn get_or_transpile_function(
|
||||||
|
&mut self,
|
||||||
|
name: &str,
|
||||||
|
handler: &impl Handler<TranspileError>,
|
||||||
|
) -> TranspileResult<String> {
|
||||||
let already_transpiled = {
|
let already_transpiled = {
|
||||||
let locations = self.function_locations.read().unwrap();
|
let locations = self.function_locations.read().unwrap();
|
||||||
locations.get(name).is_some()
|
locations.get(name).is_some()
|
||||||
|
@ -125,13 +133,21 @@ impl Transpiler {
|
||||||
if !already_transpiled {
|
if !already_transpiled {
|
||||||
let statements = {
|
let statements = {
|
||||||
let functions = self.functions.read().unwrap();
|
let functions = self.functions.read().unwrap();
|
||||||
let function_data = functions.get(name)?;
|
let function_data = functions.get(name).ok_or_else(|| {
|
||||||
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
||||||
|
handler.receive(error.clone());
|
||||||
|
error
|
||||||
|
})?;
|
||||||
function_data.statements.clone()
|
function_data.statements.clone()
|
||||||
};
|
};
|
||||||
let commands = self.transpile_function(&statements);
|
let commands = self.transpile_function(&statements, handler)?;
|
||||||
|
|
||||||
let functions = self.functions.read().unwrap();
|
let functions = self.functions.read().unwrap();
|
||||||
let function_data = functions.get(name)?;
|
let function_data = functions.get(name).ok_or_else(|| {
|
||||||
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
||||||
|
handler.receive(error.clone());
|
||||||
|
error
|
||||||
|
})?;
|
||||||
|
|
||||||
let modified_name = function_data
|
let modified_name = function_data
|
||||||
.annotations
|
.annotations
|
||||||
|
@ -167,52 +183,96 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
let locations = self.function_locations.read().unwrap();
|
let locations = self.function_locations.read().unwrap();
|
||||||
locations.get(name).map(String::to_owned)
|
locations
|
||||||
|
.get(name)
|
||||||
|
.ok_or_else(|| {
|
||||||
|
let error = TranspileError::MissingFunctionDeclaration(name.to_string());
|
||||||
|
handler.receive(error.clone());
|
||||||
|
error
|
||||||
|
})
|
||||||
|
.map(String::to_owned)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_function(&mut self, statements: &[Statement]) -> Vec<Command> {
|
fn transpile_function(
|
||||||
let mut commands = Vec::new();
|
&mut self,
|
||||||
for statement in statements {
|
statements: &[Statement],
|
||||||
commands.extend(self.transpile_statement(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));
|
||||||
}
|
}
|
||||||
commands
|
|
||||||
|
Ok(commands)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_statement(&mut self, statement: &Statement) -> Option<Command> {
|
fn transpile_statement(
|
||||||
|
&mut self,
|
||||||
|
statement: &Statement,
|
||||||
|
handler: &impl Handler<TranspileError>,
|
||||||
|
) -> TranspileResult<Option<Command>> {
|
||||||
match statement {
|
match statement {
|
||||||
Statement::LiteralCommand(literal_command) => {
|
Statement::LiteralCommand(literal_command) => {
|
||||||
Some(literal_command.clean_command().into())
|
Ok(Some(literal_command.clean_command().into()))
|
||||||
}
|
}
|
||||||
Statement::Block(_) => {
|
Statement::Block(_) => {
|
||||||
unreachable!("Only literal commands are allowed in functions at this time.")
|
unreachable!("Only literal commands are allowed in functions at this time.")
|
||||||
}
|
}
|
||||||
Statement::Conditional(cond) => self.transpile_conditional(cond),
|
Statement::Conditional(cond) => self.transpile_conditional(cond, handler),
|
||||||
Statement::DocComment(doccomment) => {
|
Statement::DocComment(doccomment) => {
|
||||||
let content = doccomment.content();
|
let content = doccomment.content();
|
||||||
Some(Command::Comment(content.to_string()))
|
Ok(Some(Command::Comment(content.to_string())))
|
||||||
}
|
}
|
||||||
Statement::Grouping(group) => {
|
Statement::Grouping(group) => {
|
||||||
let statements = group.block().statements();
|
let statements = group.block().statements();
|
||||||
|
let mut errors = Vec::new();
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| self.transpile_statement(statement))
|
.filter_map(|statement| {
|
||||||
|
self.transpile_statement(statement, handler)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
errors.push(err);
|
||||||
|
None
|
||||||
|
})
|
||||||
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
if !errors.is_empty() {
|
||||||
|
return Err(errors.remove(0));
|
||||||
|
}
|
||||||
if commands.is_empty() {
|
if commands.is_empty() {
|
||||||
None
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Some(Command::Group(commands))
|
Ok(Some(Command::Group(commands)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Statement::Semicolon(semi) => match semi.expression() {
|
Statement::Semicolon(semi) => match semi.expression() {
|
||||||
Expression::Primary(primary) => self.transpile_primary_expression(primary),
|
Expression::Primary(primary) => self
|
||||||
|
.transpile_primary_expression(primary, handler)
|
||||||
|
.map(Some),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_conditional(&mut self, cond: &Conditional) -> Option<Command> {
|
fn transpile_conditional(
|
||||||
|
&mut self,
|
||||||
|
cond: &Conditional,
|
||||||
|
handler: &impl Handler<TranspileError>,
|
||||||
|
) -> TranspileResult<Option<Command>> {
|
||||||
let (_, cond, block, el) = cond.clone().dissolve();
|
let (_, cond, block, el) = cond.clone().dissolve();
|
||||||
let (_, cond, _) = cond.dissolve();
|
let (_, cond, _) = cond.dissolve();
|
||||||
let statements = block.statements();
|
let statements = block.statements();
|
||||||
|
let mut errors = Vec::new();
|
||||||
|
|
||||||
let el = el
|
let el = el
|
||||||
.and_then(|el| {
|
.and_then(|el| {
|
||||||
|
@ -221,54 +281,81 @@ impl Transpiler {
|
||||||
if statements.is_empty() {
|
if statements.is_empty() {
|
||||||
None
|
None
|
||||||
} else if statements.len() == 1 {
|
} else if statements.len() == 1 {
|
||||||
self.transpile_statement(&statements[0])
|
self.transpile_statement(&statements[0], handler)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
errors.push(err);
|
||||||
|
None
|
||||||
|
})
|
||||||
.map(|cmd| Execute::Run(Box::new(cmd)))
|
.map(|cmd| Execute::Run(Box::new(cmd)))
|
||||||
} else {
|
} else {
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| self.transpile_statement(statement))
|
.filter_map(|statement| {
|
||||||
|
self.transpile_statement(statement, handler)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
errors.push(err);
|
||||||
|
None
|
||||||
|
})
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Some(Execute::Runs(commands))
|
Some(Execute::Runs(commands))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(Box::new);
|
.map(Box::new);
|
||||||
|
|
||||||
|
if !errors.is_empty() {
|
||||||
|
return Err(errors.remove(0));
|
||||||
|
}
|
||||||
|
|
||||||
if statements.is_empty() {
|
if statements.is_empty() {
|
||||||
if el.is_none() {
|
if el.is_none() {
|
||||||
None
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Some(Command::Execute(Execute::If(
|
Ok(Some(Command::Execute(Execute::If(
|
||||||
datapack::Condition::from(cond),
|
datapack::Condition::from(cond),
|
||||||
Box::new(Execute::Runs(Vec::new())),
|
Box::new(Execute::Runs(Vec::new())),
|
||||||
el,
|
el,
|
||||||
)))
|
))))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let run = if statements.len() > 1 {
|
let run = if statements.len() > 1 {
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| self.transpile_statement(statement))
|
.filter_map(|statement| {
|
||||||
|
self.transpile_statement(statement, handler)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
errors.push(err);
|
||||||
|
None
|
||||||
|
})
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Execute::Runs(commands)
|
Some(Execute::Runs(commands))
|
||||||
} else {
|
} else {
|
||||||
Execute::Run(Box::new(self.transpile_statement(&statements[0])?))
|
self.transpile_statement(&statements[0], handler)?
|
||||||
|
.map(|cmd| Execute::Run(Box::new(cmd)))
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(Command::Execute(Execute::If(
|
Ok(run.map(|run| {
|
||||||
datapack::Condition::from(cond),
|
Command::Execute(Execute::If(
|
||||||
Box::new(run),
|
datapack::Condition::from(cond),
|
||||||
el,
|
Box::new(run),
|
||||||
)))
|
el,
|
||||||
|
))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_primary_expression(&mut self, primary: &Primary) -> Option<Command> {
|
fn transpile_primary_expression(
|
||||||
|
&mut self,
|
||||||
|
primary: &Primary,
|
||||||
|
handler: &impl Handler<TranspileError>,
|
||||||
|
) -> TranspileResult<Command> {
|
||||||
match primary {
|
match primary {
|
||||||
Primary::FunctionCall(func) => {
|
Primary::FunctionCall(func) => {
|
||||||
let identifier = func.identifier().span();
|
let identifier = func.identifier().span();
|
||||||
let identifier = identifier.str();
|
let identifier_name = identifier.str();
|
||||||
let location = self.get_or_transpile_function(identifier)?;
|
let location = self.get_or_transpile_function(identifier_name, handler)?;
|
||||||
Some(Command::Raw(format!("function {location}")))
|
Ok(Command::Raw(format!("function {location}")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue