예제 #1
0
from __future__ import print_function, absolute_import
from alge import (datatype, Case, of, MissingCaseError, LazyCase,
                  _PatternParser, PatternSyntaxError)
import unittest

Color = datatype('Color', ['red', 'green', 'blue'])
Record = datatype('Record', ['color', 'intensity'])


class MyCase(Case):
    @of("Record(Color(r, g, b), intensity)")
    def record(self, r, g, b, intensity):
        return (r + g + b) * intensity

    @of("Color(r, g, _)")
    def red_green(self, r, g):
        return r + g


class ElseCase(Case):
    @of("Record(c, i)")
    def record(self, c, i):
        return c, i

    def otherwise(self, value):
        return ("Bad", value)


class StateCase(Case):
    @of("Record(c, i)")
    def record(self, c, i):
예제 #2
0
파일: speed.py 프로젝트: B-Rich/pyalge
from __future__ import print_function
from alge import Case, of, datatype
from timeit import repeat

Op = datatype("Op", ['a', 'b'])

class Do(Case):
	@of("Op(a, b)")
	def op(self, a, b):
		return a + b


class Visitor(object):
	def visit(self, val):
		fn = getattr(self, "visit_%s" % type(val).__name__)
		return fn(val)
	def visit_Op(self, val):
		return val.a + val.b


args = 1, 2
op = Op(*args)
t_alge = min(repeat(lambda: Do(op), repeat=3, number=1000))
t_vis = min(repeat(lambda: Visitor().visit(op), repeat=3, number=1000))
print('t_alge', t_alge)
print('t_vis ', t_vis)

예제 #3
0
from __future__ import print_function
from alge import Case, of, datatype
from timeit import repeat

Op = datatype("Op", ['a', 'b'])


class Do(Case):
    @of("Op(a, b)")
    def op(self, a, b):
        return a + b


class Visitor(object):
    def visit(self, val):
        fn = getattr(self, "visit_%s" % type(val).__name__)
        return fn(val)

    def visit_Op(self, val):
        return val.a + val.b


args = 1, 2
op = Op(*args)
t_alge = min(repeat(lambda: Do(op), repeat=3, number=1000))
t_vis = min(repeat(lambda: Visitor().visit(op), repeat=3, number=1000))
print('t_alge', t_alge)
print('t_vis ', t_vis)
예제 #4
0
"""
Function fusion example.

Fusing map operations together.
"""
from __future__ import print_function, absolute_import
from alge import datatype, Case, of

Map = datatype("Map", ["func", "value"])
Fuse = datatype("Fuse", ["first", "second"])
Func = datatype("Func", ["name"])
Var = datatype("Var", ["name"])


class fuse(Case):
    @of("Map(fa, Map(fb, val))")
    def fuse_map(self, fa, fb, val):
        print("fusing", self.value)
        return fuse(Map(Fuse(fb, fa), fuse(val)))

    def otherwise(self, value):
        return value


def main():
    expr = Map(Func("mul"), Map(Func("sub"), Map(Func("add"), Var("x"))))
    print("original", expr)
    fused = fuse(expr)
    print("fused", fused)
    expect = Map(Fuse(Func("add"), Fuse(Func("sub"), Func("mul"))), Var("x"))
    assert expect == fused
예제 #5
0
"""
A simple calculator example that implements addition and subtraction.

Notice how the more specific pattern is matched.
"""

from __future__ import print_function, absolute_import
from alge import datatype, Case, of

Add = datatype("Add", ["lhs", "rhs"])
Sub = datatype("Sub", ["lhs", "rhs"])
Num = datatype("Num", ["val"])


class calc(Case):
    @of("Add(Num(a), Num(b))")
    def add_num(self, a, b):
        print("add_num", self.value)
        return a + b

    @of("Sub(Num(a), Num(b))")
    def sub_num(self, a, b):
        print("sub_num", self.value)
        return a - b

    @of("Add(a, b)")
    def add_expr(self, a, b):
        print("add_expr", self.value)
        return calc(a) + calc(b)

    @of("Sub(a, b)")