diff --git a/python_arborist.py b/python_arborist.py index b72e820e6ec7eb83b3b36e119b42209a8024caad..8485a5ea3f63c92d69da27191687d5abe81014db 100644 --- a/python_arborist.py +++ b/python_arborist.py @@ -309,22 +309,47 @@ print(serialized_tree_final) top_bit = 0x1000 + def deserializer(serialized_array): - idx = 0 - while (idx < len(serialized_array)): - new_element = serialized_array[idx] - number_subnodes = serialized_array[idx + 1] + list_of_nodes = [] + physical_to_logical = {} + physical_idx = 0 + logical_idx = 0 + while (physical_idx < 53): #len(serialized_array)): + new_element = serialized_array[physical_idx] + print("NEW ELEMENT IS:", new_element) + number_subnodes = serialized_array[physical_idx + 1] + print(" WITH", number_subnodes, "SUBNODES") if (number_subnodes < 1): - print("MALFORMED AT IDX", idx) + print("") + print("MALFORMED AT IDX", physical_idx) print("TOTAL LEN OF ARRAY IS", len(serialized_array)) break subnodes_array = [] - for sub_idx in range(idx + 2, idx + 2 + number_subnodes): - subnodes_array.append(serialized_array[sub_idx]) + for sub_idx in range(physical_idx + 2, physical_idx + 2 + number_subnodes): + if (serialized_array[sub_idx] & top_bit != 0): # index reference + backreference_physical_index = serialized_array[sub_idx] - top_bit + print(" BACKREFERENCE_INDEX IS", backreference_physical_index) + + #this_subnode = list_of_nodes[serialized_array] + subnodes_array.append(serialized_array[sub_idx]) + else: # new subnode altogether + print(" ABINITIO SUBNODE IS", serialized_array[sub_idx]) + subnodes_array.append(serialized_array[sub_idx]) + + + print(" AND THESE SUBNODES ARE:",subnodes_array) + print("") + + new_node = TreeNode(new_element, subnodes_array) + physical_to_logical[physical_idx] = logical_idx + physical_idx += 2 + number_subnodes + logical_idx += 1 + print(physical_to_logical) + + return new_node - print("NEW SUBNODES ARE:",subnodes_array) - idx += 2 + number_subnodes @@ -334,5 +359,9 @@ class TreeNode: self.subnodes = subnodes def __str__(self): - return str(self.language_element) -deserializer(serialized_tree_final) \ No newline at end of file + return str(self.language_element) + str(self.subnodes) + + + +the_tree = deserializer(serialized_tree_final) +print(the_tree) \ No newline at end of file