Compare commits
4 Commits
58998b4246
...
9c54dee454
Author | SHA1 | Date |
---|---|---|
|
9c54dee454 | |
|
09dd19508d | |
|
79a6455d8f | |
|
2a41796405 |
|
@ -176,9 +176,8 @@ where
|
|||
|
||||
tracing::info!("Transpiling the source code.");
|
||||
|
||||
let mut transpiler = Transpiler::new(main_namespace_name, pack_format);
|
||||
transpiler.transpile(&programs, handler)?;
|
||||
let datapack = transpiler.into_datapack();
|
||||
let datapack =
|
||||
Transpiler::new(main_namespace_name, pack_format).transpile(&programs, handler)?;
|
||||
|
||||
if handler.has_received() {
|
||||
return Err(Error::other(
|
||||
|
|
|
@ -13,11 +13,8 @@ use crate::{
|
|||
base::{self, source_file::SourceElement as _, Handler},
|
||||
lexical::token::{MacroStringLiteral, MacroStringLiteralPart},
|
||||
syntax::syntax_tree::{
|
||||
condition::{
|
||||
BinaryCondition, Condition, ParenthesizedCondition, PrimaryCondition, UnaryCondition,
|
||||
},
|
||||
declaration::{Declaration, Function, ImportItems},
|
||||
expression::{Expression, FunctionCall, Primary},
|
||||
expression::{Expression, FunctionCall, Parenthesized, Primary},
|
||||
program::{Namespace, ProgramFile},
|
||||
statement::{
|
||||
execute_block::{
|
||||
|
@ -360,7 +357,7 @@ impl Conditional {
|
|||
}
|
||||
}
|
||||
|
||||
impl ParenthesizedCondition {
|
||||
impl Parenthesized {
|
||||
/// Analyzes the semantics of the parenthesized condition.
|
||||
pub fn analyze_semantics(
|
||||
&self,
|
||||
|
@ -368,26 +365,11 @@ impl ParenthesizedCondition {
|
|||
macro_names: &HashSet<String>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), error::Error> {
|
||||
self.condition
|
||||
self.expression()
|
||||
.analyze_semantics(function_names, macro_names, handler)
|
||||
}
|
||||
}
|
||||
|
||||
impl Condition {
|
||||
/// Analyzes the semantics of the condition.
|
||||
pub fn analyze_semantics(
|
||||
&self,
|
||||
function_names: &HashSet<String>,
|
||||
macro_names: &HashSet<String>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), error::Error> {
|
||||
match self {
|
||||
Self::Primary(prim) => prim.analyze_semantics(function_names, macro_names, handler),
|
||||
Self::Binary(bin) => bin.analyze_semantics(function_names, macro_names, handler),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Else {
|
||||
/// Analyzes the semantics of the else block.
|
||||
pub fn analyze_semantics(
|
||||
|
@ -618,62 +600,3 @@ impl VariableDeclaration {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimaryCondition {
|
||||
/// Analyzes the semantics of a primary condition.
|
||||
pub fn analyze_semantics(
|
||||
&self,
|
||||
function_names: &HashSet<String>,
|
||||
macro_names: &HashSet<String>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), error::Error> {
|
||||
match self {
|
||||
Self::Parenthesized(paren) => {
|
||||
paren.analyze_semantics(function_names, macro_names, handler)
|
||||
}
|
||||
Self::StringLiteral(_) => Ok(()),
|
||||
Self::Unary(unary) => unary.analyze_semantics(function_names, macro_names, handler),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UnaryCondition {
|
||||
/// Analyzes the semantics of an unary condition.
|
||||
pub fn analyze_semantics(
|
||||
&self,
|
||||
function_names: &HashSet<String>,
|
||||
macro_names: &HashSet<String>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), error::Error> {
|
||||
self.operand()
|
||||
.analyze_semantics(function_names, macro_names, handler)
|
||||
}
|
||||
}
|
||||
|
||||
impl BinaryCondition {
|
||||
/// Analyzes the semantics of a binary condition.
|
||||
pub fn analyze_semantics(
|
||||
&self,
|
||||
function_names: &HashSet<String>,
|
||||
macro_names: &HashSet<String>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), error::Error> {
|
||||
let a = self
|
||||
.left_operand()
|
||||
.analyze_semantics(function_names, macro_names, handler)
|
||||
.inspect_err(|err| {
|
||||
handler.receive(err.clone());
|
||||
});
|
||||
let b = self
|
||||
.right_operand()
|
||||
.analyze_semantics(function_names, macro_names, handler)
|
||||
.inspect_err(|err| {
|
||||
handler.receive(err.clone());
|
||||
});
|
||||
if a.is_err() {
|
||||
a
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,451 +0,0 @@
|
|||
//! Syntax tree nodes for conditions.
|
||||
|
||||
#![allow(clippy::missing_errors_doc)]
|
||||
|
||||
use std::{cmp::Ordering, collections::VecDeque};
|
||||
|
||||
use enum_as_inner::EnumAsInner;
|
||||
use getset::Getters;
|
||||
|
||||
use crate::{
|
||||
base::{
|
||||
self,
|
||||
source_file::{SourceElement, Span},
|
||||
Handler, VoidHandler,
|
||||
},
|
||||
lexical::{
|
||||
token::{Punctuation, Token},
|
||||
token_stream::Delimiter,
|
||||
},
|
||||
syntax::{
|
||||
error::{Error, ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||
parser::{Parser, Reading},
|
||||
},
|
||||
};
|
||||
|
||||
use super::AnyStringLiteral;
|
||||
|
||||
/// Condition that is viewed as a single entity during precedence parsing.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// PrimaryCondition:
|
||||
/// UnaryCondition
|
||||
/// | ParenthesizedCondition
|
||||
/// | AnyStringLiteral
|
||||
/// ```
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
||||
pub enum PrimaryCondition {
|
||||
Unary(UnaryCondition),
|
||||
Parenthesized(ParenthesizedCondition),
|
||||
StringLiteral(AnyStringLiteral),
|
||||
}
|
||||
|
||||
impl SourceElement for PrimaryCondition {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
Self::Unary(unary) => unary.span(),
|
||||
Self::Parenthesized(parenthesized) => parenthesized.span(),
|
||||
Self::StringLiteral(literal) => literal.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Condition that is composed of two conditions and a binary operator.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// BinaryCondition:
|
||||
/// Condition ConditionalBinaryOperator Condition
|
||||
/// ;
|
||||
/// ```
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||
pub struct BinaryCondition {
|
||||
/// The left operand of the binary condition.
|
||||
#[get = "pub"]
|
||||
left_operand: Box<Condition>,
|
||||
/// The operator of the binary condition.
|
||||
#[get = "pub"]
|
||||
operator: ConditionalBinaryOperator,
|
||||
/// The right operand of the binary condition.
|
||||
#[get = "pub"]
|
||||
right_operand: Box<Condition>,
|
||||
}
|
||||
|
||||
impl SourceElement for BinaryCondition {
|
||||
fn span(&self) -> Span {
|
||||
self.left_operand
|
||||
.span()
|
||||
.join(&self.right_operand.span())
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl BinaryCondition {
|
||||
/// Dissolves the binary condition into its components
|
||||
#[must_use]
|
||||
pub fn dissolve(self) -> (Condition, ConditionalBinaryOperator, Condition) {
|
||||
(*self.left_operand, self.operator, *self.right_operand)
|
||||
}
|
||||
}
|
||||
|
||||
/// Operator that is used to combine two conditions.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// ConditionalBinaryOperator:
|
||||
/// '&&'
|
||||
/// | '||'
|
||||
/// ;
|
||||
/// ```
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
||||
pub enum ConditionalBinaryOperator {
|
||||
LogicalAnd(Punctuation, Punctuation),
|
||||
LogicalOr(Punctuation, Punctuation),
|
||||
}
|
||||
|
||||
impl ConditionalBinaryOperator {
|
||||
/// Gets the precedence of the operator (the higher the number, the first it will be evaluated)
|
||||
///
|
||||
/// The least operator has precedence 1.
|
||||
#[must_use]
|
||||
pub fn get_precedence(&self) -> u8 {
|
||||
match self {
|
||||
Self::LogicalOr(..) => 1,
|
||||
Self::LogicalAnd(..) => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceElement for ConditionalBinaryOperator {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
Self::LogicalAnd(a, b) | Self::LogicalOr(a, b) => a
|
||||
.span
|
||||
.join(&b.span)
|
||||
.expect("Invalid tokens for ConditionalBinaryOperator"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Condition that is enclosed in parentheses.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// ParenthesizedCondition:
|
||||
/// '(' Condition ')';
|
||||
/// ```
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||
pub struct ParenthesizedCondition {
|
||||
/// The opening parenthesis.
|
||||
#[get = "pub"]
|
||||
pub open_paren: Punctuation,
|
||||
/// The condition within the parenthesis.
|
||||
#[get = "pub"]
|
||||
pub condition: Box<Condition>,
|
||||
/// The closing parenthesis.
|
||||
#[get = "pub"]
|
||||
pub close_paren: Punctuation,
|
||||
}
|
||||
|
||||
impl ParenthesizedCondition {
|
||||
/// Dissolves the parenthesized condition into its components
|
||||
#[must_use]
|
||||
pub fn dissolve(self) -> (Punctuation, Condition, Punctuation) {
|
||||
(self.open_paren, *self.condition, self.close_paren)
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceElement for ParenthesizedCondition {
|
||||
fn span(&self) -> Span {
|
||||
self.open_paren
|
||||
.span()
|
||||
.join(&self.close_paren.span())
|
||||
.expect("The span of the parenthesis is invalid.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Operator that is used to prefix a condition.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// ConditionalPrefixOperator: '!';
|
||||
/// ```
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
||||
pub enum ConditionalPrefixOperator {
|
||||
LogicalNot(Punctuation),
|
||||
}
|
||||
|
||||
impl SourceElement for ConditionalPrefixOperator {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
Self::LogicalNot(token) => token.span.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Condition that is prefixed by an operator.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ```ebnf
|
||||
/// UnaryCondition:
|
||||
/// ConditionalPrefixOperator PrimaryCondition
|
||||
/// ;
|
||||
/// ```
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||
pub struct UnaryCondition {
|
||||
/// The operator of the prefix.
|
||||
#[get = "pub"]
|
||||
operator: ConditionalPrefixOperator,
|
||||
/// The operand of the prefix.
|
||||
#[get = "pub"]
|
||||
operand: Box<PrimaryCondition>,
|
||||
}
|
||||
|
||||
impl SourceElement for UnaryCondition {
|
||||
fn span(&self) -> Span {
|
||||
self.operator.span().join(&self.operand.span()).unwrap()
|
||||
}
|
||||
}
|
||||
impl UnaryCondition {
|
||||
/// Dissolves the conditional prefix into its components
|
||||
#[must_use]
|
||||
pub fn dissolve(self) -> (ConditionalPrefixOperator, PrimaryCondition) {
|
||||
(self.operator, *self.operand)
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a condition in the syntax tree.
|
||||
///
|
||||
/// Syntax Synopsis:
|
||||
///
|
||||
/// ``` ebnf
|
||||
/// Condition:
|
||||
/// PrimaryCondition
|
||||
/// | BinaryCondition
|
||||
/// ;
|
||||
/// ```
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
|
||||
pub enum Condition {
|
||||
Primary(PrimaryCondition),
|
||||
Binary(BinaryCondition),
|
||||
}
|
||||
|
||||
impl SourceElement for Condition {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
Self::Primary(primary) => primary.span(),
|
||||
Self::Binary(binary) => binary.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
/// Parses a [`Condition`].
|
||||
///
|
||||
/// # Precedence of the operators
|
||||
/// 1. `!`
|
||||
/// 2. `&&`
|
||||
/// 3. `||`
|
||||
pub fn parse_condition(
|
||||
&mut self,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> ParseResult<Condition> {
|
||||
let mut lhs = Condition::Primary(self.parse_primary_condition(handler)?);
|
||||
let mut expressions = VecDeque::new();
|
||||
|
||||
// Parses a list of binary operators and expressions
|
||||
while let Ok(binary_operator) = self.try_parse_conditional_binary_operator() {
|
||||
expressions.push_back((
|
||||
binary_operator,
|
||||
Some(Condition::Primary(self.parse_primary_condition(handler)?)),
|
||||
));
|
||||
}
|
||||
|
||||
let mut candidate_index = 0;
|
||||
let mut current_precedence;
|
||||
|
||||
while !expressions.is_empty() {
|
||||
// reset precedence
|
||||
current_precedence = 0;
|
||||
|
||||
for (index, (binary_op, _)) in expressions.iter().enumerate() {
|
||||
let new_precedence = binary_op.get_precedence();
|
||||
match new_precedence.cmp(¤t_precedence) {
|
||||
// Clear the candidate indices and set the current precedence to the
|
||||
// precedence of the current binary operator.
|
||||
Ordering::Greater => {
|
||||
current_precedence = new_precedence;
|
||||
candidate_index = index;
|
||||
}
|
||||
|
||||
Ordering::Less | Ordering::Equal => (),
|
||||
}
|
||||
}
|
||||
|
||||
// ASSUMPTION: The assignments have 1 precedence and are right associative.
|
||||
assert!(current_precedence > 0);
|
||||
|
||||
if candidate_index == 0 {
|
||||
let (binary_op, rhs) = expressions.pop_front().expect("No binary operator found");
|
||||
|
||||
// fold the first expression
|
||||
lhs = Condition::Binary(BinaryCondition {
|
||||
left_operand: Box::new(lhs),
|
||||
operator: binary_op,
|
||||
right_operand: Box::new(rhs.unwrap()),
|
||||
});
|
||||
} else {
|
||||
let (binary_op, rhs) = expressions
|
||||
.remove(candidate_index)
|
||||
.expect("No binary operator found");
|
||||
|
||||
// fold the expression at candidate_index
|
||||
expressions[candidate_index - 1].1 = Some(Condition::Binary(BinaryCondition {
|
||||
left_operand: Box::new(expressions[candidate_index - 1].1.take().unwrap()),
|
||||
operator: binary_op,
|
||||
right_operand: Box::new(rhs.unwrap()),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(lhs)
|
||||
}
|
||||
|
||||
/// Parses a [`PrimaryCondition`].
|
||||
pub fn parse_primary_condition(
|
||||
&mut self,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> ParseResult<PrimaryCondition> {
|
||||
match self.stop_at_significant() {
|
||||
// prefixed expression
|
||||
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '!' => {
|
||||
// eat prefix operator
|
||||
self.forward();
|
||||
|
||||
let operator = match punc.punctuation {
|
||||
'!' => ConditionalPrefixOperator::LogicalNot(punc),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let operand = Box::new(self.parse_primary_condition(handler)?);
|
||||
|
||||
Ok(PrimaryCondition::Unary(UnaryCondition {
|
||||
operator,
|
||||
operand,
|
||||
}))
|
||||
}
|
||||
|
||||
// string literal
|
||||
Reading::Atomic(Token::StringLiteral(literal)) => {
|
||||
self.forward();
|
||||
Ok(PrimaryCondition::StringLiteral(literal.into()))
|
||||
}
|
||||
|
||||
// macro string literal
|
||||
Reading::Atomic(Token::MacroStringLiteral(literal)) => {
|
||||
self.forward();
|
||||
Ok(PrimaryCondition::StringLiteral(literal.into()))
|
||||
}
|
||||
|
||||
// parenthesized condition
|
||||
Reading::IntoDelimited(punc) if punc.punctuation == '(' => self
|
||||
.parse_parenthesized_condition(handler)
|
||||
.map(PrimaryCondition::Parenthesized),
|
||||
|
||||
unexpected => {
|
||||
// make progress
|
||||
self.forward();
|
||||
|
||||
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Either(&[
|
||||
SyntaxKind::Punctuation('!'),
|
||||
SyntaxKind::StringLiteral,
|
||||
SyntaxKind::Punctuation('('),
|
||||
]),
|
||||
found: unexpected.into_token(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a [`ParenthesizedCondition`].
|
||||
pub fn parse_parenthesized_condition(
|
||||
&mut self,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> ParseResult<ParenthesizedCondition> {
|
||||
let token_tree = self.step_into(
|
||||
Delimiter::Parenthesis,
|
||||
|parser| {
|
||||
let cond = parser.parse_condition(handler)?;
|
||||
parser.stop_at_significant();
|
||||
Ok(cond)
|
||||
},
|
||||
handler,
|
||||
)?;
|
||||
|
||||
Ok(ParenthesizedCondition {
|
||||
open_paren: token_tree.open,
|
||||
condition: Box::new(token_tree.tree?),
|
||||
close_paren: token_tree.close,
|
||||
})
|
||||
}
|
||||
|
||||
fn try_parse_conditional_binary_operator(&mut self) -> ParseResult<ConditionalBinaryOperator> {
|
||||
self.try_parse(|parser| match parser.next_significant_token() {
|
||||
Reading::Atomic(token) => match token.clone() {
|
||||
Token::Punctuation(punc) => match punc.punctuation {
|
||||
'&' => {
|
||||
let b = parser.parse_punctuation('&', false, &VoidHandler)?;
|
||||
Ok(ConditionalBinaryOperator::LogicalAnd(punc, b))
|
||||
}
|
||||
'|' => {
|
||||
let b = parser.parse_punctuation('|', false, &VoidHandler)?;
|
||||
Ok(ConditionalBinaryOperator::LogicalOr(punc, b))
|
||||
}
|
||||
_ => Err(Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Either(&[
|
||||
SyntaxKind::Punctuation('&'),
|
||||
SyntaxKind::Punctuation('|'),
|
||||
]),
|
||||
found: Some(token),
|
||||
})),
|
||||
},
|
||||
unexpected => Err(Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Either(&[
|
||||
SyntaxKind::Punctuation('&'),
|
||||
SyntaxKind::Punctuation('|'),
|
||||
]),
|
||||
found: Some(unexpected),
|
||||
})),
|
||||
},
|
||||
unexpected => Err(Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||
expected: SyntaxKind::Either(&[
|
||||
SyntaxKind::Punctuation('&'),
|
||||
SyntaxKind::Punctuation('|'),
|
||||
]),
|
||||
found: unexpected.into_token(),
|
||||
})),
|
||||
})
|
||||
}
|
||||
}
|
|
@ -620,7 +620,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_parenthesized(
|
||||
pub(super) fn parse_parenthesized(
|
||||
&mut self,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> ParseResult<Parenthesized> {
|
||||
|
|
|
@ -23,7 +23,6 @@ use super::{
|
|||
parser::Parser,
|
||||
};
|
||||
|
||||
pub mod condition;
|
||||
pub mod declaration;
|
||||
pub mod expression;
|
||||
pub mod program;
|
||||
|
|
|
@ -19,7 +19,7 @@ use crate::{
|
|||
syntax::{
|
||||
error::{Error, ParseResult, SyntaxKind, UnexpectedSyntax},
|
||||
parser::{DelimitedTree, Parser, Reading},
|
||||
syntax_tree::{condition::ParenthesizedCondition, AnyStringLiteral},
|
||||
syntax_tree::{expression::Parenthesized, AnyStringLiteral},
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -148,7 +148,7 @@ impl SourceElement for ExecuteBlockTail {
|
|||
///
|
||||
/// ``` ebnf
|
||||
/// Conditional:
|
||||
/// 'if' ParenthizedCondition
|
||||
/// 'if' Parenthized
|
||||
/// ;
|
||||
/// ```
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
|
@ -159,13 +159,13 @@ pub struct Conditional {
|
|||
if_keyword: Keyword,
|
||||
/// The condition of the conditional.
|
||||
#[get = "pub"]
|
||||
condition: ParenthesizedCondition,
|
||||
condition: Parenthesized,
|
||||
}
|
||||
|
||||
impl Conditional {
|
||||
/// Dissolves the [`Conditional`] into its components.
|
||||
#[must_use]
|
||||
pub fn dissolve(self) -> (Keyword, ParenthesizedCondition) {
|
||||
pub fn dissolve(self) -> (Keyword, Parenthesized) {
|
||||
(self.if_keyword, self.condition)
|
||||
}
|
||||
}
|
||||
|
@ -773,7 +773,7 @@ impl<'a> Parser<'a> {
|
|||
// eat the if keyword
|
||||
self.forward();
|
||||
|
||||
let condition = self.parse_parenthesized_condition(handler)?;
|
||||
let condition = self.parse_parenthesized(handler)?;
|
||||
|
||||
let conditional = Conditional {
|
||||
if_keyword,
|
||||
|
|
|
@ -1,44 +1,12 @@
|
|||
//! Conversion functions for converting between tokens/ast-nodes and [`shulkerbox`] types
|
||||
|
||||
use shulkerbox::{
|
||||
datapack::Condition as DpCondition,
|
||||
util::{MacroString, MacroStringPart},
|
||||
};
|
||||
use shulkerbox::util::{MacroString, MacroStringPart};
|
||||
|
||||
use crate::{
|
||||
lexical::token::{MacroStringLiteral, MacroStringLiteralPart},
|
||||
syntax::syntax_tree::{
|
||||
condition::{
|
||||
BinaryCondition, Condition, ConditionalBinaryOperator, ConditionalPrefixOperator,
|
||||
PrimaryCondition,
|
||||
},
|
||||
AnyStringLiteral,
|
||||
},
|
||||
syntax::syntax_tree::AnyStringLiteral,
|
||||
};
|
||||
|
||||
impl From<Condition> for DpCondition {
|
||||
fn from(value: Condition) -> Self {
|
||||
match value {
|
||||
Condition::Primary(primary) => primary.into(),
|
||||
Condition::Binary(binary) => binary.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PrimaryCondition> for DpCondition {
|
||||
fn from(value: PrimaryCondition) -> Self {
|
||||
match value {
|
||||
PrimaryCondition::StringLiteral(literal) => Self::Atom(literal.into()),
|
||||
PrimaryCondition::Parenthesized(cond) => cond.dissolve().1.into(),
|
||||
PrimaryCondition::Unary(prefix) => match prefix.operator() {
|
||||
ConditionalPrefixOperator::LogicalNot(_) => {
|
||||
Self::Not(Box::new(prefix.dissolve().1.into()))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&AnyStringLiteral> for MacroString {
|
||||
fn from(value: &AnyStringLiteral) -> Self {
|
||||
match value {
|
||||
|
@ -88,17 +56,3 @@ impl From<MacroStringLiteral> for MacroString {
|
|||
Self::from(&value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BinaryCondition> for DpCondition {
|
||||
fn from(value: BinaryCondition) -> Self {
|
||||
let (lhs, op, rhs) = value.dissolve();
|
||||
match op {
|
||||
ConditionalBinaryOperator::LogicalAnd(_, _) => {
|
||||
Self::And(Box::new(lhs.into()), Box::new(rhs.into()))
|
||||
}
|
||||
ConditionalBinaryOperator::LogicalOr(_, _) => {
|
||||
Self::Or(Box::new(lhs.into()), Box::new(rhs.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,13 +5,12 @@ use std::{fmt::Display, sync::Arc};
|
|||
use super::{Scope, VariableData};
|
||||
use crate::{
|
||||
base::VoidHandler,
|
||||
lexical::token::MacroStringLiteralPart,
|
||||
syntax::syntax_tree::expression::{
|
||||
Binary, BinaryOperator, Expression, PrefixOperator, Primary,
|
||||
},
|
||||
};
|
||||
|
||||
use derive_more::From;
|
||||
|
||||
#[cfg(feature = "shulkerbox")]
|
||||
use shulkerbox::prelude::{Command, Condition, Execute};
|
||||
|
||||
|
@ -29,11 +28,12 @@ use crate::{
|
|||
|
||||
/// Compile-time evaluated value
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, From)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ComptimeValue {
|
||||
Boolean(bool),
|
||||
Integer(i64),
|
||||
String(String),
|
||||
MacroString(String),
|
||||
}
|
||||
|
||||
impl Display for ComptimeValue {
|
||||
|
@ -42,6 +42,20 @@ impl Display for ComptimeValue {
|
|||
Self::Boolean(boolean) => write!(f, "{boolean}"),
|
||||
Self::Integer(int) => write!(f, "{int}"),
|
||||
Self::String(string) => write!(f, "{string}"),
|
||||
Self::MacroString(macro_string) => write!(f, "{macro_string}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ComptimeValue {
|
||||
/// Returns the value as a string not containing a macro.
|
||||
#[must_use]
|
||||
pub fn to_string_no_macro(&self) -> Option<String> {
|
||||
match self {
|
||||
Self::Boolean(boolean) => Some(boolean.to_string()),
|
||||
Self::Integer(int) => Some(int.to_string()),
|
||||
Self::String(string) => Some(string.clone()),
|
||||
Self::MacroString(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,19 +64,17 @@ impl Display for ComptimeValue {
|
|||
#[allow(missing_docs)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
|
||||
pub enum ValueType {
|
||||
ScoreboardValue,
|
||||
Tag,
|
||||
NumberStorage,
|
||||
BooleanStorage,
|
||||
Boolean,
|
||||
Integer,
|
||||
String,
|
||||
}
|
||||
|
||||
impl Display for ValueType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::ScoreboardValue => write!(f, "scoreboard value"),
|
||||
Self::Tag => write!(f, "tag"),
|
||||
Self::BooleanStorage => write!(f, "boolean storage"),
|
||||
Self::NumberStorage => write!(f, "number storage"),
|
||||
Self::Boolean => write!(f, "boolean"),
|
||||
Self::Integer => write!(f, "integer"),
|
||||
Self::String => write!(f, "string"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +98,22 @@ pub enum DataLocation {
|
|||
},
|
||||
}
|
||||
|
||||
impl DataLocation {
|
||||
/// Returns the type of the data location.
|
||||
#[must_use]
|
||||
pub fn value_type(&self) -> ValueType {
|
||||
match self {
|
||||
Self::ScoreboardValue { .. } => ValueType::Integer,
|
||||
Self::Tag { .. } => ValueType::Boolean,
|
||||
Self::Storage { r#type, .. } => match r#type {
|
||||
StorageType::Boolean => ValueType::Boolean,
|
||||
StorageType::Byte | StorageType::Int | StorageType::Long => ValueType::Integer,
|
||||
StorageType::Double => todo!("Double storage type"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of a storage.
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
|
||||
|
@ -133,10 +161,10 @@ impl Expression {
|
|||
|
||||
/// Evaluate at compile-time.
|
||||
#[must_use]
|
||||
pub fn comptime_eval(&self) -> Option<ComptimeValue> {
|
||||
pub fn comptime_eval(&self, scope: &Arc<Scope>) -> Option<ComptimeValue> {
|
||||
match self {
|
||||
Self::Primary(primary) => primary.comptime_eval(),
|
||||
Self::Binary(binary) => binary.comptime_eval(),
|
||||
Self::Primary(primary) => primary.comptime_eval(scope),
|
||||
Self::Binary(binary) => binary.comptime_eval(scope),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -146,30 +174,38 @@ impl Primary {
|
|||
#[must_use]
|
||||
pub fn can_yield_type(&self, r#type: ValueType, scope: &Arc<Scope>) -> bool {
|
||||
match self {
|
||||
Self::Boolean(_) => matches!(r#type, ValueType::Tag | ValueType::BooleanStorage),
|
||||
Self::Integer(_) => matches!(r#type, ValueType::ScoreboardValue),
|
||||
Self::FunctionCall(_) => matches!(
|
||||
r#type,
|
||||
ValueType::ScoreboardValue | ValueType::Tag | ValueType::BooleanStorage
|
||||
),
|
||||
Self::Boolean(_) => matches!(r#type, ValueType::Boolean),
|
||||
Self::Integer(_) => matches!(r#type, ValueType::Integer),
|
||||
Self::FunctionCall(_) => matches!(r#type, ValueType::Integer | ValueType::Boolean),
|
||||
Self::Identifier(ident) => {
|
||||
scope
|
||||
.get_variable(ident.span.str())
|
||||
.map_or(false, |variable| match r#type {
|
||||
ValueType::BooleanStorage => {
|
||||
matches!(variable.as_ref(), VariableData::BooleanStorage { .. })
|
||||
ValueType::Boolean => {
|
||||
matches!(
|
||||
variable.as_ref(),
|
||||
VariableData::Tag { .. } | VariableData::BooleanStorage { .. }
|
||||
)
|
||||
}
|
||||
ValueType::NumberStorage => false,
|
||||
ValueType::ScoreboardValue => {
|
||||
ValueType::Integer => {
|
||||
matches!(variable.as_ref(), VariableData::ScoreboardValue { .. })
|
||||
}
|
||||
ValueType::Tag => matches!(variable.as_ref(), VariableData::Tag { .. }),
|
||||
ValueType::String => false,
|
||||
})
|
||||
}
|
||||
Self::Parenthesized(parenthesized) => {
|
||||
parenthesized.expression().can_yield_type(r#type, scope)
|
||||
}
|
||||
Self::Prefix(_) => todo!(),
|
||||
Self::Prefix(prefix) => match prefix.operator() {
|
||||
PrefixOperator::LogicalNot(_) => {
|
||||
matches!(r#type, ValueType::Boolean)
|
||||
&& prefix.operand().can_yield_type(r#type, scope)
|
||||
}
|
||||
PrefixOperator::Negate(_) => {
|
||||
matches!(r#type, ValueType::Integer)
|
||||
&& prefix.operand().can_yield_type(r#type, scope)
|
||||
}
|
||||
},
|
||||
// TODO: Add support for Lua.
|
||||
#[expect(clippy::match_same_arms)]
|
||||
Self::Lua(_) => false,
|
||||
|
@ -179,30 +215,26 @@ impl Primary {
|
|||
|
||||
/// Evaluate at compile-time.
|
||||
#[must_use]
|
||||
pub fn comptime_eval(&self) -> Option<ComptimeValue> {
|
||||
#[expect(clippy::match_same_arms)]
|
||||
pub fn comptime_eval(&self, scope: &Arc<Scope>) -> Option<ComptimeValue> {
|
||||
match self {
|
||||
Self::Boolean(boolean) => Some(ComptimeValue::Boolean(boolean.value())),
|
||||
Self::Integer(int) => Some(ComptimeValue::Integer(int.as_i64())),
|
||||
Self::StringLiteral(string_literal) => Some(ComptimeValue::String(
|
||||
string_literal.str_content().to_string(),
|
||||
)),
|
||||
Self::Identifier(_) => None,
|
||||
Self::Parenthesized(parenthesized) => parenthesized.expression().comptime_eval(),
|
||||
Self::Prefix(prefix) => {
|
||||
prefix
|
||||
.operand()
|
||||
.comptime_eval()
|
||||
.and_then(|val| match (prefix.operator(), val) {
|
||||
(PrefixOperator::LogicalNot(_), ComptimeValue::Boolean(boolean)) => {
|
||||
Some(ComptimeValue::Boolean(!boolean))
|
||||
}
|
||||
(PrefixOperator::Negate(_), ComptimeValue::Integer(int)) => {
|
||||
Some(ComptimeValue::Integer(-int))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
Self::Identifier(_) | Self::FunctionCall(_) => None,
|
||||
Self::Parenthesized(parenthesized) => parenthesized.expression().comptime_eval(scope),
|
||||
Self::Prefix(prefix) => prefix.operand().comptime_eval(scope).and_then(|val| {
|
||||
match (prefix.operator(), val) {
|
||||
(PrefixOperator::LogicalNot(_), ComptimeValue::Boolean(boolean)) => {
|
||||
Some(ComptimeValue::Boolean(!boolean))
|
||||
}
|
||||
(PrefixOperator::Negate(_), ComptimeValue::Integer(int)) => {
|
||||
Some(ComptimeValue::Integer(-int))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}),
|
||||
// TODO: correctly evaluate lua code
|
||||
Self::Lua(lua) => lua
|
||||
.eval_string(&VoidHandler)
|
||||
|
@ -210,11 +242,18 @@ impl Primary {
|
|||
.flatten()
|
||||
.map(ComptimeValue::String),
|
||||
Self::MacroStringLiteral(macro_string_literal) => {
|
||||
// TODO: mark as containing macros
|
||||
Some(ComptimeValue::String(macro_string_literal.str_content()))
|
||||
if macro_string_literal
|
||||
.parts()
|
||||
.iter()
|
||||
.any(|part| matches!(part, MacroStringLiteralPart::MacroUsage { .. }))
|
||||
{
|
||||
Some(ComptimeValue::MacroString(
|
||||
macro_string_literal.str_content(),
|
||||
))
|
||||
} else {
|
||||
Some(ComptimeValue::String(macro_string_literal.str_content()))
|
||||
}
|
||||
}
|
||||
// TODO: correctly evaluate function calls
|
||||
Self::FunctionCall(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -222,11 +261,31 @@ impl Primary {
|
|||
impl Binary {
|
||||
/// Evaluate at compile-time.
|
||||
#[must_use]
|
||||
pub fn comptime_eval(&self) -> Option<ComptimeValue> {
|
||||
let left = self.left_operand().comptime_eval()?;
|
||||
let right = self.right_operand().comptime_eval()?;
|
||||
pub fn comptime_eval(&self, scope: &Arc<Scope>) -> Option<ComptimeValue> {
|
||||
let left = self.left_operand().comptime_eval(scope)?;
|
||||
let right = self.right_operand().comptime_eval(scope)?;
|
||||
|
||||
match (left, right) {
|
||||
(ComptimeValue::Boolean(true), _) | (_, ComptimeValue::Boolean(true)) if matches!(self.operator(), BinaryOperator::LogicalOr(..))
|
||||
// TODO: re-enable if can_yield_type works properly
|
||||
/*&& self
|
||||
.left_operand()
|
||||
.can_yield_type(ValueType::Boolean, scope)
|
||||
&& self
|
||||
.right_operand()
|
||||
.can_yield_type(ValueType::Boolean, scope)*/ => {
|
||||
Some(ComptimeValue::Boolean(true))
|
||||
}
|
||||
(ComptimeValue::Boolean(false), _) | (_, ComptimeValue::Boolean(false)) if matches!(self.operator(), BinaryOperator::LogicalAnd(..))
|
||||
// TODO: re-enable if can_yield_type works properly
|
||||
/*&& self
|
||||
.left_operand()
|
||||
.can_yield_type(ValueType::Boolean, scope)
|
||||
&& self
|
||||
.right_operand()
|
||||
.can_yield_type(ValueType::Boolean, scope)*/ => {
|
||||
Some(ComptimeValue::Boolean(false))
|
||||
}
|
||||
(ComptimeValue::Boolean(left), ComptimeValue::Boolean(right)) => {
|
||||
match self.operator() {
|
||||
BinaryOperator::Equal(..) => Some(ComptimeValue::Boolean(left == right)),
|
||||
|
@ -236,21 +295,6 @@ impl Binary {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
// TODO: check that the other value will be boolean (even if not comptime)
|
||||
(ComptimeValue::Boolean(true), _) | (_, ComptimeValue::Boolean(true)) => {
|
||||
if matches!(self.operator(), BinaryOperator::LogicalOr(..)) {
|
||||
Some(ComptimeValue::Boolean(true))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
(ComptimeValue::Boolean(false), _) | (_, ComptimeValue::Boolean(false)) => {
|
||||
if matches!(self.operator(), BinaryOperator::LogicalAnd(..)) {
|
||||
Some(ComptimeValue::Boolean(false))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
(ComptimeValue::Integer(left), ComptimeValue::Integer(right)) => {
|
||||
match self.operator() {
|
||||
BinaryOperator::Add(..) => left.checked_add(right).map(ComptimeValue::Integer),
|
||||
|
@ -437,7 +481,7 @@ impl Transpiler {
|
|||
))])
|
||||
}
|
||||
DataLocation::Tag { .. } => Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Tag,
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: primary.span(),
|
||||
})),
|
||||
DataLocation::Storage {
|
||||
|
@ -462,7 +506,7 @@ impl Transpiler {
|
|||
} else {
|
||||
Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: primary.span(),
|
||||
expected_type: ValueType::NumberStorage,
|
||||
expected_type: ValueType::Integer,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -478,11 +522,7 @@ impl Transpiler {
|
|||
}
|
||||
Primary::StringLiteral(_) | Primary::MacroStringLiteral(_) => {
|
||||
Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: match target {
|
||||
DataLocation::ScoreboardValue { .. } => ValueType::ScoreboardValue,
|
||||
DataLocation::Tag { .. } => ValueType::Tag,
|
||||
DataLocation::Storage { .. } => ValueType::NumberStorage,
|
||||
},
|
||||
expected_type: target.value_type(),
|
||||
expression: primary.span(),
|
||||
}))
|
||||
}
|
||||
|
@ -552,7 +592,7 @@ impl Transpiler {
|
|||
} else {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: primary.span(),
|
||||
expected_type: ValueType::BooleanStorage,
|
||||
expected_type: target.value_type(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
|
@ -593,7 +633,7 @@ impl Transpiler {
|
|||
} else {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: primary.span(),
|
||||
expected_type: ValueType::NumberStorage,
|
||||
expected_type: target.value_type(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
|
@ -601,7 +641,7 @@ impl Transpiler {
|
|||
}
|
||||
DataLocation::Tag { .. } => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Tag,
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: primary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
|
@ -610,13 +650,7 @@ impl Transpiler {
|
|||
},
|
||||
_ => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: match target {
|
||||
DataLocation::ScoreboardValue { .. } => {
|
||||
ValueType::ScoreboardValue
|
||||
}
|
||||
DataLocation::Tag { .. } => ValueType::Tag,
|
||||
DataLocation::Storage { .. } => ValueType::NumberStorage,
|
||||
},
|
||||
expected_type: target.value_type(),
|
||||
expression: primary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
|
@ -634,23 +668,450 @@ impl Transpiler {
|
|||
}
|
||||
}
|
||||
|
||||
#[expect(clippy::needless_pass_by_ref_mut)]
|
||||
fn transpile_binary_expression(
|
||||
&mut self,
|
||||
binary: &Binary,
|
||||
target: &DataLocation,
|
||||
_scope: &Arc<super::Scope>,
|
||||
_handler: &impl Handler<base::Error>,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Vec<Command>> {
|
||||
match binary.comptime_eval() {
|
||||
Some(ComptimeValue::Integer(value)) => match target {
|
||||
if let Some(value) = binary.comptime_eval(scope) {
|
||||
self.transpile_comptime_value(&value, binary, target, scope, handler)
|
||||
} else {
|
||||
match binary.operator() {
|
||||
BinaryOperator::Add(_)
|
||||
| BinaryOperator::Subtract(_)
|
||||
| BinaryOperator::Multiply(_)
|
||||
| BinaryOperator::Divide(_)
|
||||
| BinaryOperator::Modulo(_) => {
|
||||
self.transpile_scoreboard_operation(binary, target, scope, handler)
|
||||
}
|
||||
BinaryOperator::Equal(..)
|
||||
| BinaryOperator::GreaterThan(_)
|
||||
| BinaryOperator::GreaterThanOrEqual(..)
|
||||
| BinaryOperator::LessThan(_)
|
||||
| BinaryOperator::LessThanOrEqual(..)
|
||||
| BinaryOperator::NotEqual(..)
|
||||
| BinaryOperator::LogicalAnd(..)
|
||||
| BinaryOperator::LogicalOr(..) => {
|
||||
let (mut cmds, cond) =
|
||||
self.transpile_binary_expression_as_condition(binary, scope, handler)?;
|
||||
|
||||
let (success_cmd, else_cmd) = match target {
|
||||
DataLocation::ScoreboardValue { objective, target } => (
|
||||
format!("scoreboard players set {target} {objective} 1"),
|
||||
format!("scoreboard players set {target} {objective} 0"),
|
||||
),
|
||||
DataLocation::Storage {
|
||||
storage_name,
|
||||
path,
|
||||
r#type,
|
||||
} => {
|
||||
if matches!(r#type, StorageType::Boolean) {
|
||||
(
|
||||
format!(
|
||||
"data modify storage {storage_name} {path} set value 1b"
|
||||
),
|
||||
format!(
|
||||
"data modify storage {storage_name} {path} set value 0b"
|
||||
),
|
||||
)
|
||||
} else {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: binary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
DataLocation::Tag { tag_name, entity } => (
|
||||
format!("tag {entity} add {tag_name}"),
|
||||
format!("tag {entity} remove {tag_name}"),
|
||||
),
|
||||
};
|
||||
|
||||
cmds.push(Command::Execute(Execute::If(
|
||||
cond,
|
||||
Box::new(Execute::Run(Box::new(Command::Raw(success_cmd)))),
|
||||
Some(Box::new(Execute::Run(Box::new(Command::Raw(else_cmd))))),
|
||||
)));
|
||||
|
||||
Ok(cmds)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn transpile_expression_as_condition(
|
||||
&mut self,
|
||||
expression: &Expression,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<(Vec<Command>, Condition)> {
|
||||
match expression {
|
||||
Expression::Primary(primary) => {
|
||||
self.transpile_primary_expression_as_condition(primary, scope, handler)
|
||||
}
|
||||
Expression::Binary(binary) => {
|
||||
self.transpile_binary_expression_as_condition(binary, scope, handler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn transpile_primary_expression_as_condition(
|
||||
&mut self,
|
||||
primary: &Primary,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<(Vec<Command>, Condition)> {
|
||||
match primary {
|
||||
Primary::Boolean(_) => todo!("handle boolean literal if not catched by comptime eval"),
|
||||
Primary::Integer(_) => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: primary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
Primary::StringLiteral(s) => Ok((
|
||||
Vec::new(),
|
||||
Condition::Atom(s.str_content().to_string().into()),
|
||||
)),
|
||||
Primary::MacroStringLiteral(macro_string) => {
|
||||
Ok((Vec::new(), Condition::Atom(macro_string.into())))
|
||||
}
|
||||
Primary::FunctionCall(func) => {
|
||||
if func
|
||||
.arguments()
|
||||
.as_ref()
|
||||
.is_some_and(|args| !args.is_empty())
|
||||
{
|
||||
let err =
|
||||
TranspileError::FunctionArgumentsNotAllowed(FunctionArgumentsNotAllowed {
|
||||
arguments: func.arguments().as_ref().unwrap().span(),
|
||||
message: "Function calls as conditions do not support arguments."
|
||||
.into(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
} else {
|
||||
let (func_location, _) = self.get_or_transpile_function(
|
||||
&func.identifier().span,
|
||||
None,
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
|
||||
Ok((
|
||||
Vec::new(),
|
||||
Condition::Atom(format!("function {func_location}").into()),
|
||||
))
|
||||
}
|
||||
}
|
||||
Primary::Identifier(ident) => {
|
||||
#[expect(clippy::option_if_let_else)]
|
||||
if let Some(variable) = scope.get_variable(ident.span.str()).as_deref() {
|
||||
match variable {
|
||||
VariableData::BooleanStorage { storage_name, path } => Ok((
|
||||
Vec::new(),
|
||||
Condition::Atom(format!("data storage {storage_name} {{{path}: 1b}}").into()),
|
||||
)),
|
||||
VariableData::FunctionArgument { .. } => {
|
||||
Ok((
|
||||
Vec::new(),
|
||||
Condition::Atom(shulkerbox::util::MacroString::MacroString(vec![
|
||||
shulkerbox::util::MacroStringPart::MacroUsage(ident.span.str().to_string()),
|
||||
]))
|
||||
))
|
||||
}
|
||||
_ => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: primary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let err = TranspileError::UnknownIdentifier(UnknownIdentifier {
|
||||
identifier: ident.span.clone(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
|
||||
},
|
||||
Primary::Parenthesized(parenthesized) => {
|
||||
self.transpile_expression_as_condition(parenthesized.expression(), scope, handler)
|
||||
}
|
||||
Primary::Prefix(prefix) => match prefix.operator() {
|
||||
PrefixOperator::LogicalNot(_) => {
|
||||
let (cmds, cond) = self.transpile_primary_expression_as_condition(
|
||||
prefix.operand(),
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
Ok((cmds, Condition::Not(Box::new(cond))))
|
||||
}
|
||||
PrefixOperator::Negate(_) => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: primary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
},
|
||||
},
|
||||
Primary::Lua(_) => todo!("Lua code as condition"),
|
||||
}
|
||||
}
|
||||
|
||||
fn transpile_binary_expression_as_condition(
|
||||
&mut self,
|
||||
binary: &Binary,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<(Vec<Command>, Condition)> {
|
||||
match binary.operator() {
|
||||
BinaryOperator::Equal(..)
|
||||
| BinaryOperator::NotEqual(..)
|
||||
| BinaryOperator::GreaterThan(_)
|
||||
| BinaryOperator::GreaterThanOrEqual(..)
|
||||
| BinaryOperator::LessThan(_)
|
||||
| BinaryOperator::LessThanOrEqual(..) => {
|
||||
self.transpile_comparison_operator(binary, scope, handler)
|
||||
}
|
||||
BinaryOperator::LogicalAnd(..) | BinaryOperator::LogicalOr(..) => {
|
||||
self.transpile_logic_operator(binary, scope, handler)
|
||||
}
|
||||
_ => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: binary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn transpile_scoreboard_operation(
|
||||
&mut self,
|
||||
binary: &Binary,
|
||||
target: &DataLocation,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Vec<Command>> {
|
||||
let left = binary.left_operand();
|
||||
let right = binary.right_operand();
|
||||
let operator = binary.operator();
|
||||
|
||||
let (temp_objective, temp_locations) = self.get_temp_scoreboard_locations(2);
|
||||
|
||||
let score_target_location = match target {
|
||||
DataLocation::ScoreboardValue { objective, target } => (objective, target),
|
||||
_ => (&temp_objective, &temp_locations[0]),
|
||||
};
|
||||
|
||||
let left_cmds = self.transpile_expression(
|
||||
left,
|
||||
&DataLocation::ScoreboardValue {
|
||||
objective: score_target_location.0.clone(),
|
||||
target: score_target_location.1.clone(),
|
||||
},
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
let right_cmds = self.transpile_expression(
|
||||
right,
|
||||
&DataLocation::ScoreboardValue {
|
||||
objective: temp_objective.clone(),
|
||||
target: temp_locations[1].clone(),
|
||||
},
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
|
||||
let calc_cmds = {
|
||||
let (target_objective, target) = score_target_location;
|
||||
let source = &temp_locations[1];
|
||||
let source_objective = &temp_objective;
|
||||
|
||||
let operation = match operator {
|
||||
BinaryOperator::Add(_) => "+=",
|
||||
BinaryOperator::Subtract(_) => "-=",
|
||||
BinaryOperator::Multiply(_) => "*=",
|
||||
BinaryOperator::Divide(_) => "/=",
|
||||
BinaryOperator::Modulo(_) => "%=",
|
||||
_ => unreachable!("This operator should not be handled here."),
|
||||
};
|
||||
|
||||
vec![Command::Raw(format!(
|
||||
"scoreboard players operation {target} {target_objective} {operation} {source} {source_objective}"
|
||||
))]
|
||||
};
|
||||
|
||||
let transfer_cmd = match target {
|
||||
DataLocation::ScoreboardValue { .. } => None,
|
||||
DataLocation::Tag { .. } => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: binary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
return Err(err);
|
||||
}
|
||||
DataLocation::Storage {
|
||||
storage_name,
|
||||
path,
|
||||
r#type,
|
||||
} => match r#type {
|
||||
StorageType::Byte | StorageType::Double | StorageType::Int | StorageType::Long => {
|
||||
Some(Command::Execute(Execute::Store(
|
||||
format!(
|
||||
"result storage {storage_name} {path} {t} 1",
|
||||
t = r#type.as_str()
|
||||
)
|
||||
.into(),
|
||||
Box::new(Execute::Run(Box::new(Command::Raw(format!(
|
||||
"scoreboard players get {target} {objective}",
|
||||
objective = score_target_location.0,
|
||||
target = score_target_location.1
|
||||
))))),
|
||||
)))
|
||||
}
|
||||
StorageType::Boolean => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: binary.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
return Err(err);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Ok(left_cmds
|
||||
.into_iter()
|
||||
.chain(right_cmds)
|
||||
.chain(calc_cmds)
|
||||
.chain(transfer_cmd)
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn transpile_comparison_operator(
|
||||
&mut self,
|
||||
binary: &Binary,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<(Vec<Command>, Condition)> {
|
||||
let invert = matches!(binary.operator(), BinaryOperator::NotEqual(..));
|
||||
|
||||
// TODO: evaluate comptime values and compare using `matches` and integer ranges
|
||||
|
||||
let operator = match binary.operator() {
|
||||
BinaryOperator::Equal(..) | BinaryOperator::NotEqual(..) => "=",
|
||||
BinaryOperator::GreaterThan(_) => ">",
|
||||
BinaryOperator::GreaterThanOrEqual(..) => ">=",
|
||||
BinaryOperator::LessThan(_) => "<",
|
||||
BinaryOperator::LessThanOrEqual(..) => "<=",
|
||||
_ => unreachable!("This function should only be called for comparison operators."),
|
||||
};
|
||||
|
||||
let (temp_objective, mut temp_locations) = self.get_temp_scoreboard_locations(2);
|
||||
|
||||
let condition = Condition::Atom(
|
||||
format!(
|
||||
"score {target} {temp_objective} {operator} {source} {temp_objective}",
|
||||
target = temp_locations[0],
|
||||
source = temp_locations[1]
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
let left_cmds = self.transpile_expression(
|
||||
binary.left_operand(),
|
||||
&DataLocation::ScoreboardValue {
|
||||
objective: temp_objective.clone(),
|
||||
target: std::mem::take(&mut temp_locations[0]),
|
||||
},
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
let right_cmds = self.transpile_expression(
|
||||
binary.right_operand(),
|
||||
&DataLocation::ScoreboardValue {
|
||||
objective: temp_objective,
|
||||
target: std::mem::take(&mut temp_locations[1]),
|
||||
},
|
||||
scope,
|
||||
handler,
|
||||
)?;
|
||||
|
||||
Ok((
|
||||
left_cmds.into_iter().chain(right_cmds).collect(),
|
||||
if invert {
|
||||
Condition::Not(Box::new(condition))
|
||||
} else {
|
||||
condition
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn transpile_logic_operator(
|
||||
&mut self,
|
||||
binary: &Binary,
|
||||
scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<(Vec<Command>, Condition)> {
|
||||
let left = binary.left_operand().as_ref();
|
||||
let right = binary.right_operand().as_ref();
|
||||
|
||||
let (left_cmds, left_cond) =
|
||||
self.transpile_expression_as_condition(left, scope, handler)?;
|
||||
let (right_cmds, right_cond) =
|
||||
self.transpile_expression_as_condition(right, scope, handler)?;
|
||||
|
||||
let combined_cmds = left_cmds.into_iter().chain(right_cmds).collect();
|
||||
|
||||
match binary.operator() {
|
||||
BinaryOperator::LogicalAnd(..) => Ok((
|
||||
combined_cmds,
|
||||
Condition::And(Box::new(left_cond), Box::new(right_cond)),
|
||||
)),
|
||||
BinaryOperator::LogicalOr(..) => Ok((
|
||||
combined_cmds,
|
||||
Condition::Or(Box::new(left_cond), Box::new(right_cond)),
|
||||
)),
|
||||
_ => unreachable!("This function should only be called for logical operators."),
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(clippy::unused_self)]
|
||||
fn transpile_comptime_value(
|
||||
&self,
|
||||
value: &ComptimeValue,
|
||||
original: &impl SourceElement,
|
||||
target: &DataLocation,
|
||||
_scope: &Arc<super::Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Vec<Command>> {
|
||||
match value {
|
||||
ComptimeValue::Integer(value) => match target {
|
||||
DataLocation::ScoreboardValue { objective, target } => Ok(vec![Command::Raw(
|
||||
format!("scoreboard players set {target} {objective} {value}"),
|
||||
)]),
|
||||
DataLocation::Tag { .. } => Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Tag,
|
||||
expression: binary.span(),
|
||||
})),
|
||||
DataLocation::Tag { .. } => {
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: ValueType::Boolean,
|
||||
expression: original.span(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
DataLocation::Storage {
|
||||
storage_name,
|
||||
path,
|
||||
|
@ -668,14 +1129,16 @@ impl Transpiler {
|
|||
suffix = r#type.suffix(),
|
||||
))])
|
||||
} else {
|
||||
Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: binary.span(),
|
||||
expected_type: ValueType::NumberStorage,
|
||||
}))
|
||||
let err = TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: original.span(),
|
||||
expected_type: target.value_type(),
|
||||
});
|
||||
handler.receive(err.clone());
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(ComptimeValue::Boolean(value)) => match target {
|
||||
&ComptimeValue::Boolean(value) => match target {
|
||||
DataLocation::ScoreboardValue { objective, target } => {
|
||||
Ok(vec![Command::Raw(format!(
|
||||
"scoreboard players set {target} {objective} {value}",
|
||||
|
@ -699,29 +1162,39 @@ impl Transpiler {
|
|||
))])
|
||||
} else {
|
||||
Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expression: binary.span(),
|
||||
expected_type: ValueType::NumberStorage,
|
||||
expression: original.span(),
|
||||
expected_type: target.value_type(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(ComptimeValue::String(_)) => {
|
||||
ComptimeValue::String(_) | ComptimeValue::MacroString(_) => {
|
||||
Err(TranspileError::MismatchedTypes(MismatchedTypes {
|
||||
expected_type: match target {
|
||||
DataLocation::ScoreboardValue { .. } => ValueType::ScoreboardValue,
|
||||
DataLocation::Tag { .. } => ValueType::Tag,
|
||||
DataLocation::Storage { .. } => ValueType::NumberStorage,
|
||||
},
|
||||
expression: binary.span(),
|
||||
expected_type: target.value_type(),
|
||||
expression: original.span(),
|
||||
}))
|
||||
}
|
||||
None => {
|
||||
let _left = binary.left_operand();
|
||||
let _right = binary.right_operand();
|
||||
let _operator = binary.operator();
|
||||
|
||||
todo!("Transpile binary expression")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get temporary scoreboard locations.
|
||||
fn get_temp_scoreboard_locations(&mut self, amount: usize) -> (String, Vec<String>) {
|
||||
let objective = "shu_temp_".to_string()
|
||||
+ &chksum_md5::hash(&self.main_namespace_name).to_hex_lowercase();
|
||||
|
||||
self.datapack
|
||||
.register_scoreboard(&objective, None::<&str>, None::<&str>);
|
||||
|
||||
let targets = (0..amount)
|
||||
.map(|i| {
|
||||
chksum_md5::hash(format!("{namespace}\0{j}", namespace = self.main_namespace_name, j = i + self.temp_counter))
|
||||
.to_hex_lowercase()
|
||||
.split_off(16)
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.temp_counter = self.temp_counter.wrapping_add(amount);
|
||||
|
||||
(objective, targets)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ use crate::{
|
|||
|
||||
use super::{
|
||||
error::{TranspileError, TranspileResult},
|
||||
expression::ComptimeValue,
|
||||
variables::{Scope, VariableData},
|
||||
FunctionData, TranspileAnnotationValue,
|
||||
};
|
||||
|
@ -42,6 +43,7 @@ pub struct Transpiler {
|
|||
pub(super) datapack: shulkerbox::datapack::Datapack,
|
||||
pub(super) setup_cmds: Vec<Command>,
|
||||
pub(super) initialized_constant_scores: HashSet<i64>,
|
||||
pub(super) temp_counter: usize,
|
||||
/// Top-level [`Scope`] for each program identifier
|
||||
scopes: BTreeMap<String, Arc<Scope<'static>>>,
|
||||
/// Key: (program identifier, function name)
|
||||
|
@ -60,28 +62,23 @@ impl Transpiler {
|
|||
datapack: shulkerbox::datapack::Datapack::new(main_namespace_name, pack_format),
|
||||
setup_cmds: Vec::new(),
|
||||
initialized_constant_scores: HashSet::new(),
|
||||
temp_counter: 0,
|
||||
scopes: BTreeMap::new(),
|
||||
functions: BTreeMap::new(),
|
||||
aliases: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes the transpiler and returns the resulting datapack.
|
||||
#[must_use]
|
||||
pub fn into_datapack(self) -> Datapack {
|
||||
self.datapack
|
||||
}
|
||||
|
||||
/// Transpiles the given programs.
|
||||
/// Transpiles the given programs and returns the resulting datapack.
|
||||
///
|
||||
/// # Errors
|
||||
/// - [`TranspileError::MissingFunctionDeclaration`] If a called function is missing
|
||||
#[tracing::instrument(level = "trace", skip_all)]
|
||||
pub fn transpile(
|
||||
&mut self,
|
||||
mut self,
|
||||
programs: &[ProgramFile],
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> Result<(), TranspileError> {
|
||||
) -> Result<Datapack, TranspileError> {
|
||||
tracing::trace!("Transpiling program declarations");
|
||||
for program in programs {
|
||||
let program_identifier = program
|
||||
|
@ -142,7 +139,7 @@ impl Transpiler {
|
|||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(self.datapack)
|
||||
}
|
||||
|
||||
/// Transpiles the given program.
|
||||
|
@ -334,8 +331,8 @@ impl Transpiler {
|
|||
|val| match val {
|
||||
TranspileAnnotationValue::None => Ok(identifier_span.str().to_string()),
|
||||
TranspileAnnotationValue::Expression(expr) => expr
|
||||
.comptime_eval()
|
||||
.map(|val| val.to_string())
|
||||
.comptime_eval(scope)
|
||||
.and_then(|val| val.to_string_no_macro())
|
||||
.ok_or_else(|| {
|
||||
let err = TranspileError::IllegalAnnotationContent(
|
||||
IllegalAnnotationContent {
|
||||
|
@ -506,9 +503,12 @@ impl Transpiler {
|
|||
}
|
||||
Statement::ExecuteBlock(execute) => {
|
||||
let child_scope = Scope::with_parent(scope);
|
||||
Ok(self
|
||||
.transpile_execute_block(execute, program_identifier, &child_scope, handler)?
|
||||
.map_or_else(Vec::new, |cmd| vec![cmd]))
|
||||
Ok(self.transpile_execute_block(
|
||||
execute,
|
||||
program_identifier,
|
||||
&child_scope,
|
||||
handler,
|
||||
)?)
|
||||
}
|
||||
Statement::DocComment(doccomment) => {
|
||||
let content = doccomment.content();
|
||||
|
@ -652,9 +652,15 @@ impl Transpiler {
|
|||
program_identifier: &str,
|
||||
scope: &Arc<Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Option<Command>> {
|
||||
) -> TranspileResult<Vec<Command>> {
|
||||
self.transpile_execute_block_internal(execute, program_identifier, scope, handler)
|
||||
.map(|ex| ex.map(Command::Execute))
|
||||
.map(|ex| {
|
||||
ex.map(|(mut pre_cmds, exec)| {
|
||||
pre_cmds.push(exec.into());
|
||||
pre_cmds
|
||||
})
|
||||
.unwrap_or_default()
|
||||
})
|
||||
}
|
||||
|
||||
fn transpile_execute_block_internal(
|
||||
|
@ -663,7 +669,7 @@ impl Transpiler {
|
|||
program_identifier: &str,
|
||||
scope: &Arc<Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Option<Execute>> {
|
||||
) -> TranspileResult<Option<(Vec<Command>, Execute)>> {
|
||||
match execute {
|
||||
ExecuteBlock::HeadTail(head, tail) => {
|
||||
let tail = match tail {
|
||||
|
@ -687,7 +693,7 @@ impl Transpiler {
|
|||
if commands.is_empty() {
|
||||
Ok(None)
|
||||
} else {
|
||||
Ok(Some(Execute::Runs(commands)))
|
||||
Ok(Some((Vec::new(), Execute::Runs(commands))))
|
||||
}
|
||||
}
|
||||
ExecuteBlockTail::ExecuteBlock(_, execute_block) => self
|
||||
|
@ -762,58 +768,66 @@ impl Transpiler {
|
|||
program_identifier: &str,
|
||||
scope: &Arc<Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Option<Execute>> {
|
||||
let (_, cond) = cond.clone().dissolve();
|
||||
let (_, cond, _) = cond.dissolve();
|
||||
) -> TranspileResult<Option<(Vec<Command>, Execute)>> {
|
||||
let cond_expression = cond.condition().expression().as_ref();
|
||||
|
||||
let mut errors = Vec::new();
|
||||
|
||||
let el = el
|
||||
.and_then(|el| {
|
||||
let (_, block) = el.clone().dissolve();
|
||||
let statements = block.statements();
|
||||
let cmds = statements
|
||||
.iter()
|
||||
.flat_map(|statement| {
|
||||
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||
.unwrap_or_else(|err| {
|
||||
errors.push(err);
|
||||
Vec::new()
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let el = el.and_then(|el| {
|
||||
let (_, block) = el.clone().dissolve();
|
||||
let statements = block.statements();
|
||||
let cmds = statements
|
||||
.iter()
|
||||
.flat_map(|statement| {
|
||||
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||
.unwrap_or_else(|err| {
|
||||
errors.push(err);
|
||||
Vec::new()
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
match cmds.len() {
|
||||
0 => None,
|
||||
1 => Some(Execute::Run(Box::new(
|
||||
cmds.into_iter().next().expect("length is 1"),
|
||||
))),
|
||||
_ => Some(Execute::Runs(cmds)),
|
||||
}
|
||||
})
|
||||
.map(Box::new);
|
||||
match cmds.len() {
|
||||
0 => None,
|
||||
1 => Some(Execute::Run(Box::new(
|
||||
cmds.into_iter().next().expect("length is 1"),
|
||||
))),
|
||||
_ => Some(Execute::Runs(cmds)),
|
||||
}
|
||||
});
|
||||
|
||||
if !errors.is_empty() {
|
||||
return Err(errors.remove(0));
|
||||
if let Some(ComptimeValue::Boolean(value)) = cond_expression.comptime_eval(scope) {
|
||||
if value {
|
||||
Ok(Some((Vec::new(), then)))
|
||||
} else {
|
||||
Ok(el.map(|el| (Vec::new(), el)))
|
||||
}
|
||||
} else {
|
||||
if !errors.is_empty() {
|
||||
return Err(errors.remove(0));
|
||||
}
|
||||
|
||||
let (pre_cond_cmds, cond) =
|
||||
self.transpile_expression_as_condition(cond_expression, scope, handler)?;
|
||||
|
||||
Ok(Some((
|
||||
pre_cond_cmds,
|
||||
Execute::If(cond, Box::new(then), el.map(Box::new)),
|
||||
)))
|
||||
}
|
||||
|
||||
Ok(Some(Execute::If(
|
||||
datapack::Condition::from(cond),
|
||||
Box::new(then),
|
||||
el,
|
||||
)))
|
||||
}
|
||||
|
||||
fn combine_execute_head_tail(
|
||||
&mut self,
|
||||
head: &ExecuteBlockHead,
|
||||
tail: Option<Execute>,
|
||||
tail: Option<(Vec<Command>, Execute)>,
|
||||
program_identifier: &str,
|
||||
scope: &Arc<Scope>,
|
||||
handler: &impl Handler<base::Error>,
|
||||
) -> TranspileResult<Option<Execute>> {
|
||||
) -> TranspileResult<Option<(Vec<Command>, Execute)>> {
|
||||
Ok(match head {
|
||||
ExecuteBlockHead::Conditional(cond) => {
|
||||
if let Some(tail) = tail {
|
||||
if let Some((mut pre_cmds, tail)) = tail {
|
||||
self.transpile_conditional(
|
||||
cond,
|
||||
tail,
|
||||
|
@ -822,57 +836,88 @@ impl Transpiler {
|
|||
scope,
|
||||
handler,
|
||||
)?
|
||||
.map(|(pre_cond_cmds, cond)| {
|
||||
pre_cmds.extend(pre_cond_cmds);
|
||||
(pre_cmds, cond)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ExecuteBlockHead::As(r#as) => {
|
||||
let selector = r#as.as_selector();
|
||||
tail.map(|tail| Execute::As(selector.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::As(selector.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::At(at) => {
|
||||
let selector = at.at_selector();
|
||||
tail.map(|tail| Execute::At(selector.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::At(selector.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Align(align) => {
|
||||
let align = align.align_selector();
|
||||
tail.map(|tail| Execute::Align(align.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Align(align.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Anchored(anchored) => {
|
||||
let anchor = anchored.anchored_selector();
|
||||
tail.map(|tail| Execute::Anchored(anchor.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Anchored(anchor.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::In(r#in) => {
|
||||
let dimension = r#in.in_selector();
|
||||
tail.map(|tail| Execute::In(dimension.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::In(dimension.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Positioned(positioned) => {
|
||||
let position = positioned.positioned_selector();
|
||||
tail.map(|tail| Execute::Positioned(position.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(
|
||||
pre_cmds,
|
||||
Execute::Positioned(position.into(), Box::new(tail)),
|
||||
)
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Rotated(rotated) => {
|
||||
let rotation = rotated.rotated_selector();
|
||||
tail.map(|tail| Execute::Rotated(rotation.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Rotated(rotation.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Facing(facing) => {
|
||||
let facing = facing.facing_selector();
|
||||
tail.map(|tail| Execute::Facing(facing.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Facing(facing.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::AsAt(as_at) => {
|
||||
let selector = as_at.asat_selector();
|
||||
tail.map(|tail| Execute::AsAt(selector.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::AsAt(selector.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::On(on) => {
|
||||
let dimension = on.on_selector();
|
||||
tail.map(|tail| Execute::On(dimension.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::On(dimension.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Store(store) => {
|
||||
let store = store.store_selector();
|
||||
tail.map(|tail| Execute::Store(store.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Store(store.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
ExecuteBlockHead::Summon(summon) => {
|
||||
let entity = summon.summon_selector();
|
||||
tail.map(|tail| Execute::Summon(entity.into(), Box::new(tail)))
|
||||
tail.map(|(pre_cmds, tail)| {
|
||||
(pre_cmds, Execute::Summon(entity.into(), Box::new(tail)))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ use chksum_md5 as md5;
|
|||
#[cfg(feature = "shulkerbox")]
|
||||
use shulkerbox::prelude::{Command, Condition, Execute};
|
||||
|
||||
use strum::EnumIs;
|
||||
use enum_as_inner::EnumAsInner;
|
||||
|
||||
use crate::{
|
||||
base::{self, source_file::SourceElement as _, Handler},
|
||||
|
@ -33,7 +33,7 @@ use super::{
|
|||
use super::Transpiler;
|
||||
|
||||
/// Stores the data required to access a variable.
|
||||
#[derive(Debug, Clone, EnumIs)]
|
||||
#[derive(Debug, Clone, EnumAsInner)]
|
||||
pub enum VariableData {
|
||||
/// A function.
|
||||
Function {
|
||||
|
@ -376,8 +376,8 @@ fn get_single_data_location_identifiers(
|
|||
) = (name, target)
|
||||
{
|
||||
if let (Some(name_eval), Some(target_eval)) = (
|
||||
objective.comptime_eval().map(|val| val.to_string()),
|
||||
target.comptime_eval().map(|val| val.to_string()),
|
||||
objective.comptime_eval(scope).map(|val| val.to_string()),
|
||||
target.comptime_eval(scope).map(|val| val.to_string()),
|
||||
) {
|
||||
// TODO: change invalid criteria if boolean
|
||||
if !crate::util::is_valid_scoreboard_objective_name(&name_eval) {
|
||||
|
|
Loading…
Reference in New Issue