feat: finish evaluate arg allgorithm
This commit is contained in:
parent
d262d4b9fd
commit
9ccd76c7d9
@ -1,15 +1,16 @@
|
||||
<script lang="ts">
|
||||
import { decode, encode } from "$lib/helpers/flat_tree";
|
||||
|
||||
const input = [5, [6, 1], [7, 2, [5, 1]]];
|
||||
// const input = [5, [6, 1], [7, 2, [5, 1]]];
|
||||
// const input = [5, [], [6, []], []];
|
||||
// const input = [52];
|
||||
const input = [0, 0, [0, 2, 0, 128, 0, 128], 0, 128];
|
||||
|
||||
console.log("INPUT");
|
||||
console.log(input);
|
||||
|
||||
let encoded = encode(input);
|
||||
encoded = [0, 3, 5, ...encoded.slice(2).slice(0, -4), 5, 5, 1, 1];
|
||||
// encoded = [];
|
||||
console.log("ENCODED");
|
||||
console.log(encoded);
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
describe('sum test', () => {
|
||||
it('adds 1 + 2 to equal 3', () => {
|
||||
expect(1 + 2).toBe(3);
|
||||
});
|
||||
});
|
@ -1,4 +1,6 @@
|
||||
mod encoding;
|
||||
mod helpers;
|
||||
mod nodes;
|
||||
mod tree;
|
||||
pub use helpers::*;
|
||||
pub use tree::*;
|
||||
|
18
packages/plantarium/src/nodes.rs
Normal file
18
packages/plantarium/src/nodes.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use crate::encoding;
|
||||
|
||||
pub fn math_node(args: &[i32]) -> (i32, i32) {
|
||||
let math_type = args[0];
|
||||
|
||||
let a = encoding::decode_float(args[1], args[2]);
|
||||
let b = encoding::decode_float(args[3], args[4]);
|
||||
|
||||
let result = match math_type {
|
||||
0 => a + b,
|
||||
1 => a - b,
|
||||
2 => a * b,
|
||||
3 => a / b,
|
||||
_ => 0.0,
|
||||
};
|
||||
|
||||
encoding::encode_float(result)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
pub fn get_args<'a>(args: &'a Vec<i32>) -> Vec<&[i32]> {
|
||||
pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
|
||||
let mut idx: usize = 0;
|
||||
let mut depth = -1;
|
||||
|
||||
@ -57,13 +57,60 @@ pub fn get_args<'a>(args: &'a Vec<i32>) -> Vec<&[i32]> {
|
||||
arg_start_index = idx;
|
||||
}
|
||||
|
||||
return out_args;
|
||||
out_args
|
||||
}
|
||||
|
||||
pub fn evaluate_node(input_args: &[i32]) -> (i32, i32) {
|
||||
let node_type = input_args[0];
|
||||
|
||||
match node_type {
|
||||
0 => crate::nodes::math_node(&input_args[1..]),
|
||||
_ => (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn evaluate_args(input_args: &[i32]) -> (i32, i32) {
|
||||
let args = get_args(input_args);
|
||||
|
||||
let mut resolved: Vec<i32> = Vec::new();
|
||||
|
||||
for arg in args {
|
||||
if arg.len() == 1 {
|
||||
resolved.push(arg[0]);
|
||||
} else {
|
||||
let res = evaluate_args(arg);
|
||||
resolved.push(res.0);
|
||||
resolved.push(res.1);
|
||||
}
|
||||
}
|
||||
|
||||
if resolved.len() > 1 {
|
||||
evaluate_node(&resolved)
|
||||
} else {
|
||||
(resolved[0], resolved[1])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use crate::encoding::decode_float;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_resursive_evaluation() {
|
||||
let input = vec![0, 3, 0, 0, 0, 7, 0, 2, 0, 128, 0, 128, 1, 6, 0, 128];
|
||||
// this is an encoded version of a math node that multiplies 2 * 2
|
||||
// and another math node that adds 2 to that result
|
||||
// the numbers are f32 floats encoded as two i32's
|
||||
|
||||
let result = evaluate_args(&input);
|
||||
let decoded = decode_float(result.0, result.1);
|
||||
|
||||
assert_eq!(decoded, 6.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_example_input_a() {
|
||||
let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1];
|
||||
|
Loading…
Reference in New Issue
Block a user