Refactor transpile module and error handling
This commit is contained in:
parent
4b52985992
commit
2ed6e56ef1
|
@ -10,8 +10,8 @@ pub enum Error {
|
||||||
TokenizeError(#[from] crate::lexical::token::TokenizeError),
|
TokenizeError(#[from] crate::lexical::token::TokenizeError),
|
||||||
#[error("An error occurred while parsing the source code.")]
|
#[error("An error occurred while parsing the source code.")]
|
||||||
ParseError(#[from] crate::syntax::error::Error),
|
ParseError(#[from] crate::syntax::error::Error),
|
||||||
#[error("An error occurred while compiling the source code.")]
|
#[error("An error occurred while transpiling the source code.")]
|
||||||
CompileError(#[from] crate::transpile::error::TranspileError),
|
TranspileError(#[from] crate::transpile::error::TranspileError),
|
||||||
#[error("An error occurred")]
|
#[error("An error occurred")]
|
||||||
Other(&'static str),
|
Other(&'static str),
|
||||||
}
|
}
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -21,6 +21,8 @@ use std::{cell::Cell, fmt::Display, path::Path};
|
||||||
|
|
||||||
use base::{source_file::SourceFile, Handler, Result};
|
use base::{source_file::SourceFile, Handler, Result};
|
||||||
use syntax::syntax_tree::program::Program;
|
use syntax::syntax_tree::program::Program;
|
||||||
|
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
use transpile::transpiler::Transpiler;
|
use transpile::transpiler::Transpiler;
|
||||||
|
|
||||||
#[cfg(feature = "shulkerbox")]
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
@ -103,8 +105,9 @@ pub fn transpile(path: &Path) -> Result<Datapack> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut transpiler = Transpiler::new();
|
let mut transpiler = Transpiler::new("shulkerscript-pack", 27);
|
||||||
let datapack = transpiler.transpile(&program, &printer)?;
|
transpiler.transpile(&program, &printer)?;
|
||||||
|
let datapack = transpiler.into_datapack();
|
||||||
|
|
||||||
Ok(datapack)
|
Ok(datapack)
|
||||||
}
|
}
|
||||||
|
@ -144,8 +147,9 @@ pub fn compile(path: &Path) -> Result<VFolder> {
|
||||||
|
|
||||||
// println!("program: {program:#?}");
|
// println!("program: {program:#?}");
|
||||||
|
|
||||||
let mut transpiler = Transpiler::new();
|
let mut transpiler = Transpiler::new("shulkerscript-pack", 27);
|
||||||
let datapack = transpiler.transpile(&program, &printer)?;
|
transpiler.transpile(&program, &printer)?;
|
||||||
|
let datapack = transpiler.into_datapack();
|
||||||
|
|
||||||
// println!("datapack: {datapack:#?}");
|
// println!("datapack: {datapack:#?}");
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Errors that can occur during compilation.
|
//! Errors that can occur during transpilation.
|
||||||
|
|
||||||
/// Errors that can occur during compilation.
|
/// 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, Copy)]
|
||||||
pub enum TranspileError {
|
pub enum TranspileError {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
//! The transpile module is responsible for transpiling the abstract syntax tree into a data pack.
|
//! The transpile module is responsible for transpiling the abstract syntax tree into a data pack.
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
pub mod conversions;
|
pub mod conversions;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
pub mod transpiler;
|
pub mod transpiler;
|
||||||
|
|
|
@ -12,21 +12,37 @@ use crate::{
|
||||||
use super::error::{self, TranspileError};
|
use super::error::{self, TranspileError};
|
||||||
|
|
||||||
/// A transpiler for `ShulkerScript`.
|
/// A transpiler for `ShulkerScript`.
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Transpiler {
|
pub struct Transpiler {
|
||||||
functions: HashMap<String, (Vec<Statement>, AnnotationMap)>,
|
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>>,
|
||||||
}
|
}
|
||||||
type AnnotationMap = HashMap<String, Option<String>>;
|
|
||||||
|
|
||||||
impl Transpiler {
|
impl Transpiler {
|
||||||
/// Creates a new transpiler.
|
/// Creates a new transpiler.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new(pack_name: &str, pack_format: u8) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
datapack: shulkerbox::datapack::Datapack::new(pack_name, pack_format),
|
||||||
functions: HashMap::new(),
|
functions: HashMap::new(),
|
||||||
|
function_locations: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Consumes the transpiler and returns the resulting datapack.
|
||||||
|
#[must_use]
|
||||||
|
pub fn into_datapack(self) -> Datapack {
|
||||||
|
self.datapack
|
||||||
|
}
|
||||||
|
|
||||||
/// Transpiles the given program.
|
/// Transpiles the given program.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
|
@ -35,49 +51,77 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
program: &Program,
|
program: &Program,
|
||||||
handler: &impl Handler<error::TranspileError>,
|
handler: &impl Handler<error::TranspileError>,
|
||||||
) -> Result<Datapack, TranspileError> {
|
) -> Result<(), TranspileError> {
|
||||||
for declaration in program.declarations() {
|
for declaration in program.declarations() {
|
||||||
match declaration {
|
self.transpile_declaration(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, (statements, annotations))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some((main_function, main_annotations)) = self.functions.get("main") else {
|
self.get_or_transpile_function("main").ok_or_else(|| {
|
||||||
handler.receive(TranspileError::MissingMainFunction);
|
handler.receive(TranspileError::MissingMainFunction);
|
||||||
return Err(TranspileError::MissingMainFunction);
|
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,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
let main_commands = compile_function(main_function);
|
/// Gets the function at the given path, or transpiles it if it hasn't been transpiled yet.
|
||||||
// TODO: change this
|
fn get_or_transpile_function(&mut self, path: &str) -> Option<&str> {
|
||||||
let mut datapack = shulkerbox::datapack::Datapack::new("shulkerscript-pack", 27);
|
let already_transpiled = self.function_locations.get(path);
|
||||||
let namespace = datapack.namespace_mut("shulkerscript");
|
if already_transpiled.is_none() {
|
||||||
let main_function = namespace.function_mut("main");
|
let function_data = self.functions.get(path)?;
|
||||||
main_function.get_commands_mut().extend(main_commands);
|
let commands = compile_function(&function_data.statements);
|
||||||
|
|
||||||
if main_annotations.contains_key("tick") {
|
let function = self
|
||||||
datapack.add_tick("shulkerscript:main");
|
.datapack
|
||||||
}
|
.namespace_mut(&function_data.namespace)
|
||||||
if main_annotations.contains_key("load") {
|
.function_mut(path);
|
||||||
datapack.add_load("shulkerscript:main");
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(datapack)
|
self.function_locations.get(path).map(String::as_str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue