<commit_before>//! The `evaluate_expressions` module...


use backend::zcode::zfile::{ZOP, Operand, Variable, Constant, LargeConstant, Zfile, Type};
use frontend::ast::{ASTNode};
use frontend::codegen;
use frontend::codegen::{CodeGenManager};
use frontend::lexer::Token;
use frontend::lexer::Token::{TokNumOp, TokCompOp, TokLogOp, TokInt, TokBoolean, TokVariable, TokFunction, TokString, TokUnaryMinus};
use config::Config;

#[derive(Debug)]
pub enum EvaluateExpressionError {
    InvalidAST,
    NumericOperatorNeedsTwoArguments { op_name: String, location: (u64, u64) },
    UnhandledToken { token: Token },
    UnsupportedOperator { op_name: String, location: (u64, u64) },
    UnsupportedFunction { name: String, location: (u64, u64) },
    UnsupportedFunctionArgsLen { name: String, location: (u64, u64), expected: u64 },
    UnsupportedFunctionArgType { name: String, index: u64, location: (u64, u64) },
    NoTempIdLeftOnStack,
}

pub fn evaluate_expression<'a>(node: &'a ASTNode, code: &mut Vec<ZOP>, mut manager: &mut CodeGenManager<'a>, mut out: &mut Zfile) -> Operand {
    let mut temp_ids = CodeGenManager::new_temp_var_vec();
    evaluate_expression_internal(node, code, &mut temp_ids, manager, &mut out)
}

/// Evaluates an expression node to zCode.
fn evaluate_expression_internal<'a>(node: &'a ASTNode, code: &mut Vec<ZOP>,
        temp_ids: &mut Vec<u8>, mut manager: &mut CodeGenManager<'a>, mut out: &mut Zfile) -> Operand {
    let n = node.as_default();
    let cfg = manager.cfg;

    match n.category {
        TokNumOp { ref op_name, ref location } => {
            if n.childs.len() != 2 {
                error_panic!(cfg => EvaluateExpressionError::NumericOperatorNeedsTwoArguments { op_name: op_name.clone(), location: location.clone() } );

                // Try error recovery. Ignores operator.
                warn!("Trying to fix expression. This will compile but probably not do what you want.");<commit_msg>removed warning, maybe theres a nicer way?<commit_after>//! The `evaluate_expressions` module...


use backend::zcode::zfile::{ZOP, Operand, Variable, Constant, LargeConstant, Zfile, Type};
use frontend::ast::{ASTNode};
use frontend::codegen;
use frontend::codegen::{CodeGenManager};
use frontend::lexer::Token;
use frontend::lexer::Token::{TokNumOp, TokCompOp, TokLogOp, TokInt, TokBoolean, TokVariable, TokFunction, TokString, TokUnaryMinus};
#[allow(unused_imports)] use config::Config;

#[derive(Debug)]
pub enum EvaluateExpressionError {
    InvalidAST,
    NumericOperatorNeedsTwoArguments { op_name: String, location: (u64, u64) },
    UnhandledToken { token: Token },
    UnsupportedOperator { op_name: String, location: (u64, u64) },
    UnsupportedFunction { name: String, location: (u64, u64) },
    UnsupportedFunctionArgsLen { name: String, location: (u64, u64), expected: u64 },
    UnsupportedFunctionArgType { name: String, index: u64, location: (u64, u64) },
    NoTempIdLeftOnStack,
}

pub fn evaluate_expression<'a>(node: &'a ASTNode, code: &mut Vec<ZOP>, mut manager: &mut CodeGenManager<'a>, mut out: &mut Zfile) -> Operand {
    let mut temp_ids = CodeGenManager::new_temp_var_vec();
    evaluate_expression_internal(node, code, &mut temp_ids, manager, &mut out)
}

/// Evaluates an expression node to zCode.
fn evaluate_expression_internal<'a>(node: &'a ASTNode, code: &mut Vec<ZOP>,
        temp_ids: &mut Vec<u8>, mut manager: &mut CodeGenManager<'a>, mut out: &mut Zfile) -> Operand {
    let n = node.as_default();
    let cfg = manager.cfg;

    match n.category {
        TokNumOp { ref op_name, ref location } => {
            if n.childs.len() != 2 {
                error_panic!(cfg => EvaluateExpressionError::NumericOperatorNeedsTwoArguments { op_name: op_name.clone(), location: location.clone() } );

                // Try error recovery. Ignores operator.
                warn!("Trying to fix expression. This will compile but probably not do what you want.");
{'fraction_non_alphanumeric': 0.13267376966007102, 'fraction_numerical': 0.013191273465246067, 'mean_word_length': 4.386612021857924, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3619823', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1244, 'n_words': 344}