Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoblon committed Feb 3, 2021
1 parent 787e741 commit 722c6ce
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 67 deletions.
73 changes: 73 additions & 0 deletions rudder-lang/src/ir/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
context::Type,
context::VarContext,
enum_tree::{EnumItem, EnumTree},
value::Value,
};
use crate::{error::*, parser::*};
use std::collections::{HashMap, HashSet};
Expand All @@ -14,6 +15,77 @@ use toml::Value as TomlValue;
// TODO named item tests
// TODO aliases tests

#[derive(Debug, PartialEq, Clone)]
pub enum Expression<'src> {
Enum(EnumExpression<'src>),
Variable(VariableExpression<'src>),
}

#[derive(Debug, PartialEq, Clone)]
pub enum Variable<'src> {
Value(Value<'src>),
Identifier(Token<'src>),
}

/// An expression that can be defined using variables and raw rudderlang types
#[derive(Debug, PartialEq, Clone)]
pub struct VariableExpression<'src> {
pub source: Token<'src>,
pub expression: VariableExpressionPart<'src>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum VariableExpressionPart<'src> {
// list and struct
// implicit: NotIncludes = Not(Includes)
Includes(Box<Variable<'src>>, Box<Variable<'src>>),

// integer and float
// includes Smaller (reversed left/right)
GreaterOrEqual(Box<Variable<'src>>, Box<Variable<'src>>),
Greater(Box<Variable<'src>>, Box<Variable<'src>>),

// list struct integer float string
// explicit & implicit: NotEqual = Not(Equal)
Equal(
Box<VariableExpressionPart<'src>>,
Box<VariableExpressionPart<'src>>,
),
And(
Box<VariableExpressionPart<'src>>,
Box<VariableExpressionPart<'src>>,
),
Or(
Box<VariableExpressionPart<'src>>,
Box<VariableExpressionPart<'src>>,
),
Not(Box<VariableExpressionPart<'src>>),
}

impl<'src> VariableExpressionPart<'src> {
/// List all variables that are used in an expression,
/// and put them into the 'variables' hashset
/// this is recursive mutable, pass it an empty hashset at first call
/// Only used by evaluate.
fn list_variables_tree(
&self,
variables: &mut HashMap<(Token<'src>, Token<'src>), HashSet<Token<'src>>>, // (variable, tree) -> item list
) {
unimplemented!()
// match self {
// VariableExpressionPart::Not(v) => v.list_variables_tree(variables),
// VariableExpressionPart::Or(v1, v2) => {
// v1.list_variables_tree(variables);
// v2.list_variables_tree(variables);
// }
// VariableExpressionPart::And(v1, v2) => {
// v1.list_variables_tree(variables);
// v2.list_variables_tree(variables);
// }
// }
}
}

/// EnumList Is a singleton containing all enum definitions
/// It also has a direct pointer from an item name to its enum type
#[derive(Debug)]
Expand Down Expand Up @@ -624,6 +696,7 @@ impl<'it, 'src> Iterator for CrossIterator<'it, 'src> {
None
}
}

/// A boolean expression that can be defined using enums
#[derive(Debug, PartialEq, Clone)]
pub struct EnumExpression<'src> {
Expand Down
10 changes: 5 additions & 5 deletions rudder-lang/src/ir/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,9 @@ impl<'src> Statement<'src> {
PStatement::Noop => Statement::Noop,
PStatement::Case(case, v) => Statement::Case(
case,
map_vec_results(v.into_iter(), |(exp, sts)| {
let expr = enum_list.canonify_expression(context, exp)?;
Ok((
expr,
map_vec_results(v.into_iter(), |(exp, sts)| match exp {
PExpression::Enum(e) => Ok((
enum_list.canonify_expression(context, e)?,
map_vec_results(sts.into_iter(), |st| {
Statement::from_pstatement(
context,
Expand All @@ -546,7 +545,8 @@ impl<'src> Statement<'src> {
enum_list,
)
})?,
))
)),
PExpression::Variable(v) => unimplemented!(),
})?,
),
})
Expand Down
38 changes: 30 additions & 8 deletions rudder-lang/src/ir/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use super::{
context::Type,
context::VarContext,
enums::{EnumExpression, EnumList},
enums::{EnumExpression, EnumList, Expression, VariableExpression},
};
use crate::{error::*, parser::*};
use std::{collections::HashMap, convert::TryFrom};
Expand Down Expand Up @@ -190,13 +190,10 @@ impl<'src> Value<'src> {
pub struct ComplexValue<'src> {
source: Token<'src>,
// nested complex values not supported
cases: Vec<(EnumExpression<'src>, Option<Value<'src>>)>,
cases: Vec<(Expression<'src>, Option<Value<'src>>)>,
}
impl<'src> ComplexValue<'src> {
pub fn new(
source: Token<'src>,
cases: Vec<(EnumExpression<'src>, Option<Value<'src>>)>,
) -> Self {
pub fn new(source: Token<'src>, cases: Vec<(Expression<'src>, Option<Value<'src>>)>) -> Self {
Self { source, cases }
}

Expand All @@ -206,7 +203,12 @@ impl<'src> ComplexValue<'src> {
pvalue: PComplexValue<'src>,
) -> Result<Self> {
let cases = map_vec_results(pvalue.cases.into_iter(), |(case, value)| {
let case = enum_list.canonify_expression(context, case)?;
let case = match case {
PExpression::Enum(expr) => {
Expression::Enum(enum_list.canonify_expression(context, expr)?)
}
PExpression::Variable(expr) => unimplemented!(),
};
let value = match value {
None => None,
Some(v) => Some(Value::from_pvalue(enum_list, context, v)?),
Expand Down Expand Up @@ -247,8 +249,28 @@ impl<'src> ComplexValue<'src> {
}

pub fn verify(&self, enum_list: &EnumList<'src>) -> Result<()> {
let mut enum_cases: Vec<(EnumExpression, Option<Value>)> = Vec::new();
let mut variable_cases: Vec<(VariableExpression, Option<Value>)> = Vec::new();
for (expr, val) in self.cases.clone() {
match expr {
Expression::Enum(e) => enum_cases.push((e, val)),
Expression::Variable(e) => variable_cases.push((e, val)),
};
}
// check enums
let mut errors = enum_list.evaluate(&self.cases, "TODO".into());
let enum_cases = self
.cases
.iter()
.filter_map(|(expr, val)| {
if let Expression::Enum(e) = expr {
Some((e.clone(), val.clone()))
} else {
None
}
})
.collect::<Vec<(EnumExpression, Option<Value>)>>();
let variable_cases = unimplemented!();
let mut errors = enum_list.evaluate(&enum_cases, "TODO".into());
// check type
let init_val = self
.first_value()
Expand Down
Loading

0 comments on commit 722c6ce

Please sign in to comment.