Compare commits
5 Commits
4bfae0c960
...
a07f16f283
Author | SHA1 | Date |
---|---|---|
|
a07f16f283 | |
|
38f90e6491 | |
|
0d0df920ee | |
|
b8303689db | |
|
a6a5e42b6b |
|
@ -3,7 +3,7 @@ on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- development
|
- develop
|
||||||
- 'releases/**'
|
- 'releases/**'
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
|
|
|
@ -304,7 +304,9 @@ impl Semicolon {
|
||||||
Err(error)
|
Err(error)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
SemicolonStatement::VariableDeclaration(decl) => decl.analyze_semantics(handler),
|
SemicolonStatement::VariableDeclaration(decl) => {
|
||||||
|
decl.analyze_semantics(function_names, macro_names, handler)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -521,61 +523,87 @@ impl VariableDeclaration {
|
||||||
/// Analyzes the semantics of a variable declaration.
|
/// Analyzes the semantics of a variable declaration.
|
||||||
pub fn analyze_semantics(
|
pub fn analyze_semantics(
|
||||||
&self,
|
&self,
|
||||||
|
function_names: &HashSet<String>,
|
||||||
|
macro_names: &HashSet<String>,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> Result<(), error::Error> {
|
) -> Result<(), error::Error> {
|
||||||
match self.expression() {
|
match self {
|
||||||
Expression::Primary(Primary::Integer(num)) => {
|
Self::Array(array) => array.assignment().as_ref().map_or(Ok(()), |assignment| {
|
||||||
if self.variable_type().keyword == KeywordKind::Bool {
|
assignment
|
||||||
|
.expression()
|
||||||
|
.analyze_semantics(function_names, macro_names, handler)
|
||||||
|
}),
|
||||||
|
Self::Single(single) => {
|
||||||
|
if let Some(assignment) = single.assignment() {
|
||||||
|
let err = match single.variable_type().keyword {
|
||||||
|
KeywordKind::Int => !matches!(
|
||||||
|
assignment.expression(),
|
||||||
|
// TODO: also allow macro identifier but not macro string literal
|
||||||
|
Expression::Primary(
|
||||||
|
Primary::Integer(_) | Primary::Lua(_) | Primary::FunctionCall(_)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
KeywordKind::Bool => !matches!(
|
||||||
|
assignment.expression(),
|
||||||
|
Expression::Primary(Primary::Boolean(_) | Primary::Lua(_))
|
||||||
|
),
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
if err {
|
||||||
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
||||||
Expression::Primary(Primary::Integer(num.clone())),
|
assignment.expression().clone(),
|
||||||
));
|
));
|
||||||
handler.receive(err.clone());
|
handler.receive(err.clone());
|
||||||
Err(err)
|
return Err(err);
|
||||||
|
}
|
||||||
|
assignment
|
||||||
|
.expression()
|
||||||
|
.analyze_semantics(function_names, macro_names, handler)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression::Primary(Primary::Boolean(bool)) => {
|
Self::Score(score) => {
|
||||||
if self.variable_type().keyword == KeywordKind::Int {
|
if let Some((_, assignment)) = score.target_assignment() {
|
||||||
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
// TODO: also allow macro identifier but not macro string literal
|
||||||
Expression::Primary(Primary::Boolean(bool.clone())),
|
if !matches!(
|
||||||
));
|
assignment.expression(),
|
||||||
handler.receive(err.clone());
|
Expression::Primary(
|
||||||
Err(err)
|
Primary::Integer(_) | Primary::Lua(_) | Primary::FunctionCall(_)
|
||||||
} else {
|
)
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Expression::Primary(Primary::StringLiteral(str)) => {
|
|
||||||
if matches!(
|
|
||||||
self.variable_type().keyword,
|
|
||||||
KeywordKind::Int | KeywordKind::Bool
|
|
||||||
) {
|
) {
|
||||||
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
||||||
Expression::Primary(Primary::StringLiteral(str.clone())),
|
assignment.expression().clone(),
|
||||||
));
|
));
|
||||||
handler.receive(err.clone());
|
handler.receive(err.clone());
|
||||||
Err(err)
|
return Err(err);
|
||||||
|
}
|
||||||
|
assignment
|
||||||
|
.expression()
|
||||||
|
.analyze_semantics(function_names, macro_names, handler)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression::Primary(Primary::MacroStringLiteral(str)) => {
|
Self::Tag(tag) => {
|
||||||
if matches!(
|
if let Some((_, assignment)) = tag.target_assignment() {
|
||||||
self.variable_type().keyword,
|
if !matches!(
|
||||||
KeywordKind::Int | KeywordKind::Bool
|
assignment.expression(),
|
||||||
|
Expression::Primary(Primary::Boolean(_) | Primary::Lua(_))
|
||||||
) {
|
) {
|
||||||
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
let err = error::Error::UnexpectedExpression(UnexpectedExpression(
|
||||||
Expression::Primary(Primary::MacroStringLiteral(str.clone())),
|
assignment.expression().clone(),
|
||||||
));
|
));
|
||||||
handler.receive(err.clone());
|
handler.receive(err.clone());
|
||||||
Err(err)
|
return Err(err);
|
||||||
|
}
|
||||||
|
assignment
|
||||||
|
.expression()
|
||||||
|
.analyze_semantics(function_names, macro_names, handler)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression::Primary(_) => Ok(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
52
src/serde.rs
52
src/serde.rs
|
@ -14,6 +14,7 @@ use serde::{
|
||||||
|
|
||||||
use crate::base::source_file::SourceFile;
|
use crate::base::source_file::SourceFile;
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
static DEDUPLICATE_SOURCE_FILES: LazyLock<RwLock<bool>> = LazyLock::new(|| RwLock::new(false));
|
static DEDUPLICATE_SOURCE_FILES: LazyLock<RwLock<bool>> = LazyLock::new(|| RwLock::new(false));
|
||||||
|
|
||||||
static SERIALIZE_DATA: LazyLock<Mutex<SerializeData>> =
|
static SERIALIZE_DATA: LazyLock<Mutex<SerializeData>> =
|
||||||
|
@ -21,6 +22,7 @@ static SERIALIZE_DATA: LazyLock<Mutex<SerializeData>> =
|
||||||
|
|
||||||
static DESERIALIZE_DATA: LazyLock<RwLock<Option<DeserializeData>>> =
|
static DESERIALIZE_DATA: LazyLock<RwLock<Option<DeserializeData>>> =
|
||||||
LazyLock::new(|| RwLock::new(None));
|
LazyLock::new(|| RwLock::new(None));
|
||||||
|
}
|
||||||
|
|
||||||
/// Wrapper to remove duplicate source file data during (de-)serialization
|
/// Wrapper to remove duplicate source file data during (de-)serialization
|
||||||
#[expect(clippy::module_name_repetitions)]
|
#[expect(clippy::module_name_repetitions)]
|
||||||
|
@ -35,20 +37,27 @@ where
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
*DEDUPLICATE_SOURCE_FILES.write().unwrap() = true;
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = true);
|
||||||
SERIALIZE_DATA.lock().unwrap().clear();
|
SERIALIZE_DATA.with(|d| d.lock().unwrap().clear());
|
||||||
|
// hold guard so no other can serialize at the same time in same thread
|
||||||
|
let s = DEDUPLICATE_SOURCE_FILES.with(|d| {
|
||||||
|
let guard = d.read().unwrap();
|
||||||
let mut serialized_data = flexbuffers::FlexbufferSerializer::new();
|
let mut serialized_data = flexbuffers::FlexbufferSerializer::new();
|
||||||
self.0
|
self.0
|
||||||
.serialize(&mut serialized_data)
|
.serialize(&mut serialized_data)
|
||||||
.map_err(|_| serde::ser::Error::custom("could not buffer serialization"))?;
|
.map_err(|_| serde::ser::Error::custom("could not buffer serialization"))?;
|
||||||
drop(serialized_data);
|
drop(serialized_data);
|
||||||
let mut s = serializer.serialize_struct("SerdeWrapper", 3)?;
|
let mut s = serializer.serialize_struct("SerdeWrapper", 3)?;
|
||||||
s.serialize_field(
|
|
||||||
"source_files",
|
SERIALIZE_DATA.with(|d| {
|
||||||
&SERIALIZE_DATA.lock().unwrap().id_to_source_file,
|
s.serialize_field("source_files", &d.lock().unwrap().id_to_source_file)
|
||||||
)?;
|
})?;
|
||||||
s.serialize_field("data", &self.0)?;
|
s.serialize_field("data", &self.0)?;
|
||||||
*DEDUPLICATE_SOURCE_FILES.write().unwrap() = false;
|
drop(guard);
|
||||||
|
Ok(s)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = false);
|
||||||
s.end()
|
s.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,11 +96,13 @@ where
|
||||||
let source_files: BTreeMap<u64, SourceFile> = seq
|
let source_files: BTreeMap<u64, SourceFile> = seq
|
||||||
.next_element()?
|
.next_element()?
|
||||||
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
|
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
|
||||||
*DESERIALIZE_DATA.write().unwrap() = Some(DeserializeData {
|
DESERIALIZE_DATA.with(|d| {
|
||||||
|
*d.write().unwrap() = Some(DeserializeData {
|
||||||
id_to_source_file: source_files
|
id_to_source_file: source_files
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(k, v)| (k, Arc::new(v)))
|
.map(|(k, v)| (k, Arc::new(v)))
|
||||||
.collect(),
|
.collect(),
|
||||||
|
})
|
||||||
});
|
});
|
||||||
let data = seq
|
let data = seq
|
||||||
.next_element()?
|
.next_element()?
|
||||||
|
@ -113,12 +124,14 @@ where
|
||||||
if data.is_some() {
|
if data.is_some() {
|
||||||
return Err(de::Error::duplicate_field("data"));
|
return Err(de::Error::duplicate_field("data"));
|
||||||
}
|
}
|
||||||
*DESERIALIZE_DATA.write().unwrap() =
|
DESERIALIZE_DATA.with(|d| {
|
||||||
|
*d.write().unwrap() =
|
||||||
source_files.as_ref().map(|source_files| DeserializeData {
|
source_files.as_ref().map(|source_files| DeserializeData {
|
||||||
id_to_source_file: source_files
|
id_to_source_file: source_files
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(&k, v)| (k, Arc::new(v.clone())))
|
.map(|(&k, v)| (k, Arc::new(v.clone())))
|
||||||
.collect(),
|
.collect(),
|
||||||
|
})
|
||||||
});
|
});
|
||||||
data = Some(map.next_value()?);
|
data = Some(map.next_value()?);
|
||||||
}
|
}
|
||||||
|
@ -136,14 +149,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*DEDUPLICATE_SOURCE_FILES.write().unwrap() = true;
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = true);
|
||||||
*DESERIALIZE_DATA.write().unwrap() = None;
|
DESERIALIZE_DATA.with(|d| *d.write().unwrap() = None);
|
||||||
let res = deserializer.deserialize_struct(
|
let res = deserializer.deserialize_struct(
|
||||||
"SerdeWrapper",
|
"SerdeWrapper",
|
||||||
&["source_files", "data"],
|
&["source_files", "data"],
|
||||||
WrapperVisitor(PhantomData::<T>),
|
WrapperVisitor(PhantomData::<T>),
|
||||||
);
|
);
|
||||||
*DEDUPLICATE_SOURCE_FILES.write().unwrap() = false;
|
DEDUPLICATE_SOURCE_FILES.with(|d| *d.write().unwrap() = false);
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -200,9 +213,11 @@ pub mod source_file {
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
if *DEDUPLICATE_SOURCE_FILES.read().unwrap() {
|
if DEDUPLICATE_SOURCE_FILES.with(|d| *d.read().unwrap()) {
|
||||||
let mut data = SERIALIZE_DATA.lock().unwrap();
|
SERIALIZE_DATA.with(|d| {
|
||||||
|
let mut data = d.lock().unwrap();
|
||||||
serializer.serialize_u64(data.get_id_of(this))
|
serializer.serialize_u64(data.get_id_of(this))
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
this.as_ref().serialize(serializer)
|
this.as_ref().serialize(serializer)
|
||||||
}
|
}
|
||||||
|
@ -212,17 +227,18 @@ pub mod source_file {
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
if *DEDUPLICATE_SOURCE_FILES.read().unwrap() {
|
if DEDUPLICATE_SOURCE_FILES.with(|d| *d.read().unwrap()) {
|
||||||
let id = u64::deserialize(deserializer)?;
|
let id = u64::deserialize(deserializer)?;
|
||||||
Ok(DESERIALIZE_DATA
|
Ok(DESERIALIZE_DATA.with(|d| {
|
||||||
.read()
|
d.read()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| de::Error::custom("SourceFiles do not have been loaded yet"))?
|
.ok_or_else(|| de::Error::custom("SourceFiles do not have been loaded yet"))?
|
||||||
.id_to_source_file
|
.id_to_source_file
|
||||||
.get(&id)
|
.get(&id)
|
||||||
.map(Arc::clone)
|
.map(Arc::clone)
|
||||||
.ok_or_else(|| serde::de::Error::custom("invalid source_file id"))?)
|
.ok_or_else(|| serde::de::Error::custom("invalid source_file id"))
|
||||||
|
}))?
|
||||||
} else {
|
} else {
|
||||||
Ok(Arc::new(SourceFile::deserialize(deserializer)?))
|
Ok(Arc::new(SourceFile::deserialize(deserializer)?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,20 @@
|
||||||
pub mod execute_block;
|
pub mod execute_block;
|
||||||
|
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
|
use enum_as_inner::EnumAsInner;
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
base::{
|
base::{
|
||||||
self,
|
self,
|
||||||
source_file::{SourceElement, Span},
|
source_file::{SourceElement, Span},
|
||||||
Handler,
|
Handler, VoidHandler,
|
||||||
},
|
},
|
||||||
lexical::{
|
lexical::{
|
||||||
token::{CommandLiteral, DocComment, Identifier, Keyword, KeywordKind, Punctuation, Token},
|
token::{
|
||||||
|
CommandLiteral, DocComment, Identifier, Integer, Keyword, KeywordKind, Punctuation,
|
||||||
|
Token,
|
||||||
|
},
|
||||||
token_stream::Delimiter,
|
token_stream::Delimiter,
|
||||||
},
|
},
|
||||||
syntax::{
|
syntax::{
|
||||||
|
@ -23,7 +27,7 @@ use crate::{
|
||||||
|
|
||||||
use self::execute_block::ExecuteBlock;
|
use self::execute_block::ExecuteBlock;
|
||||||
|
|
||||||
use super::expression::Expression;
|
use super::{expression::Expression, AnyStringLiteral};
|
||||||
|
|
||||||
/// Represents a statement in the syntax tree.
|
/// Represents a statement in the syntax tree.
|
||||||
///
|
///
|
||||||
|
@ -230,7 +234,7 @@ impl Semicolon {
|
||||||
/// ;
|
/// ;
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From, EnumAsInner)]
|
||||||
pub enum SemicolonStatement {
|
pub enum SemicolonStatement {
|
||||||
/// An expression that ends with a semicolon.
|
/// An expression that ends with a semicolon.
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
|
@ -252,18 +256,69 @@ impl SourceElement for SemicolonStatement {
|
||||||
/// Syntax Synopsis:
|
/// Syntax Synopsis:
|
||||||
///
|
///
|
||||||
/// ```ebnf
|
/// ```ebnf
|
||||||
/// LuaCode:
|
/// VariableDeclaration:
|
||||||
/// ('int' | 'bool') identifier '=' Expression ';'
|
/// SingleVariableDeclaration
|
||||||
|
/// | ArrayVariableDeclaration
|
||||||
|
/// | ScoreVariableDeclaration
|
||||||
|
/// | TagVariableDeclaration
|
||||||
|
/// ;
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, From, EnumAsInner)]
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
pub enum VariableDeclaration {
|
||||||
|
Single(SingleVariableDeclaration),
|
||||||
|
Array(ArrayVariableDeclaration),
|
||||||
|
Score(ScoreVariableDeclaration),
|
||||||
|
Tag(TagVariableDeclaration),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for VariableDeclaration {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
match self {
|
||||||
|
Self::Single(declaration) => declaration.span(),
|
||||||
|
Self::Array(declaration) => declaration.span(),
|
||||||
|
Self::Score(declaration) => declaration.span(),
|
||||||
|
Self::Tag(declaration) => declaration.span(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VariableDeclaration {
|
||||||
|
/// Get the identifier of the variable declaration
|
||||||
|
#[must_use]
|
||||||
|
pub fn identifier(&self) -> &Identifier {
|
||||||
|
match self {
|
||||||
|
Self::Single(declaration) => &declaration.identifier,
|
||||||
|
Self::Array(declaration) => &declaration.identifier,
|
||||||
|
Self::Score(declaration) => &declaration.identifier,
|
||||||
|
Self::Tag(declaration) => &declaration.identifier,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the type of the variable declaration
|
||||||
|
#[must_use]
|
||||||
|
pub fn variable_type(&self) -> &Keyword {
|
||||||
|
match self {
|
||||||
|
Self::Single(declaration) => &declaration.variable_type,
|
||||||
|
Self::Array(declaration) => &declaration.variable_type,
|
||||||
|
Self::Score(declaration) => &declaration.int_keyword,
|
||||||
|
Self::Tag(declaration) => &declaration.bool_keyword,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a variable assignment.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// VariableDeclarationAssignment:
|
||||||
|
/// '=' Expression
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
pub struct VariableDeclaration {
|
pub struct VariableDeclarationAssignment {
|
||||||
/// The type of the variable.
|
|
||||||
#[get = "pub"]
|
|
||||||
variable_type: Keyword,
|
|
||||||
/// The identifier of the variable.
|
|
||||||
#[get = "pub"]
|
|
||||||
identifier: Identifier,
|
|
||||||
/// The equals sign of the variable declaration.
|
/// The equals sign of the variable declaration.
|
||||||
#[get = "pub"]
|
#[get = "pub"]
|
||||||
equals: Punctuation,
|
equals: Punctuation,
|
||||||
|
@ -272,12 +327,265 @@ pub struct VariableDeclaration {
|
||||||
expression: Expression,
|
expression: Expression,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceElement for VariableDeclaration {
|
impl SourceElement for VariableDeclarationAssignment {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.equals
|
||||||
|
.span()
|
||||||
|
.join(&self.expression.span())
|
||||||
|
.expect("The span of the variable declaration assignment is invalid.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VariableDeclarationAssignment {
|
||||||
|
/// Dissolves the [`VariableDeclarationAssignment`] into its components.
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(self) -> (Punctuation, Expression) {
|
||||||
|
(self.equals, self.expression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a single variable declaration in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// SingleVariableDeclaration:
|
||||||
|
/// ('int' | 'bool') identifier VariableDeclarationAssignment?
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct SingleVariableDeclaration {
|
||||||
|
/// The type of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
variable_type: Keyword,
|
||||||
|
/// The identifier of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
identifier: Identifier,
|
||||||
|
/// The optional assignment of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
assignment: Option<VariableDeclarationAssignment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for SingleVariableDeclaration {
|
||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
self.variable_type
|
self.variable_type
|
||||||
.span()
|
.span()
|
||||||
.join(&self.expression.span())
|
.join(
|
||||||
.expect("The span of the variable declaration is invalid.")
|
&self
|
||||||
|
.assignment
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| self.identifier.span(), SourceElement::span),
|
||||||
|
)
|
||||||
|
.expect("The span of the single variable declaration is invalid.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SingleVariableDeclaration {
|
||||||
|
/// Dissolves the [`SingleVariableDeclaration`] into its components.
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(self) -> (Keyword, Identifier, Option<VariableDeclarationAssignment>) {
|
||||||
|
(self.variable_type, self.identifier, self.assignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents an array variable declaration in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// ArrayVariableDeclaration:
|
||||||
|
/// ('int' | 'bool') identifier '[' integer ']' VariableDeclarationAssignment?
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct ArrayVariableDeclaration {
|
||||||
|
/// The type of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
variable_type: Keyword,
|
||||||
|
/// The identifier of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
identifier: Identifier,
|
||||||
|
/// The opening bracket of the array.
|
||||||
|
#[get = "pub"]
|
||||||
|
open_bracket: Punctuation,
|
||||||
|
/// The array size
|
||||||
|
#[get = "pub"]
|
||||||
|
size: Integer,
|
||||||
|
/// The closing bracket of the array.
|
||||||
|
#[get = "pub"]
|
||||||
|
close_bracket: Punctuation,
|
||||||
|
/// The optional assignment of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
assignment: Option<VariableDeclarationAssignment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for ArrayVariableDeclaration {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.variable_type
|
||||||
|
.span()
|
||||||
|
.join(
|
||||||
|
&self
|
||||||
|
.assignment
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| self.close_bracket.span(), SourceElement::span),
|
||||||
|
)
|
||||||
|
.expect("The span of the array variable declaration is invalid.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ArrayVariableDeclaration {
|
||||||
|
/// Dissolves the [`ArrayVariableDeclaration`] into its components.
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(
|
||||||
|
self,
|
||||||
|
) -> (
|
||||||
|
Keyword,
|
||||||
|
Identifier,
|
||||||
|
Punctuation,
|
||||||
|
Integer,
|
||||||
|
Punctuation,
|
||||||
|
Option<VariableDeclarationAssignment>,
|
||||||
|
) {
|
||||||
|
(
|
||||||
|
self.variable_type,
|
||||||
|
self.identifier,
|
||||||
|
self.open_bracket,
|
||||||
|
self.size,
|
||||||
|
self.close_bracket,
|
||||||
|
self.assignment,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CriteriaSelection = (Punctuation, AnyStringLiteral, Punctuation);
|
||||||
|
|
||||||
|
/// Represents a scoreboard variable declaration in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// ScoreVariableDeclaration:
|
||||||
|
/// 'int' ('<' AnyStringLiteral '>')? identifier '[' AnyStringLiteral? ']' VariableDeclarationAssignment?
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct ScoreVariableDeclaration {
|
||||||
|
/// `int` keyword
|
||||||
|
#[get = "pub"]
|
||||||
|
int_keyword: Keyword,
|
||||||
|
/// The scoreboard criteria
|
||||||
|
#[get = "pub"]
|
||||||
|
criteria: Option<CriteriaSelection>,
|
||||||
|
/// The identifier of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
identifier: Identifier,
|
||||||
|
/// Opening bracket of the score variable
|
||||||
|
#[get = "pub"]
|
||||||
|
open_bracket: Punctuation,
|
||||||
|
/// Closing bracket of the score variable
|
||||||
|
#[get = "pub"]
|
||||||
|
close_bracket: Punctuation,
|
||||||
|
/// The optional assignment of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
target_assignment: Option<(AnyStringLiteral, VariableDeclarationAssignment)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for ScoreVariableDeclaration {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.int_keyword
|
||||||
|
.span()
|
||||||
|
.join(&self.target_assignment.as_ref().map_or_else(
|
||||||
|
|| self.close_bracket.span(),
|
||||||
|
|(_, assignment)| assignment.span(),
|
||||||
|
))
|
||||||
|
.expect("The span of the score variable declaration is invalid.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ScoreVariableDeclaration {
|
||||||
|
/// Dissolves the [`ScoreVariableDeclaration`] into its components.
|
||||||
|
#[expect(clippy::type_complexity)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(
|
||||||
|
self,
|
||||||
|
) -> (
|
||||||
|
Keyword,
|
||||||
|
Option<CriteriaSelection>,
|
||||||
|
Identifier,
|
||||||
|
Punctuation,
|
||||||
|
Punctuation,
|
||||||
|
Option<(AnyStringLiteral, VariableDeclarationAssignment)>,
|
||||||
|
) {
|
||||||
|
(
|
||||||
|
self.int_keyword,
|
||||||
|
self.criteria,
|
||||||
|
self.identifier,
|
||||||
|
self.open_bracket,
|
||||||
|
self.close_bracket,
|
||||||
|
self.target_assignment,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents a tag variable declaration in the syntax tree.
|
||||||
|
///
|
||||||
|
/// Syntax Synopsis:
|
||||||
|
///
|
||||||
|
/// ```ebnf
|
||||||
|
/// TagVariableDeclaration:
|
||||||
|
/// 'bool' identifier '[' AnyStringLiteral? ']' VariableDeclarationAssignment?
|
||||||
|
/// ```
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
|
||||||
|
pub struct TagVariableDeclaration {
|
||||||
|
/// `bool` keyword
|
||||||
|
#[get = "pub"]
|
||||||
|
bool_keyword: Keyword,
|
||||||
|
/// The identifier of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
identifier: Identifier,
|
||||||
|
/// Opening bracket of the score variable
|
||||||
|
#[get = "pub"]
|
||||||
|
open_bracket: Punctuation,
|
||||||
|
/// Closing bracket of the score variable
|
||||||
|
#[get = "pub"]
|
||||||
|
close_bracket: Punctuation,
|
||||||
|
/// The optional assignment of the variable.
|
||||||
|
#[get = "pub"]
|
||||||
|
target_assignment: Option<(AnyStringLiteral, VariableDeclarationAssignment)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceElement for TagVariableDeclaration {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.bool_keyword
|
||||||
|
.span()
|
||||||
|
.join(&self.target_assignment.as_ref().map_or_else(
|
||||||
|
|| self.close_bracket.span(),
|
||||||
|
|(_, assignment)| assignment.span(),
|
||||||
|
))
|
||||||
|
.expect("The span of the tag variable declaration is invalid.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TagVariableDeclaration {
|
||||||
|
/// Dissolves the [`TagVariableDeclaration`] into its components.
|
||||||
|
#[must_use]
|
||||||
|
pub fn dissolve(
|
||||||
|
self,
|
||||||
|
) -> (
|
||||||
|
Keyword,
|
||||||
|
Identifier,
|
||||||
|
Punctuation,
|
||||||
|
Punctuation,
|
||||||
|
Option<(AnyStringLiteral, VariableDeclarationAssignment)>,
|
||||||
|
) {
|
||||||
|
(
|
||||||
|
self.bool_keyword,
|
||||||
|
self.identifier,
|
||||||
|
self.open_bracket,
|
||||||
|
self.close_bracket,
|
||||||
|
self.target_assignment,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,6 +738,12 @@ impl<'a> Parser<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> ParseResult<VariableDeclaration> {
|
) -> ParseResult<VariableDeclaration> {
|
||||||
|
enum IndexingType {
|
||||||
|
IntegerSize(Integer),
|
||||||
|
AnyString(AnyStringLiteral),
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
let variable_type = match self.stop_at_significant() {
|
let variable_type = match self.stop_at_significant() {
|
||||||
Reading::Atomic(Token::Keyword(keyword))
|
Reading::Atomic(Token::Keyword(keyword))
|
||||||
if matches!(keyword.keyword, KeywordKind::Int | KeywordKind::Bool) =>
|
if matches!(keyword.keyword, KeywordKind::Int | KeywordKind::Bool) =>
|
||||||
|
@ -450,21 +764,163 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let criteria_selection = if variable_type.keyword == KeywordKind::Int {
|
||||||
|
self.try_parse(|p| {
|
||||||
|
let open = p.parse_punctuation('<', true, &VoidHandler)?;
|
||||||
|
let criteria = p.parse_any_string_literal(&VoidHandler)?;
|
||||||
|
let close = p.parse_punctuation('>', true, &VoidHandler)?;
|
||||||
|
Ok((open, criteria, close))
|
||||||
|
})
|
||||||
|
.ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
// read identifier
|
// read identifier
|
||||||
self.stop_at_significant();
|
self.stop_at_significant();
|
||||||
let identifier = self.parse_identifier(handler)?;
|
let identifier = self.parse_identifier(handler)?;
|
||||||
|
|
||||||
|
match self.stop_at_significant() {
|
||||||
|
Reading::IntoDelimited(punc) if punc.punctuation == '[' => {
|
||||||
|
let tree = self.step_into(
|
||||||
|
Delimiter::Bracket,
|
||||||
|
|p| {
|
||||||
|
let res = match p.stop_at_significant() {
|
||||||
|
Reading::Atomic(Token::Integer(int)) => {
|
||||||
|
p.forward();
|
||||||
|
IndexingType::IntegerSize(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
Reading::Atomic(Token::StringLiteral(s)) => {
|
||||||
|
let selector = AnyStringLiteral::from(s);
|
||||||
|
p.forward();
|
||||||
|
IndexingType::AnyString(selector)
|
||||||
|
}
|
||||||
|
Reading::Atomic(Token::MacroStringLiteral(s)) => {
|
||||||
|
let selector = AnyStringLiteral::from(s);
|
||||||
|
p.forward();
|
||||||
|
IndexingType::AnyString(selector)
|
||||||
|
}
|
||||||
|
|
||||||
|
Reading::DelimitedEnd(punc) if punc.punctuation == ']' => {
|
||||||
|
IndexingType::None
|
||||||
|
}
|
||||||
|
|
||||||
|
unexpected => {
|
||||||
|
let err = Error::UnexpectedSyntax(UnexpectedSyntax {
|
||||||
|
expected: SyntaxKind::Either(&[
|
||||||
|
SyntaxKind::Integer,
|
||||||
|
SyntaxKind::AnyStringLiteral,
|
||||||
|
]),
|
||||||
|
found: unexpected.into_token(),
|
||||||
|
});
|
||||||
|
handler.receive(err.clone());
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
},
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let open_bracket = tree.open;
|
||||||
|
let close_bracket = tree.close;
|
||||||
|
let inner = tree.tree?;
|
||||||
|
|
||||||
|
match inner {
|
||||||
|
IndexingType::IntegerSize(size) => {
|
||||||
|
let assignment = self
|
||||||
|
.try_parse(|p| {
|
||||||
// read equals sign
|
// read equals sign
|
||||||
let equals = self.parse_punctuation('=', true, handler)?;
|
let equals = p.parse_punctuation('=', true, handler)?;
|
||||||
|
|
||||||
// read expression
|
// read expression
|
||||||
let expression = self.parse_expression(handler)?;
|
let expression = p.parse_expression(handler)?;
|
||||||
|
|
||||||
Ok(VariableDeclaration {
|
Ok(VariableDeclarationAssignment { equals, expression })
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
Ok(VariableDeclaration::Array(ArrayVariableDeclaration {
|
||||||
variable_type,
|
variable_type,
|
||||||
identifier,
|
identifier,
|
||||||
equals,
|
open_bracket,
|
||||||
expression,
|
size,
|
||||||
})
|
close_bracket,
|
||||||
|
assignment,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
IndexingType::AnyString(selector) => {
|
||||||
|
let equals = self.parse_punctuation('=', true, handler)?;
|
||||||
|
let expression = self.parse_expression(handler)?;
|
||||||
|
|
||||||
|
let assignment = VariableDeclarationAssignment { equals, expression };
|
||||||
|
|
||||||
|
match variable_type.keyword {
|
||||||
|
KeywordKind::Int => {
|
||||||
|
Ok(VariableDeclaration::Score(ScoreVariableDeclaration {
|
||||||
|
int_keyword: variable_type,
|
||||||
|
criteria: criteria_selection,
|
||||||
|
identifier,
|
||||||
|
open_bracket,
|
||||||
|
close_bracket,
|
||||||
|
target_assignment: Some((selector, assignment)),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
KeywordKind::Bool => {
|
||||||
|
Ok(VariableDeclaration::Tag(TagVariableDeclaration {
|
||||||
|
bool_keyword: variable_type,
|
||||||
|
identifier,
|
||||||
|
open_bracket,
|
||||||
|
close_bracket,
|
||||||
|
target_assignment: Some((selector, assignment)),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IndexingType::None => match variable_type.keyword {
|
||||||
|
KeywordKind::Int => {
|
||||||
|
Ok(VariableDeclaration::Score(ScoreVariableDeclaration {
|
||||||
|
int_keyword: variable_type,
|
||||||
|
criteria: criteria_selection,
|
||||||
|
identifier,
|
||||||
|
open_bracket,
|
||||||
|
close_bracket,
|
||||||
|
target_assignment: None,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
KeywordKind::Bool => Ok(VariableDeclaration::Tag(TagVariableDeclaration {
|
||||||
|
bool_keyword: variable_type,
|
||||||
|
identifier,
|
||||||
|
open_bracket,
|
||||||
|
close_bracket,
|
||||||
|
target_assignment: None,
|
||||||
|
})),
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// SingleVariableDeclaration with Assignment
|
||||||
|
Reading::Atomic(Token::Punctuation(punc)) if punc.punctuation == '=' => {
|
||||||
|
self.forward();
|
||||||
|
let equals = punc;
|
||||||
|
let expression = self.parse_expression(handler)?;
|
||||||
|
let assignment = VariableDeclarationAssignment { equals, expression };
|
||||||
|
|
||||||
|
Ok(VariableDeclaration::Single(SingleVariableDeclaration {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
assignment: Some(assignment),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
// SingleVariableDeclaration without Assignment
|
||||||
|
_ => Ok(VariableDeclaration::Single(SingleVariableDeclaration {
|
||||||
|
variable_type,
|
||||||
|
identifier,
|
||||||
|
assignment: None,
|
||||||
|
})),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ mod transpiler;
|
||||||
#[cfg(feature = "shulkerbox")]
|
#[cfg(feature = "shulkerbox")]
|
||||||
#[cfg_attr(feature = "shulkerbox", doc(inline))]
|
#[cfg_attr(feature = "shulkerbox", doc(inline))]
|
||||||
pub use transpiler::Transpiler;
|
pub use transpiler::Transpiler;
|
||||||
|
#[cfg(feature = "shulkerbox")]
|
||||||
|
mod variables;
|
||||||
|
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
program::{Namespace, ProgramFile},
|
program::{Namespace, ProgramFile},
|
||||||
statement::{
|
statement::{
|
||||||
execute_block::{Conditional, Else, ExecuteBlock, ExecuteBlockHead, ExecuteBlockTail},
|
execute_block::{Conditional, Else, ExecuteBlock, ExecuteBlockHead, ExecuteBlockTail},
|
||||||
Statement,
|
SemicolonStatement, Statement,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
transpile::error::MissingFunctionDeclaration,
|
transpile::error::MissingFunctionDeclaration,
|
||||||
|
@ -29,6 +29,7 @@ use crate::{
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error::{TranspileError, TranspileResult},
|
error::{TranspileError, TranspileResult},
|
||||||
|
variables::{Scope, VariableType},
|
||||||
FunctionData,
|
FunctionData,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -74,8 +75,10 @@ impl Transpiler {
|
||||||
) -> Result<(), TranspileError> {
|
) -> Result<(), TranspileError> {
|
||||||
tracing::trace!("Transpiling program declarations");
|
tracing::trace!("Transpiling program declarations");
|
||||||
|
|
||||||
|
let scope = Scope::new();
|
||||||
|
|
||||||
for program in programs {
|
for program in programs {
|
||||||
self.transpile_program_declarations(program, handler);
|
self.transpile_program_declarations(program, &scope, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut always_transpile_functions = Vec::new();
|
let mut always_transpile_functions = Vec::new();
|
||||||
|
@ -98,7 +101,7 @@ impl Transpiler {
|
||||||
);
|
);
|
||||||
|
|
||||||
for identifier_span in always_transpile_functions {
|
for identifier_span in always_transpile_functions {
|
||||||
self.get_or_transpile_function(&identifier_span, None, handler)?;
|
self.get_or_transpile_function(&identifier_span, None, &scope, handler)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -108,12 +111,13 @@ impl Transpiler {
|
||||||
fn transpile_program_declarations(
|
fn transpile_program_declarations(
|
||||||
&mut self,
|
&mut self,
|
||||||
program: &ProgramFile,
|
program: &ProgramFile,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) {
|
) {
|
||||||
let namespace = program.namespace();
|
let namespace = program.namespace();
|
||||||
|
|
||||||
for declaration in program.declarations() {
|
for declaration in program.declarations() {
|
||||||
self.transpile_declaration(declaration, namespace, handler);
|
self.transpile_declaration(declaration, namespace, scope, handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +127,7 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
declaration: &Declaration,
|
declaration: &Declaration,
|
||||||
namespace: &Namespace,
|
namespace: &Namespace,
|
||||||
|
_scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) {
|
) {
|
||||||
let program_identifier = declaration.span().source_file().identifier().clone();
|
let program_identifier = declaration.span().source_file().identifier().clone();
|
||||||
|
@ -143,7 +148,6 @@ impl Transpiler {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
#[allow(clippy::significant_drop_tightening)]
|
|
||||||
self.functions.insert(
|
self.functions.insert(
|
||||||
(program_identifier, name),
|
(program_identifier, name),
|
||||||
FunctionData {
|
FunctionData {
|
||||||
|
@ -203,6 +207,7 @@ impl Transpiler {
|
||||||
if tag.replace().is_some() {
|
if tag.replace().is_some() {
|
||||||
sb_tag.set_replace(true);
|
sb_tag.set_replace(true);
|
||||||
}
|
}
|
||||||
|
// TODO: handle global variables
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -215,6 +220,7 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
identifier_span: &Span,
|
identifier_span: &Span,
|
||||||
arguments: Option<&[&Expression]>,
|
arguments: Option<&[&Expression]>,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<(String, Option<BTreeMap<String, String>>)> {
|
) -> TranspileResult<(String, Option<BTreeMap<String, String>>)> {
|
||||||
let program_identifier = identifier_span.source_file().identifier();
|
let program_identifier = identifier_span.source_file().identifier();
|
||||||
|
@ -237,6 +243,8 @@ impl Transpiler {
|
||||||
if !already_transpiled {
|
if !already_transpiled {
|
||||||
tracing::trace!("Function not transpiled yet, transpiling.");
|
tracing::trace!("Function not transpiled yet, transpiling.");
|
||||||
|
|
||||||
|
let function_scope = scope.new_child();
|
||||||
|
|
||||||
let statements = {
|
let statements = {
|
||||||
let functions = &self.functions;
|
let functions = &self.functions;
|
||||||
let function_data = functions
|
let function_data = functions
|
||||||
|
@ -257,9 +265,15 @@ impl Transpiler {
|
||||||
error
|
error
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
for (i, param) in function_data.parameters.iter().enumerate() {
|
||||||
|
function_scope.set_variable(param, VariableType::FunctionArgument { index: i });
|
||||||
|
}
|
||||||
|
|
||||||
function_data.statements.clone()
|
function_data.statements.clone()
|
||||||
};
|
};
|
||||||
let commands = self.transpile_function(&statements, program_identifier, handler)?;
|
|
||||||
|
let commands =
|
||||||
|
self.transpile_function(&statements, program_identifier, &function_scope, handler)?;
|
||||||
|
|
||||||
let functions = &self.functions;
|
let functions = &self.functions;
|
||||||
let function_data = functions
|
let function_data = functions
|
||||||
|
@ -369,22 +383,22 @@ impl Transpiler {
|
||||||
})
|
})
|
||||||
.map(|(s, _)| s.to_owned())?;
|
.map(|(s, _)| s.to_owned())?;
|
||||||
|
|
||||||
let arg_count = arguments.iter().flat_map(|x| x.iter()).count();
|
let arg_count = arguments.map(<[&Expression]>::len);
|
||||||
if arg_count != parameters.len() {
|
if arg_count.is_some_and(|arg_count| arg_count != parameters.len()) {
|
||||||
let err = TranspileError::InvalidFunctionArguments(InvalidFunctionArguments {
|
let err = TranspileError::InvalidFunctionArguments(InvalidFunctionArguments {
|
||||||
expected: parameters.len(),
|
expected: parameters.len(),
|
||||||
actual: arg_count,
|
actual: arg_count.expect("checked in if condition"),
|
||||||
span: identifier_span.clone(),
|
span: identifier_span.clone(),
|
||||||
});
|
});
|
||||||
handler.receive(err.clone());
|
handler.receive(err.clone());
|
||||||
Err(err)
|
Err(err)
|
||||||
} else if arg_count > 0 {
|
} else if arg_count.is_some_and(|arg_count| arg_count > 0) {
|
||||||
let mut compiled_args = Vec::new();
|
let mut compiled_args = Vec::new();
|
||||||
let mut errs = Vec::new();
|
let mut errs = Vec::new();
|
||||||
for expression in arguments.iter().flat_map(|x| x.iter()) {
|
for expression in arguments.iter().flat_map(|x| x.iter()) {
|
||||||
let value = match expression {
|
let value = match expression {
|
||||||
Expression::Primary(Primary::FunctionCall(func)) => self
|
Expression::Primary(Primary::FunctionCall(func)) => self
|
||||||
.transpile_function_call(func, handler)
|
.transpile_function_call(func, scope, handler)
|
||||||
.map(|cmd| match cmd {
|
.map(|cmd| match cmd {
|
||||||
Command::Raw(s) => s,
|
Command::Raw(s) => s,
|
||||||
_ => unreachable!("Function call should always return a raw command"),
|
_ => unreachable!("Function call should always return a raw command"),
|
||||||
|
@ -426,13 +440,14 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
statements: &[Statement],
|
statements: &[Statement],
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Vec<Command>> {
|
) -> TranspileResult<Vec<Command>> {
|
||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| {
|
.filter_map(|statement| {
|
||||||
self.transpile_statement(statement, program_identifier, handler)
|
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -451,6 +466,7 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
statement: &Statement,
|
statement: &Statement,
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Option<Command>> {
|
) -> TranspileResult<Option<Command>> {
|
||||||
match statement {
|
match statement {
|
||||||
|
@ -459,7 +475,7 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
Statement::Run(run) => match run.expression() {
|
Statement::Run(run) => match run.expression() {
|
||||||
Expression::Primary(Primary::FunctionCall(func)) => {
|
Expression::Primary(Primary::FunctionCall(func)) => {
|
||||||
self.transpile_function_call(func, handler).map(Some)
|
self.transpile_function_call(func, scope, handler).map(Some)
|
||||||
}
|
}
|
||||||
Expression::Primary(Primary::Integer(num)) => {
|
Expression::Primary(Primary::Integer(num)) => {
|
||||||
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
|
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
|
||||||
|
@ -489,19 +505,26 @@ impl Transpiler {
|
||||||
unreachable!("Only literal commands are allowed in functions at this time.")
|
unreachable!("Only literal commands are allowed in functions at this time.")
|
||||||
}
|
}
|
||||||
Statement::ExecuteBlock(execute) => {
|
Statement::ExecuteBlock(execute) => {
|
||||||
self.transpile_execute_block(execute, program_identifier, handler)
|
let child_scope = scope.new_child();
|
||||||
|
self.transpile_execute_block(execute, program_identifier, &child_scope, handler)
|
||||||
}
|
}
|
||||||
Statement::DocComment(doccomment) => {
|
Statement::DocComment(doccomment) => {
|
||||||
let content = doccomment.content();
|
let content = doccomment.content();
|
||||||
Ok(Some(Command::Comment(content.to_string())))
|
Ok(Some(Command::Comment(content.to_string())))
|
||||||
}
|
}
|
||||||
Statement::Grouping(group) => {
|
Statement::Grouping(group) => {
|
||||||
|
let child_scope = scope.new_child();
|
||||||
let statements = group.block().statements();
|
let statements = group.block().statements();
|
||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| {
|
.filter_map(|statement| {
|
||||||
self.transpile_statement(statement, program_identifier, handler)
|
self.transpile_statement(
|
||||||
|
statement,
|
||||||
|
program_identifier,
|
||||||
|
&child_scope,
|
||||||
|
handler,
|
||||||
|
)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -521,7 +544,7 @@ impl Transpiler {
|
||||||
#[expect(clippy::match_wildcard_for_single_variants)]
|
#[expect(clippy::match_wildcard_for_single_variants)]
|
||||||
SemicolonStatement::Expression(expr) => match expr {
|
SemicolonStatement::Expression(expr) => match expr {
|
||||||
Expression::Primary(Primary::FunctionCall(func)) => {
|
Expression::Primary(Primary::FunctionCall(func)) => {
|
||||||
self.transpile_function_call(func, handler).map(Some)
|
self.transpile_function_call(func, scope, handler).map(Some)
|
||||||
}
|
}
|
||||||
unexpected => {
|
unexpected => {
|
||||||
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
|
let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
|
||||||
|
@ -531,8 +554,24 @@ impl Transpiler {
|
||||||
Err(error)
|
Err(error)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
SemicolonStatement::VariableDeclaration(_) => {
|
SemicolonStatement::VariableDeclaration(decl) => {
|
||||||
todo!("Variable declarations are not yet supported.")
|
// let value = match decl {
|
||||||
|
// VariableDeclaration::Single(single) => {
|
||||||
|
// match single.variable_type().keyword {
|
||||||
|
// KeywordKind::Int => {
|
||||||
|
// VariableType::ScoreboardValue { objective: (), name: () }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// TODO: only for demonstration
|
||||||
|
// scope.set_variable(
|
||||||
|
// decl.identifier().span.str(),
|
||||||
|
// VariableType::Tag {
|
||||||
|
// tag_name: "TODO".to_string(),
|
||||||
|
// },
|
||||||
|
// );
|
||||||
|
todo!("Variable declarations are not yet supported: {decl:?}")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -541,14 +580,19 @@ impl Transpiler {
|
||||||
fn transpile_function_call(
|
fn transpile_function_call(
|
||||||
&mut self,
|
&mut self,
|
||||||
func: &FunctionCall,
|
func: &FunctionCall,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Command> {
|
) -> TranspileResult<Command> {
|
||||||
let arguments = func
|
let arguments = func
|
||||||
.arguments()
|
.arguments()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|l| l.elements().map(Deref::deref).collect::<Vec<_>>());
|
.map(|l| l.elements().map(Deref::deref).collect::<Vec<_>>());
|
||||||
let (location, arguments) =
|
let (location, arguments) = self.get_or_transpile_function(
|
||||||
self.get_or_transpile_function(&func.identifier().span, arguments.as_deref(), handler)?;
|
&func.identifier().span,
|
||||||
|
arguments.as_deref(),
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?;
|
||||||
let mut function_call = format!("function {location}");
|
let mut function_call = format!("function {location}");
|
||||||
if let Some(arguments) = arguments {
|
if let Some(arguments) = arguments {
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
@ -572,9 +616,10 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
execute: &ExecuteBlock,
|
execute: &ExecuteBlock,
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Option<Command>> {
|
) -> TranspileResult<Option<Command>> {
|
||||||
self.transpile_execute_block_internal(execute, program_identifier, handler)
|
self.transpile_execute_block_internal(execute, program_identifier, scope, handler)
|
||||||
.map(|ex| ex.map(Command::Execute))
|
.map(|ex| ex.map(Command::Execute))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,6 +627,7 @@ impl Transpiler {
|
||||||
&mut self,
|
&mut self,
|
||||||
execute: &ExecuteBlock,
|
execute: &ExecuteBlock,
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Option<Execute>> {
|
) -> TranspileResult<Option<Execute>> {
|
||||||
match execute {
|
match execute {
|
||||||
|
@ -593,7 +639,7 @@ impl Transpiler {
|
||||||
.statements()
|
.statements()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|s| {
|
.filter_map(|s| {
|
||||||
self.transpile_statement(s, program_identifier, handler)
|
self.transpile_statement(s, program_identifier, scope, handler)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -614,11 +660,12 @@ impl Transpiler {
|
||||||
.transpile_execute_block_internal(
|
.transpile_execute_block_internal(
|
||||||
execute_block,
|
execute_block,
|
||||||
program_identifier,
|
program_identifier,
|
||||||
|
scope,
|
||||||
handler,
|
handler,
|
||||||
),
|
),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
self.combine_execute_head_tail(head, tail, program_identifier, handler)
|
self.combine_execute_head_tail(head, tail, program_identifier, scope, handler)
|
||||||
}
|
}
|
||||||
ExecuteBlock::IfElse(cond, block, el) => {
|
ExecuteBlock::IfElse(cond, block, el) => {
|
||||||
let statements = block.statements();
|
let statements = block.statements();
|
||||||
|
@ -629,7 +676,7 @@ impl Transpiler {
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| {
|
.filter_map(|statement| {
|
||||||
self.transpile_statement(statement, program_identifier, handler)
|
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -641,7 +688,7 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
Some(Execute::Runs(commands))
|
Some(Execute::Runs(commands))
|
||||||
} else {
|
} else {
|
||||||
self.transpile_statement(&statements[0], program_identifier, handler)?
|
self.transpile_statement(&statements[0], program_identifier, scope, handler)?
|
||||||
.map(|cmd| Execute::Run(Box::new(cmd)))
|
.map(|cmd| Execute::Run(Box::new(cmd)))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -653,6 +700,7 @@ impl Transpiler {
|
||||||
then,
|
then,
|
||||||
Some(el),
|
Some(el),
|
||||||
program_identifier,
|
program_identifier,
|
||||||
|
scope,
|
||||||
handler,
|
handler,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
@ -667,6 +715,7 @@ impl Transpiler {
|
||||||
then: Execute,
|
then: Execute,
|
||||||
el: Option<&Else>,
|
el: Option<&Else>,
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Option<Execute>> {
|
) -> TranspileResult<Option<Execute>> {
|
||||||
let (_, cond) = cond.clone().dissolve();
|
let (_, cond) = cond.clone().dissolve();
|
||||||
|
@ -680,7 +729,7 @@ 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], program_identifier, handler)
|
self.transpile_statement(&statements[0], program_identifier, scope, handler)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -690,7 +739,7 @@ impl Transpiler {
|
||||||
let commands = statements
|
let commands = statements
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|statement| {
|
.filter_map(|statement| {
|
||||||
self.transpile_statement(statement, program_identifier, handler)
|
self.transpile_statement(statement, program_identifier, scope, handler)
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
errors.push(err);
|
errors.push(err);
|
||||||
None
|
None
|
||||||
|
@ -718,12 +767,20 @@ impl Transpiler {
|
||||||
head: &ExecuteBlockHead,
|
head: &ExecuteBlockHead,
|
||||||
tail: Option<Execute>,
|
tail: Option<Execute>,
|
||||||
program_identifier: &str,
|
program_identifier: &str,
|
||||||
|
scope: &Scope,
|
||||||
handler: &impl Handler<base::Error>,
|
handler: &impl Handler<base::Error>,
|
||||||
) -> TranspileResult<Option<Execute>> {
|
) -> TranspileResult<Option<Execute>> {
|
||||||
Ok(match head {
|
Ok(match head {
|
||||||
ExecuteBlockHead::Conditional(cond) => {
|
ExecuteBlockHead::Conditional(cond) => {
|
||||||
if let Some(tail) = tail {
|
if let Some(tail) = tail {
|
||||||
self.transpile_conditional(cond, tail, None, program_identifier, handler)?
|
self.transpile_conditional(
|
||||||
|
cond,
|
||||||
|
tail,
|
||||||
|
None,
|
||||||
|
program_identifier,
|
||||||
|
scope,
|
||||||
|
handler,
|
||||||
|
)?
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,11 +47,11 @@ pub fn identifier_to_macro(ident: &str) -> std::borrow::Cow<str> {
|
||||||
if ident.contains("__")
|
if ident.contains("__")
|
||||||
|| ident
|
|| ident
|
||||||
.chars()
|
.chars()
|
||||||
.any(|c| !(c == '_' && c.is_ascii_alphanumeric()))
|
.any(|c| c == '_' || !c.is_ascii_alphanumeric())
|
||||||
{
|
{
|
||||||
let new_ident = ident
|
let new_ident = ident
|
||||||
.chars()
|
.chars()
|
||||||
.filter(|c| *c == '_' || c.is_ascii_alphanumeric())
|
.filter(|c| *c != '_' && c.is_ascii_alphanumeric())
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
|
|
||||||
let chksum = md5::hash(ident).to_hex_lowercase();
|
let chksum = md5::hash(ident).to_hex_lowercase();
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
#![expect(unused)]
|
||||||
|
|
||||||
|
use std::{collections::HashMap, sync::RwLock};
|
||||||
|
|
||||||
|
use super::Transpiler;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum VariableType {
|
||||||
|
FunctionArgument {
|
||||||
|
index: usize,
|
||||||
|
},
|
||||||
|
Scoreboard {
|
||||||
|
objective: String,
|
||||||
|
},
|
||||||
|
ScoreboardValue {
|
||||||
|
objective: String,
|
||||||
|
name: String,
|
||||||
|
},
|
||||||
|
ScoreboardArray {
|
||||||
|
objective: String,
|
||||||
|
names: Vec<String>,
|
||||||
|
},
|
||||||
|
Tag {
|
||||||
|
tag_name: String,
|
||||||
|
},
|
||||||
|
BooleanStorage {
|
||||||
|
storage_name: String,
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
BooleanStorageArray {
|
||||||
|
storage_name: String,
|
||||||
|
paths: Vec<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Scope<'a> {
|
||||||
|
parent: Option<&'a Scope<'a>>,
|
||||||
|
variables: RwLock<HashMap<String, VariableType>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Scope<'a> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_child(&'a self) -> Self {
|
||||||
|
Self {
|
||||||
|
parent: Some(self),
|
||||||
|
variables: RwLock::new(HashMap::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_variable(&self, name: &str) -> Option<VariableType> {
|
||||||
|
let var = self.variables.read().unwrap().get(name).cloned();
|
||||||
|
if var.is_some() {
|
||||||
|
var
|
||||||
|
} else {
|
||||||
|
self.parent.and_then(|parent| parent.get_variable(name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_variable(&self, name: &str, var: VariableType) {
|
||||||
|
self.variables
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(name.to_string(), var);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_parent(&self) -> Option<&'a Self> {
|
||||||
|
self.parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transpiler {}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_scope() {
|
||||||
|
let scope = Scope::new();
|
||||||
|
{
|
||||||
|
let mut variables = scope.variables.write().unwrap();
|
||||||
|
variables.insert(
|
||||||
|
"test".to_string(),
|
||||||
|
VariableType::Scoreboard {
|
||||||
|
objective: "test".to_string(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
match scope.get_variable("test") {
|
||||||
|
Some(VariableType::Scoreboard { objective }) => assert_eq!(objective, "test"),
|
||||||
|
_ => panic!("Incorrect Variable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parent() {
|
||||||
|
let scope = Scope::new();
|
||||||
|
{
|
||||||
|
let mut variables = scope.variables.write().unwrap();
|
||||||
|
variables.insert(
|
||||||
|
"test".to_string(),
|
||||||
|
VariableType::Scoreboard {
|
||||||
|
objective: "test".to_string(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let child = scope.new_child();
|
||||||
|
match child.get_variable("test") {
|
||||||
|
Some(VariableType::Scoreboard { objective }) => assert_eq!(objective, "test"),
|
||||||
|
_ => panic!("Incorrect Variable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue