Add ShulkerScript basic compiler for only main function and literal commands

This commit is contained in:
Moritz Hölting 2024-03-28 01:17:17 +01:00
parent 9d24571b40
commit 0d93faf87f
6 changed files with 102 additions and 4 deletions

View File

@ -10,6 +10,7 @@ colored = "2.1.0"
derive_more = { version = "0.99.17", default-features = false, features = ["deref", "from", "deref_mut"] } derive_more = { version = "0.99.17", default-features = false, features = ["deref", "from", "deref_mut"] }
enum-as-inner = "0.6.0" enum-as-inner = "0.6.0"
getset = "0.1.2" getset = "0.1.2"
shulkerbox = { path = "../shulkerbox" }
strum = { version = "0.26.2", features = ["derive"] } strum = { version = "0.26.2", features = ["derive"] }
strum_macros = "0.26.2" strum_macros = "0.26.2"
thiserror = "1.0.58" thiserror = "1.0.58"

View File

@ -10,6 +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.")]
CompileError(#[from] crate::compile::error::CompileError),
#[error("An error occurred")] #[error("An error occurred")]
Other(&'static str), Other(&'static str),
} }

76
src/compile/compiler.rs Normal file
View File

@ -0,0 +1,76 @@
//! Compiler for `ShulkerScript`
use std::collections::HashMap;
use shulkerbox::datapack::{Command, Datapack};
use crate::{
base::{source_file::SourceElement, Handler},
syntax::syntax_tree::{declaration::Declaration, program::Program, statement::Statement},
};
use super::error::{self, CompileError};
/// A compiler for `ShulkerScript`.
#[derive(Debug, Clone, Default)]
pub struct Compiler {
functions: HashMap<String, Vec<Statement>>,
}
impl Compiler {
/// Creates a new compiler.
#[must_use]
pub fn new() -> Self {
Self {
functions: HashMap::new(),
}
}
/// Compiles the given program.
///
/// # Errors
/// - [`CompileError::MissingMainFunction`] If the main function is missing.
pub fn compile(
&mut self,
program: &Program,
handler: &impl Handler<error::CompileError>,
) -> Result<Datapack, CompileError> {
for declaration in program.declarations() {
match declaration {
Declaration::Function(function) => self.functions.insert(
function.identifier().span().str().to_string(),
function.block().statements().clone(),
),
};
}
let Some(main_function) = self.functions.get("main") else {
handler.receive(CompileError::MissingMainFunction);
return Err(CompileError::MissingMainFunction);
};
let main_commands = compile_function(main_function);
// TODO: change this
let mut datapack = shulkerbox::datapack::Datapack::new("shulkerscript-pack", 27);
let namespace = datapack.namespace_mut("shulkerscript");
let main_function = namespace.function_mut("main");
main_function.get_commands_mut().extend(main_commands);
Ok(datapack)
}
}
fn compile_function(statements: &[Statement]) -> Vec<Command> {
let mut commands = Vec::new();
for statement in statements {
match statement {
Statement::LiteralCommand(literal_command) => {
commands.push(literal_command.clean_command().into());
}
Statement::Block(_) => {
unreachable!("Only literal commands are allowed in functions at this time.")
}
}
}
commands
}

9
src/compile/error.rs Normal file
View File

@ -0,0 +1,9 @@
//! Errors that can occur during compilation.
/// Errors that can occur during compilation.
#[allow(clippy::module_name_repetitions, missing_docs)]
#[derive(Debug, thiserror::Error, Clone, Copy)]
pub enum CompileError {
#[error("No main function was found in the source code.")]
MissingMainFunction,
}

4
src/compile/mod.rs Normal file
View File

@ -0,0 +1,4 @@
//! The compile module is responsible for compiling the abstract syntax tree into a data pack.
pub mod compiler;
pub mod error;

View File

@ -13,12 +13,15 @@
#![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)] #![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)]
pub mod base; pub mod base;
pub mod compile;
pub mod lexical; pub mod lexical;
pub mod syntax; pub mod syntax;
use std::{cell::Cell, fmt::Display, path::PathBuf}; use std::{cell::Cell, fmt::Display, path::PathBuf};
use base::{source_file::SourceFile, Handler, Result}; use base::{source_file::SourceFile, Handler, Result};
use compile::compiler::Compiler;
use shulkerbox::{util::compile::CompileOptions, virtual_fs::VFolder};
use crate::{base::Error, lexical::token_stream::TokenStream, syntax::parser::Parser}; use crate::{base::Error, lexical::token_stream::TokenStream, syntax::parser::Parser};
@ -26,7 +29,7 @@ use crate::{base::Error, lexical::token_stream::TokenStream, syntax::parser::Par
/// ///
/// # Errors /// # Errors
/// - If an error occurs while reading the file. /// - If an error occurs while reading the file.
pub fn compile(path: PathBuf) -> Result<()> { pub fn compile(path: PathBuf) -> Result<VFolder> {
let source_file = SourceFile::load(path)?; let source_file = SourceFile::load(path)?;
let printer = Printer::new(); let printer = Printer::new();
@ -40,11 +43,14 @@ pub fn compile(path: PathBuf) -> Result<()> {
} }
let mut parser = Parser::new(&tokens); let mut parser = Parser::new(&tokens);
let result = parser.parse_program(&printer).ok_or(Error::Other( let program = parser.parse_program(&printer).ok_or(Error::Other(
"An error occured while parsing the source code.", "An error occured while parsing the source code.",
))?; ))?;
println!("result: {result:#?}"); // println!("result: {result:#?}");
let mut compiler = Compiler::new();
let datapack = compiler.compile(&program, &printer)?;
if printer.has_printed() { if printer.has_printed() {
return Err(Error::Other( return Err(Error::Other(
@ -52,7 +58,7 @@ pub fn compile(path: PathBuf) -> Result<()> {
)); ));
} }
Ok(()) Ok(datapack.compile(&CompileOptions::default()))
} }
struct Printer { struct Printer {