47 lines
782 B
Python
47 lines
782 B
Python
#! /usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# vim:fenc=utf-8
|
|
#
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
#
|
|
# Distributed under terms of the MIT license.
|
|
|
|
"""
|
|
Evaluating a binary tree
|
|
"""
|
|
|
|
from .operator import OPERATORS
|
|
|
|
def compute(node, left_v, right_v):
|
|
"""
|
|
Computing a node
|
|
|
|
:example:
|
|
|
|
>>> compute("+", 1, 2)
|
|
3
|
|
>>> compute("-", 1, 2)
|
|
-1
|
|
>>> compute("-", None, 2)
|
|
-2
|
|
|
|
"""
|
|
op = OPERATORS[node]
|
|
lv = left_v
|
|
rv = right_v
|
|
if lv is None:
|
|
return op["_operate"](rv)()
|
|
|
|
try:
|
|
return op["operate"](lv)(rv)
|
|
except NotImplemented:
|
|
return op["roperate"](rv)(lv)
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
# Reglages pour 'vim'
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
# cursor: 16 del
|