JSON-Lang/silly_thing.py

40 lines
913 B
Python
Raw Normal View History

2023-03-08 01:25:26 +00:00
from emis_funky_funktions import *
from typing import Collection, Sequence, TypeAlias
2023-03-08 13:35:21 +00:00
from ir import Expression
2023-03-08 01:25:26 +00:00
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()