Last active
September 7, 2023 04:30
-
-
Save narskidan/aa5ae947167c4ab7a8673a5f21b989a3 to your computer and use it in GitHub Desktop.
Mini Nock interpret
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BTree: | |
def __init__(self, l, r): | |
self.raw_tree = (l, r) | |
self.left = BTree(*l) if type(l) == tuple else l | |
self.right = BTree(*r) if type(r) == tuple else r | |
def __str__(self): | |
return 'BTreeNode*' | |
def index(self, desired_i): | |
i = 1 | |
current_row = [(i, self)] | |
all_rows = [current_row] | |
while i < desired_i: | |
new_row = [] | |
for col in current_row: | |
if not issubclass(type(col[1]), BTree): | |
col = (col[0], BTree(None, None)) | |
i += 1 | |
new_row.append((i, col[1].left)) | |
i += 1 | |
new_row.append((i, col[1].right)) | |
current_row = new_row | |
all_rows.append(current_row) | |
for col in current_row: | |
if col[0] == desired_i: | |
if col[1] is None: | |
raise IndexError('Asked for empty index in BTree') | |
return col[1] | |
class Subject(BTree): | |
def apply(self, formula): | |
return formula.eval(self) | |
class Formula(BTree): | |
def eval(self, subject): | |
self.subject = subject | |
command = self.left | |
if type(self.right) is BTree: | |
if type(self.right.left) == int: | |
self.right = Formula(*self.right.raw_tree) | |
self.right = self.right.eval(subject) | |
else: | |
non_executable_pair = self.right | |
left = non_executable_pair.left | |
right = non_executable_pair.right | |
left = Formula(*left.raw_tree) | |
non_executable_pair.left = left.eval(subject) | |
right = Formula(*right.raw_tree) | |
non_executable_pair.right = right.eval(subject) | |
if command == 0: | |
return self.zero(self.right) | |
if command == 50: | |
return self.fifty(self.right) | |
return subject | |
# checks equality of two values | |
def fifty(self, values): | |
return int(values.left != values.right) | |
# get value at index | |
def zero(self, index): | |
return self.subject.index(index) | |
tree = BTree(*(((11, 12), (99, 22)), ((500, 16), (22, 33)))) | |
print(tree.index(15)) | |
# Subject: [5 [2 4]] | |
subject = Subject(5, (2, 4)) | |
# Formula: [50 [[0 6] [0 2]]] | |
formula = Formula(50, ((0, 6), (0, 2))) | |
# 1 | |
result = subject.apply(formula) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment