Пример #1
0
def _logical(op):
    return lexeme(pc.char('(').skip(
                  lexeme(string(op.call_name)).skip(
                  many_list(expr).bind(lambda exprs:
                  pc.char(')').skip(
                  inject(op(*exprs)))))))
Пример #2
0
class _expr(pc.Parser):
    name = '_expr'

    def parse(self,s):
        return pc.choice(immediate, primitive, conditional, logical).parse(s)

expr = _expr().drop_whitespace().with_name('expr')

identifier = concat(extended_alph_char, many(choice(extended_alph_char, digit))).drop_whitespace() \
    .with_name('identifier')

primitive = pc.char('(').skip(
    identifier.bind(lambda i: cond(i in Primitive.__registry__.keys(),
    many_list(expr).bind(lambda args:
    pc.char(')').skip(
    inject(Primitive[i](*args)))))))\
    .with_name('primitive')


conditional = pc.char('(').skip(
    lexeme(string('if')).skip(
    expr.bind(lambda condition:
    expr.bind(lambda if_branch:
    expr.bind(lambda else_branch:
    pc.char(')').skip(
    inject(If(condition, if_branch, else_branch))))))))\
    .with_name('conditional')

def _logical(op):
    return lexeme(pc.char('(').skip(