from emis_funky_funktions import * from typing import Collection, Sequence, TypeAlias from ir import Expression from dataclasses import dataclass from operator import add def evaluate(expr: Expression) -> Expression: """ >>> funktion = Function(( ... (SPattern(NamePattern('n')), ( ... Application(( ... Variable('+'), ... Variable('n'), ... Variable('n'), ... )) ... )), ... (NamePattern('0'), ( ... Int(1312) ... )), ... )) >>> evaluate(Application((funktion, Int(3)))) 4 >>> evaluate(Application((funktion, Int(0)))) 1312 """ if expr.is_value(): return expr else: match expr.step(): case Some(next): return evaluate(next) case None: raise AssertionError('Evaluate called on a value which cannot step:', expr) raise Exception('Unreachable') if __name__ == '__main__': import doctest doctest.testmod()