rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Substitute trait parameters in copied default method bodies.
use crate::ast::{Block, Expr, Stmt};
use crate::types::Type;
use std::collections::HashMap;

pub(crate) fn block(block: &mut Block, bindings: &HashMap<String, Type>) {
    for statement in &mut block.statements {
        match statement {
            Stmt::Let {
                type_annotation,
                initializer,
                ..
            } => {
                if let Some(ty) = type_annotation {
                    *ty = ty.substitute(bindings);
                }
                expression(initializer, bindings);
            }
            Stmt::While {
                condition, body, ..
            } => {
                expression(condition, bindings);
                self::block(body, bindings);
            }
            Stmt::For { iterable, body, .. } => {
                expression(iterable, bindings);
                self::block(body, bindings);
            }
            Stmt::Loop { body, .. } => self::block(body, bindings),
            Stmt::Return { value, .. } | Stmt::Break { value, .. } => {
                if let Some(value) = value {
                    expression(value, bindings);
                }
            }
            Stmt::Expr {
                expression: value, ..
            } => expression(value, bindings),
            // Nested declarations introduce their own generic scope.
            _ => {}
        }
    }
}

fn expression(expr: &mut Expr, bindings: &HashMap<String, Type>) {
    match expr {
        Expr::QualifiedPath {
            target,
            trait_arguments,
            ..
        } => {
            *target = target.substitute(bindings);
            for ty in trait_arguments {
                *ty = ty.substitute(bindings);
            }
        }
        Expr::GenericPath { arguments, .. } => {
            for ty in arguments {
                *ty = ty.substitute(bindings);
            }
        }
        Expr::Cast {
            operand, target, ..
        } => {
            *target = target.substitute(bindings);
            expression(operand, bindings);
        }
        Expr::Member { object, .. }
        | Expr::Try {
            operand: object, ..
        }
        | Expr::Borrow { target: object, .. }
        | Expr::Unary {
            operand: object, ..
        } => expression(object, bindings),
        Expr::Index {
            object: left,
            index: right,
            ..
        }
        | Expr::Assign {
            target: left,
            value: right,
            ..
        }
        | Expr::Binary { left, right, .. }
        | Expr::Logical { left, right, .. }
        | Expr::Range {
            start: left,
            end: right,
            ..
        } => {
            expression(left, bindings);
            expression(right, bindings);
        }
        Expr::Tuple { elements, .. } => {
            for element in elements {
                expression(element, bindings);
            }
        }
        Expr::Array {
            elements, repeat, ..
        } => {
            for element in elements {
                expression(element, bindings);
            }
            if let Some(repeat) = repeat {
                expression(repeat, bindings);
            }
        }
        Expr::RecordLiteral { fields, .. } => {
            for field in fields {
                expression(&mut field.value, bindings);
            }
        }
        Expr::Call {
            callee, arguments, ..
        } => {
            expression(callee, bindings);
            for argument in arguments {
                expression(argument, bindings);
            }
        }
        Expr::If {
            condition,
            then_branch,
            else_branch,
            ..
        } => {
            expression(condition, bindings);
            block(then_branch, bindings);
            if let Some(branch) = else_branch {
                expression(branch, bindings);
            }
        }
        Expr::Match { value, arms, .. } => {
            expression(value, bindings);
            for arm in arms {
                expression(&mut arm.expression, bindings);
            }
        }
        Expr::Block(body) => block(body, bindings),
        Expr::Literal { .. } | Expr::Variable { .. } | Expr::Path { .. } => {}
    }
}