aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/src/nodes.py
blob: ea9517d444d7d196bef5c05c81b13aa84707a101 (plain) (tree)















































































































































                                                                    
# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.

class ProgramNode:
    def __init__(self, stmt_list):
        self._stmt_list = stmt_list

    def execute(self):
        for stmt in self._stmt_list:
            stmt.execute()

_varmap = { }

class CompoundStatementNode:
    def __init__(self, stmt_list):
        self._stmt_list = stmt_list

    def execute(self):
        for stmt in self._stmt_list:
            stmt.execute()

class EmptyStatementNode:
    def execute(self):
        pass

class AssignmentNode:
    def __init__(self, identifier, arithm_expr):
        self._identifier = identifier
        self._arithm_expr = arithm_expr

    def execute(self):
        _varmap[str(self._identifier)] = self._arithm_expr.execute()
        return None

class PrintStatementNode:
    def __init__(self, arithm_expr):
        self._arithm_expr = arithm_expr

    def execute(self):
        print(self._arithm_expr.execute())
        return None

class IdentifierNode:
    def __init__(self, identifier):
        self._identifier = identifier

    def execute(self):
        return _varmap[str(self._identifier)]

class AdditionOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() + self._right.execute()

class SubtractionOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() + self._right.execute()

class MultiplicationOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() * self._right.execute()

class DivisionOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() / self._right.execute()

class IntegerNumberNode:
    def __init__(self, n):
        self._n = n

    def execute(self):
        return int(self._n)

class FloatingPointNumberNode:
    def __init__(self, n):
        self._n = n

    def execute(self):
        return float(self._n)

class IfStatementNode:
    def __init__(self, cond, body):
        self._cond = cond
        self._body = body

    def execute(self):
        if self._cond.execute():
            return self._body.execute()

class TrueNode:
    def execute(self):
        return True

class FalseNode:
    def execute(self):
        return False

class AndOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() and self._right.execute()

class OrOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() or self._right.execute()

class EqualsOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() == self._right.execute()

class NotEqualsOpNode:
    def __init__(self, left, right):
        self._left = left
        self._right = right

    def execute(self):
        return self._left.execute() != self._right.execute()