From ffc3e3423897d77b5e43af8ed567b544f00cf526 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 6 May 2015 06:00:26 +0300 Subject: initial commit --- src/nodes.py | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/nodes.py (limited to 'src/nodes.py') diff --git a/src/nodes.py b/src/nodes.py new file mode 100644 index 0000000..ea9517d --- /dev/null +++ b/src/nodes.py @@ -0,0 +1,144 @@ +# Copyright 2015 Egor Tensin +# 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() -- cgit v1.2.3