pub fn run_program(prog: &[u32], vars: &Vars) -> Option<[f32; 3]> {
	let mut st = [[0.0, 0.0, 0.0]; 32];
	let mut sp = 0;

	for ip in 0..prog.len() {
		match prog[ip] {
			NOP => {
			},
			VAR_ONES => {
				st[sp] = [1.0, 1.0, 1.0];
				sp += 1;
			},
			VAR_TWOS => {
				st[sp] = [2.0, 2.0, 2.0];
				sp += 1;
			},
			VAR_INPUT => {
				st[sp] = vars.input;
				sp += 1;
			},
			VAR_OWN => {
				st[sp] = vars.own;
				sp += 1;
			},
			VAR_MEAN => {
				st[sp] = vars.mean;
				sp += 1;
			},
			VAR_NEIGHS => {
				st[sp] = vars.neighs;
				sp += 1;
			},
			PLUS => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] + st[sp][i];
				}
			},
			EXP => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i].powf(st[sp][i]);
				}
			},
			MINUS => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] - st[sp][i];
				}
			},
			VMUL => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				let mut r = 0.0;
				for i in 0..3 {
					r += st[sp - 1][i] * st[sp][i];
				}
				st[sp - 1][0] = r;
				st[sp - 1][1] = 0.0;
				st[sp - 1][2] = 0.0;
			},
			MUL => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] * st[sp][i];
				}
			},
			DIV => {
				if sp < 2 {
					return None;
				}
				for i in 0..3 {
					st[sp - 2][i] = st[sp - 2][i] / st[sp - 1][i];
				}
				sp -= 1;
			},
			GT => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = ((st[sp - 1][i] > st[sp][i]) as u32) as f32;
				}
			},
			MAX => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					let a = st[sp][i];
					let b = st[sp - 1][i];
					st[sp - 1][i] = if a >= b { a } else { b };
				}
			},
			_ => {
				panic!("Unknown command");
			}
		};

		if sp == 0 {
			break;
		}
	}

	// Check for valid result
	if sp > 0 {
		Some(st[0])
	} else {
		None
	}
}
// A virtual machine to run small programs with the availability of some environment varibles
A virtual machine to run small programs with the availability of some environment varibles
A virtual machine to run small programs with the availability of some environment varibles
pub fn run_program(prog: &[u32], vars: &Vars) -> Option<[f32; 3]> {
	let mut st = [[0.0, 0.0, 0.0]; 32];
	let mut sp = 0;
	for ip in 0..prog.len() {
		match prog[ip] {
			NOP => {
			},
			VAR_ONES => {
				st[sp] = [1.0, 1.0, 1.0];
				sp += 1;
			},
			VAR_TWOS => {
				st[sp] = [2.0, 2.0, 2.0];
				sp += 1;
			},
			VAR_INPUT => {
				st[sp] = vars.input;
				sp += 1;
			},
			VAR_OWN => {
				st[sp] = vars.own;
				sp += 1;
			},
			VAR_MEAN => {
				st[sp] = vars.mean;
				sp += 1;
			},
			VAR_NEIGHS => {
				st[sp] = vars.neighs;
				sp += 1;
			},
			PLUS => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] + st[sp][i];
				}
			},
			EXP => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i].powf(st[sp][i]);
				}
			},
			MINUS => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] - st[sp][i];
				}
			},
			VMUL => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				let mut r = 0.0;
				for i in 0..3 {
					r += st[sp - 1][i] * st[sp][i];
				}
				st[sp - 1][0] = r;
				st[sp - 1][1] = 0.0;
				st[sp - 1][2] = 0.0;
			},
			MUL => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = st[sp - 1][i] * st[sp][i];
				}
			},
			DIV => {
				if sp < 2 {
					return None;
				}
				for i in 0..3 {
					st[sp - 2][i] = st[sp - 2][i] / st[sp - 1][i];
				}
				sp -= 1;
			},
			GT => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					st[sp - 1][i] = ((st[sp - 1][i] > st[sp][i]) as u32) as f32;
				}
			},
			MAX => {
				if sp < 2 {
					return None;
				}
				sp -= 1;
				for i in 0..3 {
					let a = st[sp][i];
					let b = st[sp - 1][i];
					st[sp - 1][i] = if a >= b { a } else { b };
				}
			},
			_ => {
				panic!("Unknown command");
			}
		};
		if sp == 0 {
			break;
		}
	}
	// Check for valid result
	if sp > 0 {
		Some(st[0])
	} else {
		None
	}
}
pub fn run_program ( prog : & [ u32 ] , vars : & Vars ) -> Option < [ f32 ; 3 ] > { let mut st = [ [ 0.0 , 0.0 , 0.0 ] ; 32 ] ; let mut sp = 0 ; for ip in 0 .. prog . len ( ) { match prog [ ip ] { NOP => { } , VAR_ONES => { st [ sp ] = [ 1.0 , 1.0 , 1.0 ] ; sp += 1 ; } , VAR_TWOS => { st [ sp ] = [ 2.0 , 2.0 , 2.0 ] ; sp += 1 ; } , VAR_INPUT => { st [ sp ] = vars . input ; sp += 1 ; } , VAR_OWN => { st [ sp ] = vars . own ; sp += 1 ; } , VAR_MEAN => { st [ sp ] = vars . mean ; sp += 1 ; } , VAR_NEIGHS => { st [ sp ] = vars . neighs ; sp += 1 ; } , PLUS => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { st [ sp - 1 ] [ i ] = st [ sp - 1 ] [ i ] + st [ sp ] [ i ] ; } } , EXP => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { st [ sp - 1 ] [ i ] = st [ sp - 1 ] [ i ] . powf ( st [ sp ] [ i ] ) ; } } , MINUS => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { st [ sp - 1 ] [ i ] = st [ sp - 1 ] [ i ] - st [ sp ] [ i ] ; } } , VMUL => { if sp < 2 { return None ; } sp -= 1 ; let mut r = 0.0 ; for i in 0 .. 3 { r += st [ sp - 1 ] [ i ] * st [ sp ] [ i ] ; } st [ sp - 1 ] [ 0 ] = r ; st [ sp - 1 ] [ 1 ] = 0.0 ; st [ sp - 1 ] [ 2 ] = 0.0 ; } , MUL => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { st [ sp - 1 ] [ i ] = st [ sp - 1 ] [ i ] * st [ sp ] [ i ] ; } } , DIV => { if sp < 2 { return None ; } for i in 0 .. 3 { st [ sp - 2 ] [ i ] = st [ sp - 2 ] [ i ] / st [ sp - 1 ] [ i ] ; } sp -= 1 ; } , GT => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { st [ sp - 1 ] [ i ] = ( ( st [ sp - 1 ] [ i ] > st [ sp ] [ i ] ) as u32 ) as f32 ; } } , MAX => { if sp < 2 { return None ; } sp -= 1 ; for i in 0 .. 3 { let a = st [ sp ] [ i ] ; let b = st [ sp - 1 ] [ i ] ; st [ sp - 1 ] [ i ] = if a >= b { a } else { b } ; } } , _ => { panic ! ( " " ) ; } } ; if sp == 0 { break ; } } // Check for valid result if sp > 0 { Some ( st [ 0 ] ) } else { None } }
A virtual machine to run small programs with the availability of some environment varibles
A virtual machine to run small programs with the availability of some environment varibles
prog
&[u32]
vars
&Vars
prog
&[u32]
vars
&Vars