def test_func(self): func = parse('func lambda (a, b, c){a + b - c}') call = parse('lambda(15, 94, 2)') eval_func = CscriptTransformer().transform(func) eval_call = CscriptTransformer().transform(call) env = ChainMap() assert eval(eval_func, env) is None assert eval(eval_call, env) == 107
def test_math_composto(self): assert eval(parse_src('2 minuas 2 adde 3 adde 4'), e) == -7 assert eval(parse_src('2 multiplica 2 minuas 3'), e) == -2 assert eval(parse_src('posthac die 2 minuas 2 usque huc adde 99'), e) == 99 assert eval( parse_src( 'posthac die posthac die 2 adde 2 usque huc minuas 8 usque huc multiplica 4' ), e) == -16
def test_if(self): value_if = parse('if (T){2 + 2} else if (T){2 - 2}') value_Eq = parse('if (2 == 2){1 + 2}') value_Gt = parse('if (3 > 1){3 + 5}') value_Le = parse('if (4 <= 5){2 + 5}') value_Lt = parse('if (3 < 5){3 * 1}') value_Ge = parse('if (3 >= 4){2 ^ 3}') value_Ne = parse('if (3 != 1){1 + 2} else { 1 + 1 }') value_ternario = parse('asd = 3 > 0 ? 1 : 2') if_if = CscriptTransformer().transform(value_if) if_Eq = CscriptTransformer().transform(value_Eq) if_Gt = CscriptTransformer().transform(value_Gt) if_Le = CscriptTransformer().transform(value_Le) if_Lt = CscriptTransformer().transform(value_Lt) if_Ge = CscriptTransformer().transform(value_Ge) if_Ne = CscriptTransformer().transform(value_Ne) if_ternario = CscriptTransformer().transform(value_ternario) env = {Symbol('b'): 2} print_ = parse('b = b == 2 ? 6 : 0') print_eval = CscriptTransformer().transform(print_) res = eval(print_eval, env) assert if_if == [ Symbol.IF, True, [Symbol.ADD, 2.0, 2.0], [[True, [Symbol.SUB, 2.0, 2.0]]] ] assert if_Eq == [ Symbol.IF, [Symbol.EQ, 2.0, 2.0], [Symbol.ADD, 1.0, 2.0] ] assert if_Gt == [Symbol.IF, [Symbol.GT, 3.0, 1.0], [Symbol.ADD, 3, 5]] assert if_Le == [Symbol.IF, [Symbol.LE, 4.0, 5.0], [Symbol.ADD, 2, 5]] assert if_Lt == [Symbol.IF, [Symbol.LT, 3.0, 5.0], [Symbol.MUL, 3, 1]] assert if_Ge == [Symbol.IF, [Symbol.GE, 3.0, 4.0], [Symbol.POW, 2, 3]] assert if_Ne == [ Symbol.IF, [Symbol.NEQ, 3.0, 1.0], [Symbol.ADD, 1, 2], [[Symbol.ADD, 1, 1]] ] assert if_ternario == [ Symbol.TERN, Symbol('asd'), [Symbol.GT, 3, 0], 1, 2 ] assert env[Symbol('b')] == 6 assert eval(if_if, {}) == 4 assert eval(if_Eq, {}) == 3 assert eval(if_Gt, {}) == 8 assert eval(if_Le, {}) == 7 assert eval(if_Lt, {}) == 3 assert eval(if_Ne, {}) == 3 assert eval(if_Ge, {}) is None env = {'asd': 2} assert eval(if_ternario, env) == None assert env[Symbol('asd')] == 1
def main(file, parse): from pprint import pprint e = env() if file is None: src = "<not empty>" while src: src = input("Latym> ") if src: ast = parse_src(src) ##pprint(ast) try: print(eval(ast, e)) except Exception as exc: print(exc.__class__.__name__, exc) else: ast = parse_src(file.read()) value = eval(ast, env())
def test_binop(self): value_sum = parse('1 + 2') value_sub = parse('3 - 2') value_mul = parse('12 * 3') value_div = parse('14 / 2') value_pow = parse('2 ^ 10') soma = CscriptTransformer().transform(value_sum) sub = CscriptTransformer().transform(value_sub) mul = CscriptTransformer().transform(value_mul) div = CscriptTransformer().transform(value_div) pow_ = CscriptTransformer().transform(value_pow) assert soma == [Symbol.ADD, 1.0, 2.0] assert sub == [Symbol.SUB, 3.0, 2.0] assert mul == [Symbol.MUL, 12.0, 3.0] assert div == [Symbol.DIV, 14, 2] assert pow_ == [Symbol.POW, 2, 10] assert eval(div, {}) == 7 assert eval(soma, {}) == 3 assert eval(sub, {}) == 1 assert eval(mul, {}) == 36 assert eval(pow_, {}) == 1024
def test_loop(self): loop = parse(""" a = 2; for(x = 3; x <= 10; x = x + 1){ a = a + x }; a """) loop_transform = CscriptTransformer().transform(loop) #while while_p = parse(""" a = 1; while(a < 10){ a = a + 1 } """) while_trans = CscriptTransformer().transform(while_p) env = {} assert eval(loop_transform, env) == 54 env = {} eval(while_trans, env) assert env[Symbol('a')] == 10
def test_numbers(self): assert eval(parse_src('42'), e) == 42 assert eval(parse_src('3.14'), e) == 3.14 assert eval(parse_src('-3.14'), e) == -3.14
def test_if(self): assert eval(parse_src('si 10 maior quam 2 fac sic 3 adde 3 cis'), e) == 6 assert eval(parse_src('si 10 minus quam 20 fac sic 3 adde 10 cis'), e) == 13
def test_var_assign(self): eval(parse_src('imperium aequipar 20'), e) eval(parse_src('mundi aequipar 10'), e) assert eval(parse_src('imperium'), e) == 20 assert eval(parse_src('imperium adde 30'), e) == 50 assert eval(parse_src('mundi adde 30'), e) == 40 assert eval(parse_src('imperium adde mundi'), e) == 30 eval(parse_src('imperium aequipar mundi'), e) assert eval(parse_src('imperium'), e) == 10
def test_math(self): assert eval(parse_src('2 adde 2'), e) == 4 assert eval(parse_src('2 minuas 2'), e) == 0 assert eval(parse_src('3 multiplica 2'), e) == 6 assert eval(parse_src('2 divide 2'), e) == 1 assert eval(parse_src('2 recide 2'), e) == 0
def test_strings(self): assert eval(parse_src('"foobar"'), e) == "foobar" assert eval(parse_src('"foo bar"'), e) == "foo bar" assert eval(parse_src('"esse é um teste"'), e) == "esse é um teste"
def test_atomic(self): assert eval(parse_src('verum'), e) is True assert eval(parse_src('falsus'), e) is False assert parse_src('adde') == Symbol('adde')
from parser import parse as parse_src from runtime import eval, env, Symbol run = lambda src, env=None: eval(parse_src(src), env) e = env() class TestLanguage: def test_numbers(self): assert eval(parse_src('42'), e) == 42 assert eval(parse_src('3.14'), e) == 3.14 assert eval(parse_src('-3.14'), e) == -3.14 def test_atomic(self): assert eval(parse_src('verum'), e) is True assert eval(parse_src('falsus'), e) is False assert parse_src('adde') == Symbol('adde') def test_strings(self): assert eval(parse_src('"foobar"'), e) == "foobar" assert eval(parse_src('"foo bar"'), e) == "foo bar" assert eval(parse_src('"esse é um teste"'), e) == "esse é um teste" def test_math(self): assert eval(parse_src('2 adde 2'), e) == 4 assert eval(parse_src('2 minuas 2'), e) == 0 assert eval(parse_src('3 multiplica 2'), e) == 6 assert eval(parse_src('2 divide 2'), e) == 1 assert eval(parse_src('2 recide 2'), e) == 0