Implement enum definition abstraction
This commit is contained in:
parent
77ded361ff
commit
b864a23960
@ -1,4 +1,9 @@
|
|||||||
use super::{AbstractTree, Identifier, Type, WithPosition};
|
use crate::{
|
||||||
|
context::Context,
|
||||||
|
error::{RuntimeError, ValidationError},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{AbstractTree, Action, Identifier, Type, WithPosition};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct EnumDefinition {
|
pub struct EnumDefinition {
|
||||||
@ -22,24 +27,17 @@ impl EnumDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractTree for EnumDefinition {
|
impl AbstractTree for EnumDefinition {
|
||||||
fn expected_type(
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
||||||
&self,
|
Ok(Type::None)
|
||||||
_context: &crate::context::Context,
|
|
||||||
) -> Result<Type, crate::error::ValidationError> {
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
||||||
&self,
|
Ok(())
|
||||||
_context: &crate::context::Context,
|
|
||||||
) -> Result<(), crate::error::ValidationError> {
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
|
||||||
self,
|
context.set_enum_definition(self.name.clone(), self)?;
|
||||||
_context: &crate::context::Context,
|
|
||||||
) -> Result<super::Action, crate::error::RuntimeError> {
|
Ok(Action::None)
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ impl AbstractTree for Statement {
|
|||||||
Statement::Assignment(assignment) => assignment.expected_type(_context),
|
Statement::Assignment(assignment) => assignment.expected_type(_context),
|
||||||
Statement::Block(block) => block.expected_type(_context),
|
Statement::Block(block) => block.expected_type(_context),
|
||||||
Statement::Break => Ok(Type::None),
|
Statement::Break => Ok(Type::None),
|
||||||
Statement::EnumDefinition(_) => todo!(),
|
Statement::EnumDefinition(enum_definition) => enum_definition.expected_type(_context),
|
||||||
Statement::Expression(expression) => expression.expected_type(_context),
|
Statement::Expression(expression) => expression.expected_type(_context),
|
||||||
Statement::IfElse(if_else) => if_else.expected_type(_context),
|
Statement::IfElse(if_else) => if_else.expected_type(_context),
|
||||||
Statement::Loop(r#loop) => r#loop.expected_type(_context),
|
Statement::Loop(r#loop) => r#loop.expected_type(_context),
|
||||||
@ -38,7 +38,7 @@ impl AbstractTree for Statement {
|
|||||||
Statement::Assignment(assignment) => assignment.validate(_context),
|
Statement::Assignment(assignment) => assignment.validate(_context),
|
||||||
Statement::Block(block) => block.validate(_context),
|
Statement::Block(block) => block.validate(_context),
|
||||||
Statement::Break => Ok(()),
|
Statement::Break => Ok(()),
|
||||||
Statement::EnumDefinition(_) => todo!(),
|
Statement::EnumDefinition(enum_definition) => enum_definition.validate(_context),
|
||||||
Statement::Expression(expression) => expression.validate(_context),
|
Statement::Expression(expression) => expression.validate(_context),
|
||||||
Statement::IfElse(if_else) => if_else.validate(_context),
|
Statement::IfElse(if_else) => if_else.validate(_context),
|
||||||
Statement::Loop(r#loop) => r#loop.validate(_context),
|
Statement::Loop(r#loop) => r#loop.validate(_context),
|
||||||
@ -51,7 +51,7 @@ impl AbstractTree for Statement {
|
|||||||
Statement::Assignment(assignment) => assignment.run(_context),
|
Statement::Assignment(assignment) => assignment.run(_context),
|
||||||
Statement::Block(block) => block.run(_context),
|
Statement::Block(block) => block.run(_context),
|
||||||
Statement::Break => Ok(Action::Break),
|
Statement::Break => Ok(Action::Break),
|
||||||
Statement::EnumDefinition(_) => todo!(),
|
Statement::EnumDefinition(enum_definition) => enum_definition.run(_context),
|
||||||
Statement::Expression(expression) => expression.run(_context),
|
Statement::Expression(expression) => expression.run(_context),
|
||||||
Statement::IfElse(if_else) => if_else.run(_context),
|
Statement::IfElse(if_else) => if_else.run(_context),
|
||||||
Statement::Loop(r#loop) => r#loop.run(_context),
|
Statement::Loop(r#loop) => r#loop.run(_context),
|
||||||
|
@ -4,7 +4,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
abstract_tree::{Identifier, Type},
|
abstract_tree::{EnumDefinition, Identifier, Type},
|
||||||
error::RwLockPoisonError,
|
error::RwLockPoisonError,
|
||||||
value::{BUILT_IN_FUNCTIONS, BUILT_IN_MODULES},
|
value::{BUILT_IN_FUNCTIONS, BUILT_IN_MODULES},
|
||||||
Value,
|
Value,
|
||||||
@ -77,6 +77,7 @@ impl Context {
|
|||||||
let r#type = match value_data {
|
let r#type = match value_data {
|
||||||
ValueData::Type(r#type) => r#type.clone(),
|
ValueData::Type(r#type) => r#type.clone(),
|
||||||
ValueData::Value(value) => value.r#type(),
|
ValueData::Value(value) => value.r#type(),
|
||||||
|
ValueData::EnumDefinition(_) => return Ok(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
return Ok(Some(r#type.clone()));
|
return Ok(Some(r#type.clone()));
|
||||||
@ -117,6 +118,18 @@ impl Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_enum_definition(
|
||||||
|
&self,
|
||||||
|
identifier: Identifier,
|
||||||
|
definition: EnumDefinition,
|
||||||
|
) -> Result<(), RwLockPoisonError> {
|
||||||
|
let mut inner = self.inner.write()?;
|
||||||
|
|
||||||
|
inner.insert(identifier, ValueData::EnumDefinition(definition));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> {
|
pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> {
|
||||||
self.inner
|
self.inner
|
||||||
.write()?
|
.write()?
|
||||||
@ -138,4 +151,5 @@ impl Context {
|
|||||||
pub enum ValueData {
|
pub enum ValueData {
|
||||||
Type(Type),
|
Type(Type),
|
||||||
Value(Value),
|
Value(Value),
|
||||||
|
EnumDefinition(EnumDefinition),
|
||||||
}
|
}
|
||||||
|
@ -506,11 +506,11 @@ mod tests {
|
|||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
Identifier::new("Foo"),
|
Identifier::new("Foo"),
|
||||||
vec![Type::Custom(Identifier::new("F")).with_position((0, 0))],
|
vec![Type::Custom(Identifier::new("F")).with_position((62, 63))],
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
Identifier::new("Bar"),
|
Identifier::new("Bar"),
|
||||||
vec![Type::Custom(Identifier::new("B")).with_position((0, 0))]
|
vec![Type::Custom(Identifier::new("B")).with_position((90, 91))]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
|
Loading…
Reference in New Issue
Block a user