Mapytex/mapytex/calculus/core/evaluate.py

47 lines
782 B
Python
Raw Normal View History

2018-01-21 15:16:08 +00:00
#! /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