2021-07-22 19:50:59 +00:00
|
|
|
use crate::{Block, Expr, Expression, ParserWorkingSet, Pipeline, Span, Statement};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FlatShape {
|
|
|
|
Garbage,
|
|
|
|
Int,
|
|
|
|
InternalCall,
|
|
|
|
External,
|
|
|
|
Literal,
|
|
|
|
Operator,
|
|
|
|
Signature,
|
|
|
|
String,
|
|
|
|
Variable,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ParserWorkingSet<'a> {
|
|
|
|
pub fn flatten_block(&self, block: &Block) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
|
|
|
for stmt in &block.stmts {
|
|
|
|
output.extend(self.flatten_statement(stmt));
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flatten_statement(&self, stmt: &Statement) -> Vec<(Span, FlatShape)> {
|
|
|
|
match stmt {
|
|
|
|
Statement::Expression(expr) => self.flatten_expression(expr),
|
|
|
|
Statement::Pipeline(pipeline) => self.flatten_pipeline(pipeline),
|
2021-07-23 21:19:30 +00:00
|
|
|
_ => vec![],
|
2021-07-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flatten_expression(&self, expr: &Expression) -> Vec<(Span, FlatShape)> {
|
|
|
|
match &expr.expr {
|
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
output.extend(self.flatten_expression(&lhs));
|
|
|
|
output.extend(self.flatten_expression(&op));
|
|
|
|
output.extend(self.flatten_expression(&rhs));
|
|
|
|
output
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Block(block_id) => self.flatten_block(self.get_block(*block_id)),
|
2021-07-22 19:50:59 +00:00
|
|
|
Expr::Call(call) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
output.push((call.head, FlatShape::InternalCall));
|
|
|
|
for positional in &call.positional {
|
|
|
|
output.extend(self.flatten_expression(positional));
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::ExternalCall(..) => {
|
|
|
|
vec![(expr.span, FlatShape::External)]
|
|
|
|
}
|
|
|
|
Expr::Garbage => {
|
|
|
|
vec![(expr.span, FlatShape::Garbage)]
|
|
|
|
}
|
|
|
|
Expr::Int(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Int)]
|
|
|
|
}
|
|
|
|
Expr::List(list) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for l in list {
|
|
|
|
output.extend(self.flatten_expression(l));
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Literal(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Literal)]
|
|
|
|
}
|
|
|
|
Expr::Operator(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Operator)]
|
|
|
|
}
|
|
|
|
Expr::Signature(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Signature)]
|
|
|
|
}
|
|
|
|
Expr::String(_) => {
|
|
|
|
vec![(expr.span, FlatShape::String)]
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Subexpression(block_id) => self.flatten_block(self.get_block(*block_id)),
|
2021-07-22 19:50:59 +00:00
|
|
|
Expr::Table(headers, cells) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for e in headers {
|
|
|
|
output.extend(self.flatten_expression(e));
|
|
|
|
}
|
|
|
|
for row in cells {
|
|
|
|
for expr in row {
|
|
|
|
output.extend(self.flatten_expression(expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
Expr::Var(_) => {
|
|
|
|
vec![(expr.span, FlatShape::Variable)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flatten_pipeline(&self, pipeline: &Pipeline) -> Vec<(Span, FlatShape)> {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in &pipeline.expressions {
|
|
|
|
output.extend(self.flatten_expression(expr))
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
}
|