Exemplo n.º 1
0
    @rule
    def expr(expr, MUL, expr_1):
        return expr * expr_1

    @rule
    def expr(expr, POW, expr_1):
        return expr**expr_1


from pprint import pprint

table = {}

calc.interpret('x  =  8')
calc.interpret('y  =  x -  6 ')
calc.interpret('z  =  x ** y ')

calc.interpret(' (3) ')
calc.interpret(' x = 03 ')
calc.interpret(' y = 4 * x ** (2 + 1) * 2')

print(table)

# print(calc.dumps())
calc1 = LALR.loads(calc.dumps(), globals())

calc1.interpret(' w = x + 1')

print(table)
Exemplo n.º 2
0
# exec(ds, {}, ctx)
# pprint(ctx)

# lx_dp = p_sexp.lexer.dumps()
# print(lx_dp)
# lexer1 = Lexer.loads(lx_dp, globals())

# print(list(lexer1.tokenize(' 123  99 ')))

import warnings

with warnings.catch_warnings(record=True) as w:
    s = p_sexp.interpret('(a 123 (c (d)) %  & e)')
    assert len(w) == 1

assert s == ['a', 123, ['c', ['d']], 'e'], s

sexp_dp = p_sexp.dumps()

with open('sexp_dump.py', 'w') as o:
    o.write(sexp_dp)

# print(sexp_dp)
p_sexp1 = LALR.loads(sexp_dp, globals())

with warnings.catch_warnings(record=True) as w:
    s = p_sexp.interpret('(a & 123 (c (d)) %  & e)')
    assert len(w) == 2

assert s == ['a', 123, ['c', ['d']], 'e'], s