File size: 5,545 Bytes
d5bfab8 |
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 |
//! The `loda-rust eval` subcommand, evaluates terms of a program.
use loda_rust_core;
use std::time::Instant;
use std::rc::Rc;
use std::path::PathBuf;
use loda_rust_core::control::{DependencyManager,DependencyManagerFileSystemMode};
use loda_rust_core::execute::{NodeLoopLimit, ProgramCache, ProgramRunner, RegisterValue, RunMode};
use loda_rust_core::execute::NodeRegisterLimit;
use loda_rust_core::unofficial_function::UnofficialFunctionRegistry;
use crate::config::Config;
pub enum SubcommandEvaluateMode {
PrintTerms,
PrintSteps,
PrintDebug,
}
pub fn subcommand_evaluate(
program_id: u64,
number_of_terms: u64,
mode: SubcommandEvaluateMode,
) {
let config = Config::load();
let loda_programs_oeis_dir: PathBuf = config.loda_programs_oeis_dir();
let mut dm = DependencyManager::new(
DependencyManagerFileSystemMode::System,
loda_programs_oeis_dir,
UnofficialFunctionRegistry::new(),
);
let program_runner: Rc::<ProgramRunner> = match dm.load(program_id) {
Ok(value) => value,
Err(error) => {
panic!("Failed to load program: {:?}", error);
}
};
match mode {
SubcommandEvaluateMode::PrintTerms => {
program_runner.print_terms(number_of_terms);
},
SubcommandEvaluateMode::PrintSteps => {
program_runner.print_steps(number_of_terms);
},
SubcommandEvaluateMode::PrintDebug => {
program_runner.print_debug(number_of_terms);
}
}
}
trait PrintTermsStepsDebug {
fn print_terms(&self, count: u64);
fn print_steps(&self, count: u64);
fn print_debug(&self, count: u64);
}
impl PrintTermsStepsDebug for ProgramRunner {
fn print_terms(&self, count: u64) {
if count >= 0x7fff_ffff_ffff_ffff {
panic!("Value is too high. Cannot be converted to 64bit signed integer.");
}
if count < 1 {
panic!("Expected number of terms to be 1 or greater.");
}
let mut cache = ProgramCache::new();
let step_count_limit: u64 = 1000000000;
let mut step_count: u64 = 0;
let start_time = Instant::now();
for index in 0..(count as i64) {
let input = RegisterValue::from_i64(index);
let result_run = self.run(
input,
RunMode::Silent,
&mut step_count,
step_count_limit,
NodeRegisterLimit::Unlimited,
NodeLoopLimit::Unlimited,
&mut cache
);
let output: RegisterValue = match result_run {
Ok(value) => value,
Err(error) => {
panic!("Failure while computing term {}, error: {:?}", index, error);
}
};
if index == 0 {
print!("{}", output.0);
continue;
}
print!(",{}", output.0);
}
print!("\n");
debug!("steps: {}", step_count);
debug!("cache: {}", cache.hit_miss_info());
debug!("elapsed: {:?} ms", start_time.elapsed().as_millis());
}
fn print_steps(&self, count: u64) {
if count >= 0x7fff_ffff_ffff_ffff {
panic!("Value is too high. Cannot be converted to 64bit signed integer.");
}
if count < 1 {
panic!("Expected number of terms to be 1 or greater.");
}
let mut cache = ProgramCache::new();
let step_count_limit: u64 = 1000000000;
for index in 0..(count as i64) {
let input = RegisterValue::from_i64(index);
let mut step_count: u64 = 0;
let result_run = self.run(
input,
RunMode::Silent,
&mut step_count,
step_count_limit,
NodeRegisterLimit::Unlimited,
NodeLoopLimit::Unlimited,
&mut cache,
);
if let Err(error) = result_run {
panic!("Failure while computing term {}, error: {:?}", index, error);
}
if index == 0 {
print!("{}", step_count);
continue;
}
print!(",{}", step_count);
}
print!("\n");
}
fn print_debug(&self, count: u64) {
if count >= 0x7fff_ffff_ffff_ffff {
panic!("Value is too high. Cannot be converted to 64bit signed integer.");
}
if count < 1 {
panic!("Expected number of terms to be 1 or greater.");
}
let mut cache = ProgramCache::new();
let step_count_limit: u64 = 1000000000;
let mut step_count: u64 = 0;
for index in 0..(count as i64) {
println!("INPUT: a({})", index);
let input = RegisterValue::from_i64(index);
let result_run = self.run(
input,
RunMode::Verbose,
&mut step_count,
step_count_limit,
NodeRegisterLimit::Unlimited,
NodeLoopLimit::Unlimited,
&mut cache,
);
let output: RegisterValue = match result_run {
Ok(value) => value,
Err(error) => {
panic!("Failure while computing term {}, error: {:?}", index, error);
}
};
println!("OUTPUT: a({}) = {}", index, output.0);
}
debug!("stats: step_count: {}", step_count);
}
}
|