1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
use duckscript::parser; use duckscript::runner; use duckscript::types::command::{CommandResult, Commands, GoToValue}; use duckscript::types::instruction::{Instruction, InstructionType}; use duckscript::types::runtime::StateValue; use std::collections::HashMap; #[cfg(test)] #[path = "./eval_test.rs"] mod eval_test; fn parse(arguments: &Vec<String>) -> Result<Instruction, String> { let mut line_buffer = String::new(); for argument in arguments { if argument.contains(" ") { line_buffer.push('"'); } line_buffer.push_str(argument); if argument.contains(" ") { line_buffer.push('"'); } line_buffer.push(' '); } let line_str = line_buffer .replace("\r", "") .replace("\n", "") .replace("\\", "\\\\"); match parser::parse_text(&line_str) { Ok(instructions) => Ok(instructions[0].clone()), Err(error) => Err(error.to_string()), } } pub(crate) fn eval( arguments: &Vec<String>, state: &mut HashMap<String, StateValue>, variables: &mut HashMap<String, String>, commands: &mut Commands, ) -> Result<CommandResult, String> { if arguments.is_empty() { Ok(CommandResult::Continue(None)) } else { match parse(arguments) { Ok(instruction) => { let (command_result, _) = runner::run_instruction(commands, variables, state, &vec![], instruction, 0); Ok(command_result) } Err(error) => Err(error.to_string()), } } } pub(crate) fn eval_with_error( arguments: &Vec<String>, state: &mut HashMap<String, StateValue>, variables: &mut HashMap<String, String>, commands: &mut Commands, ) -> CommandResult { match eval(arguments, state, variables, commands) { Ok(command_result) => match command_result.clone() { CommandResult::Crash(error) => CommandResult::Error(error), _ => command_result, }, Err(error) => CommandResult::Error(error.to_string()), } } pub(crate) fn eval_with_instructions( arguments: &Vec<String>, instructions: &Vec<Instruction>, state: &mut HashMap<String, StateValue>, variables: &mut HashMap<String, String>, commands: &mut Commands, ) -> CommandResult { if arguments.is_empty() { CommandResult::Continue(None) } else { match parse(arguments) { Ok(instruction) => { let mut all_instructions = instructions.clone(); all_instructions.push(instruction); let (flow_result, flow_output) = eval_instructions( &all_instructions, commands, state, variables, all_instructions.len() - 1, ); match flow_result { Some(result) => match result.clone() { CommandResult::Crash(error) => CommandResult::Error(error), _ => result, }, None => CommandResult::Continue(flow_output), } } Err(error) => CommandResult::Error(error), } } } pub(crate) fn eval_instructions( instructions: &Vec<Instruction>, commands: &mut Commands, state: &mut HashMap<String, StateValue>, variables: &mut HashMap<String, String>, start_line: usize, ) -> (Option<CommandResult>, Option<String>) { let mut line = start_line; let mut flow_output = None; let mut flow_result = None; loop { let instruction = if instructions.len() > line { instructions[line].clone() } else { break; }; match instruction.instruction_type { InstructionType::Script(ref script_instruction) => { let (command_result, _) = runner::run_instruction( commands, variables, state, &instructions, instruction.clone(), line, ); match command_result { CommandResult::Exit(output) => { flow_result = Some(CommandResult::Exit(output)); break; } CommandResult::Error(error) => { flow_result = Some(CommandResult::Error(error)); break; } CommandResult::Crash(error) => { flow_result = Some(CommandResult::Crash(error)); break; } CommandResult::GoTo(output, goto_value) => { flow_output = output.clone(); match goto_value { GoToValue::Label(_) => { flow_result = Some(CommandResult::Error( "goto label result not supported in alias command flow." .to_string(), )); break; } GoToValue::Line(line_number) => line = line_number, } } CommandResult::Continue(output) => { flow_output = output.clone(); match script_instruction.output { Some(ref output_variable) => { match output { Some(value) => { variables.insert(output_variable.to_string(), value) } None => variables.remove(output_variable), }; } None => (), }; line = line + 1; } }; } _ => { line = line + 1; } }; } (flow_result, flow_output) }