~stacyharper/bonsai

45074e30df614edd69944c1ec54281d5cb45e50e — Stacy Harper 4 months ago 4b6df95 v0.2.0
Drop "hole" branches in favor of empty "context" ones
M bonsai/tree/json_load.ha => bonsai/tree/json_load.ha +6 -17
@@ 122,7 122,7 @@ fn load_tree_branch_exec(object: json::object) (branch | error) = {
	};
};

fn load_tree_branch_hole(object: json::object) (branch | error) = {
fn load_tree_branch_context(object: json::object) (branch | error) = {
	let branches = match (load_branch_childs(object)) {
	case let branches: []branch =>
		yield branches;


@@ 130,18 130,16 @@ fn load_tree_branch_hole(object: json::object) (branch | error) = {
		return err;
	};

	return branch_hole {
		branches = branches,
	};
};

fn load_tree_branch_context(object: json::object) (branch | error) = {
	let contexts = match(json::get(&object, "contexts")) {
	case let value: *json::value =>
		yield value;
	case void =>
		return missing_contexts;
		return branch_context {
			branches = branches,
			...
		};
	};

	let contexts = match(*contexts) {
	case let value: json::object =>
		yield value;


@@ 171,13 169,6 @@ fn load_tree_branch_context(object: json::object) (branch | error) = {
		};
	};

	let branches = match (load_branch_childs(object)) {
	case let branches: []branch =>
		yield branches;
	case let err: error =>
		return err;
	};

	return branch_context {
		branches = branches,
		context_elements = context_elements,


@@ 209,8 200,6 @@ fn load_tree_branch(object: json::object) (branch | error) = {
		return load_tree_branch_delay(object);
	case "exec" =>
		return load_tree_branch_exec(object);
	case "hole" =>
		return load_tree_branch_hole(object);
	case =>
		return unsupported_type;
	};

M bonsai/tree/json_test.ha => bonsai/tree/json_test.ha +2 -2
@@ 33,8 33,8 @@ use strings;
	assert(len(subject.branches) == 3);
	assert(subject.event_name == "typical_event");

	assert(subject.branches[1] is branch_hole);
	let subject2 = subject.branches[1] as branch_hole;
	assert(subject.branches[1] is branch_context);
	let subject2 = subject.branches[1] as branch_context;
	assert(len(subject2.branches) == 0);

	assert(subject.branches[0] is branch_delay);

M bonsai/tree/json_write.ha => bonsai/tree/json_write.ha +14 -20
@@ 11,19 11,22 @@ export fn write_tree(
		fmt::fprint(handle, "{")!;
		match(branches[i]) {
		case let branch: branch_context =>
			fmt::fprint(handle, "\"type\":\"context\",\"contexts\":{")!;
			for (let y = 0z; y < len(branch.context_elements); y += 1) {
				fmt::fprintf(
					handle,
					"\"{}\":\"{}\"",
					branch.context_elements[y].0,
					branch.context_elements[y].1
				)!;
				if (y < len(branch.context_elements) - 1) {
					fmt::fprint(handle, ",")!;
			fmt::fprint(handle, "\"type\":\"context\"")!;
			if (0 < len(branch.context_elements)) {
				fmt::fprint(handle, ",\"contexts\":{")!;
				for (let y = 0z; y < len(branch.context_elements); y += 1) {
					fmt::fprintf(
						handle,
						"\"{}\":\"{}\"",
						branch.context_elements[y].0,
						branch.context_elements[y].1
					)!;
					if (y < len(branch.context_elements) - 1) {
						fmt::fprint(handle, ",")!;
					};
				};
				fmt::fprint(handle, "}")!;
			};
			fmt::fprint(handle, "}")!;
			if (0 < len(branch.branches)) {
				fmt::fprint(handle, ",\"branches\":")!;
				write_tree(handle, branch.branches);


@@ 68,15 71,6 @@ export fn write_tree(
				fmt::fprint(handle, ",\"branches\":")!;
				write_tree(handle, branch.branches);
			};
		case let branch: branch_hole =>
			fmt::fprintf(
				handle,
				"\"type\":\"hole\"",
			)!;
			if (0 < len(branch.branches)) {
				fmt::fprint(handle, ",\"branches\":")!;
				write_tree(handle, branch.branches);
			};
		};
		fmt::fprint(handle, "}")!;
		if (i < len(branches) - 1) {

M bonsai/tree/test_voyager.ha => bonsai/tree/test_voyager.ha +6 -4
@@ 508,23 508,25 @@ let tree7: []branch = [
				],
				...
			},
			branch_hole {
			branch_context {
				branches = [
					branch_event {
						event_name = "target_1",
						...
					},
				]
				],
				...
			}
		]
	},
	branch_hole {
	branch_context {
		branches = [
			branch_event {
				event_name = "target_unwanted",
				...
			},
		]
		],
		...
	},
];


M bonsai/tree/type.ha => bonsai/tree/type.ha +0 -5
@@ 23,16 23,11 @@ export type branch_delay = struct {
	delay_duration: int,
};

export type branch_hole = struct {
	branches: []branch,
};

export type branch = (
	branch_event |
	branch_context |
	branch_exec |
	branch_delay |
	branch_hole |
);

export type voyager_action_event_received = struct {

M bonsai/tree/voyager.ha => bonsai/tree/voyager.ha +0 -4
@@ 91,10 91,6 @@ export fn voyager_forward_available_branches(voyager: *voyager) void = {

				return voyager_forward_available_branches(voyager);
			};
		case let hole_branch: branch_hole =>
			voyager.available_branches = hole_branch.branches;

			return voyager_forward_available_branches(voyager);
		case branch =>
			continue;
		};

M fixtures/basic_tree.json => fixtures/basic_tree.json +2 -2
@@ 24,10 24,10 @@
            ]
          },
          {
            "type": "hole"
            "type": "context"
          },
          {
            "type": "hole",
            "type": "context",
            "branches": [
              {
                "type": "exec",