def test_expression(string): expression = expressions.parse(string) switched = expression.accept(Switcher()) assert_equal(compose(expression) .replace('and', '%OLDAND%') .replace('or', 'and') .replace('%OLDAND%', 'or'), compose(switched))
def compose(self, p, **x): """Produce the equivalent simple language as matched. Might have different whitespace and be otherwise formatted in a normalized fashion. """ return "{0} = {1}".format(compose(self[0]), compose(self[1]))
def compose(self, p, **x): """Produce the equivalent simple language as matched. Might have different whitespace and be otherwise formatted in a normalized fashion. """ s = "{0}".format(compose(self[0])) for x in self[1:]: s += " + {0}".format(compose(x)) return s
def test_if_tx1yx2y(self): """Test parsing if statement if (x > y) {...} else {...}.""" expected = "if (x > y)\n" expected += "{\n" expected += " x = 1;\n" expected += " y = x * 3;\n" expected += "}\n" expected += "else\n" expected += "{\n" expected += " x = 2;\n" expected += " y = 3 * x;\n" expected += "}" ast = parse(expected, p.If) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual(expected, c) self.assertEqual( If( GreaterThan(Variable('x'), Variable('y')), Sequence( Assign('x', Number(1)), Assign('y', Multiply(Variable('x'), Number(3)))), Sequence( Assign('x', Number(2)), Assign('y', Multiply(Number(3), Variable('x'))))), e)
def test_multiplication_15x(self): """Test parsing multiplication expression 1.5 * x.""" ast = parse("1.5 * x", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("1.5 * x", c) self.assertEqual(Multiply(Number(1.5), Variable('x')), e)
def test_multiplication_m2m1(self): """Test parsing multiplication expression -2 * -1.""" ast = parse("-2 * -1", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("-2 * -1", c) self.assertEqual(Multiply(Number(-2), Number(-1)), e)
def test_and_nxy(self): """Test parsing logical and expression !x && y.""" ast = parse("!x && y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("!x && y", c) self.assertEqual(And(Not(Variable('x')), Variable('y')), e)
def test_and_ft(self): """Test parsing logical and expression false && true.""" ast = parse("false && true", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("false && true", c) self.assertEqual(And(Boolean(False), Boolean(True)), e)
def test_lessthan_15x(self): """Test parsing less than expression 1.5 < x.""" ast = parse("1.5 < x", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("1.5 < x", c) self.assertEqual(LessThan(Number(1.5), Variable('x')), e)
def test_greaterthan_x25(self): """Test parsing greater than expression x > 2.5.""" ast = parse("x > 2.5", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x > 2.5", c) self.assertEqual(GreaterThan(Variable('x'), Number(2.5)), e)
def test_subtraction_15x(self): """Test parsing subtraction expression 1.5 - x.""" ast = parse("1.5 - x", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("1.5 - x", c) self.assertEqual(Subtract(Number(1.5), Variable('x')), e)
def test_variable_x_x(self): """Test parsing variable x_x.""" ast = parse("x_x", p.term_expression) c = compose(ast, indent=" ", autoblank=False) v = ast.to_simple() self.assertEqual("x_x", c) self.assertEqual(Variable('x_x'), v)
def test_boolean_f(self): """Test parsing boolean literal value false.""" ast = parse("false", p.term_expression) c = compose(ast, indent=" ", autoblank=False) b = ast.to_simple() self.assertEqual("false", c) self.assertEqual(Boolean(False), b)
def test_multiplication_xy(self): """Test parsing multiplication expression x * y.""" ast = parse("x * y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x * y", c) self.assertEqual(Multiply(Variable('x'), Variable('y')), e)
def test_variable_mixedcase(self): """Test parsing variable MixedCase.""" ast = parse("MixedCase", p.term_expression) c = compose(ast, indent=" ", autoblank=False) v = ast.to_simple() self.assertEqual("MixedCase", c) self.assertEqual(Variable('MixedCase'), v)
def test_subtraction_2m1m(self): """Test parsing subtraction expression -2 - -1.""" ast = parse("-2 - -1", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("-2 - -1", c) self.assertEqual(Subtract(Number(-2), Number(-1)), e)
def test_addition_xy(self): """Test parsing addition expression x + y.""" ast = parse("x + y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x + y", c) self.assertEqual(Add(Variable('x'), Variable('y')), e)
def test_subtraction_xy(self): """Test parsing subtraction expression x - y.""" ast = parse("x - y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x - y", c) self.assertEqual(Subtract(Variable('x'), Variable('y')), e)
def test_division_1525(self): """Test parsing division expression 1.5 / 2.5.""" ast = parse("1.5 / 2.5", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("1.5 / 2.5", c) self.assertEqual(Divide(Number(1.5), Number(2.5)), e)
def test_lessthan_2m1m(self): """Test parsing less than expression -2 - -1.""" ast = parse("-2 < -1", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("-2 < -1", c) self.assertEqual(LessThan(Number(-2), Number(-1)), e)
def test_division_m2m1(self): """Test parsing division expression -2 / -1.""" ast = parse("-2 / -1", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("-2 / -1", c) self.assertEqual(Divide(Number(-2), Number(-1)), e)
def test_lessthan_xy(self): """Test parsing less than expression x < y.""" ast = parse("x < y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x < y", c) self.assertEqual(LessThan(Variable('x'), Variable('y')), e)
def test_division_xy(self): """Test parsing division expression x / y.""" ast = parse("x / y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x / y", c) self.assertEqual(Divide(Variable('x'), Variable('y')), e)
def test_and_xf(self): """Test parsing logical and expression x && false.""" ast = parse("x && false", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x && false", c) self.assertEqual(And(Variable('x'), Boolean(False)), e)
def inline(self) -> str: """ Return an inline string format of the output source :return: """ return "{0}:{1}:{2}".format(self.amount, self.base, pypeg2.compose(self.condition, output.Condition))
def test_and_tt(self): """Test parsing logical and expression true && true.""" ast = parse("true && true", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("true && true", c) self.assertEqual(And(Boolean(True), Boolean(True)), e)
def test_not_false(self): """Test parsing logical negation expression !false.""" ast = parse("!false", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("!false", c) self.assertEqual(Not(Boolean(False)), e)
def test_assign_xyz(self): """Test parsing assignment statement.""" ast = parse("x = y + z;", p.Assign) c = compose(ast, indent=" ", autoblank=False) s = ast.to_simple() self.assertEqual("x = y + z;", c) self.assertEqual(Assign('x', Add(Variable('y'), Variable('z'))), s)
def test_number_int_2(self): """Test parsing integer literal value -123.""" ast = parse("-123", p.term_expression) c = compose(ast, indent=" ", autoblank=False) n = ast.to_simple() self.assertEqual("-123", c) self.assertEqual(Number(-123), n)
def test_and_1my(self): """Test parsing logical and expression -1 && y.""" ast = parse("-1 && y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("-1 && y", c) self.assertEqual(And(Number(-1), Variable('y')), e)
def test_assign_nxt(self): """Test parsing assignment statement.""" ast = parse("x = !true;", p.Assign) c = compose(ast, indent=" ", autoblank=False) s = ast.to_simple() self.assertEqual("x = !true;", c) self.assertEqual(Assign('x', Not(Boolean(True))), s)
def test_assign_x12(self): """Test parsing assignment statement.""" ast = parse("x = 1 + 2;", p.Assign) c = compose(ast, indent=" ", autoblank=False) s = ast.to_simple() self.assertEqual("x = 1 + 2;", c) self.assertEqual(Assign('x', Add(Number(1), Number(2))), s)
def test_and_xny(self): """Test parsing logical and expression x && !y.""" ast = parse("x && !y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x && !y", c) self.assertEqual(And(Variable('x'), Not(Variable('y'))), e)
def test_number_fp_1(self): """Test parsing floating point literal value 1.23.""" ast = parse("1.23", p.term_expression) c = compose(ast, indent=" ", autoblank=False) n = ast.to_simple() self.assertEqual("1.23", c) self.assertEqual(Number(1.23), n)
def test_or_nxy(self): """Test parsing logical or expression !x || y.""" ast = parse("!x || y", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("!x || y", c) self.assertEqual(Or(Not(Variable('x')), Variable('y')), e)
def test_or_xf(self): """Test parsing logical or expression x || false.""" ast = parse("x || false", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("x || false", c) self.assertEqual(Or(Variable('x'), Boolean(False)), e)
def test_or_ft(self): """Test parsing logical or expression false || true.""" ast = parse("false || true", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("false || true", c) self.assertEqual(Or(Boolean(False), Boolean(True)), e)
def test_not_1p1(self): """Test parsing logical negation expression !1.1.""" ast = parse("!1.1", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("!1.1", c) self.assertEqual(Not(Number(1.1)), e)
def test_not_x(self): """Test parsing logical negation expression !x.""" ast = parse("!x", p.Expression) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual("!x", c) self.assertEqual(Not(Variable('x')), e)
def test_while_tx1x2(self): """Test parsing while statement while (false) {x = 1;}.""" expected = "while (false)\n{\n x = 1;\n}" ast = parse(expected, p.While) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual(expected, c) self.assertEqual(While(Boolean(False), Assign('x', Number(1))), e)
def test_simple_or_condition(self): condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6))" result = pypeg2.parse(condition, output.Condition) self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(pypeg2.compose(result, output.Condition), condition)
def get_js_function_declaration(glsl_function, scope): glsl_type_str = peg.compose(glsl_function.type, type(glsl_function.type)) js_function = js.FunctionDeclaration(glsl_function.name, type_=f'/*{glsl_type_str}*/') js_function.documentation = glsl_function.documentation local_scope = scope.get_subscope(glsl_function) try: for glsl_parameter in glsl_function.parameters: if ('out' in glsl_parameter.qualifiers): raise throw_not_implemented_error(glsl_parameter, 'output reference parameters') glsl_type_str = peg.compose(glsl_parameter.type, type(glsl_function.type)) js_function.parameters.append(f'/*{glsl_type_str}*/') js_function.parameters.append(js.ParameterDeclaration(glsl_parameter.name)) for glsl_element in glsl_function.content: js_function.content.append(get_js(glsl_element, local_scope)) except (NotImplementedError, ValueError) as error: return f'/*Function "{glsl_function.name}" not available: {error}*/' return js_function
def test_csv_cltv_condition(self): condition = "(CSV(1654300) || (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && CLTV(2594024)))" result = pypeg2.parse(condition, Condition) self.assertEqual(result.left.left.time, "1654300") self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") self.assertEqual(result.left.right.op.name, "&&") self.assertEqual(result.left.right.right.timestamp, "2594024") self.assertEqual(pypeg2.compose(result, Condition), condition)
def test_if_tx1x2(self): """Test parsing if statement if (true) {x = 1;} else {x = 2;}.""" expected = "if (true)\n{\n x = 1;\n}\n" expected += "else\n{\n x = 2;\n}" ast = parse(expected, p.If) c = compose(ast, indent=" ", autoblank=False) e = ast.to_simple() self.assertEqual(expected, c) self.assertEqual( If(Boolean(True), Assign('x', Number(1)), Assign('x', Number(2))), e)
def test_complex_condition(self): condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) || (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) && XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))" result = pypeg2.parse(condition, output.Condition) self.assertEqual(result.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd") self.assertEqual(result.left.op.name, "||") self.assertEqual(result.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV") self.assertEqual(result.left.right.op.name, "&&") self.assertEqual(result.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6") self.assertEqual(pypeg2.compose(result, output.Condition), condition)
def convert_text(input_text, input_handling='omit'): ''' "convert_text" is a pure function that performs a transformation on a string containing glsl code, then returns transformed output. It may run convert_glsl behind the scenes, and may also perform additional string based transformations, such as string substitutions or regex replacements ''' input_glsl = peg.parse(input_text, glsl.code) output_glsl = convert_glsl(input_glsl, input_handling=input_handling) output_text = peg.compose(output_glsl, glsl.code, autoblank=False) return output_text
def convert_text(input_text): ''' "convert_text" is a pure function that performs a transformation on a string containing glsl code, then returns transformed output. It may run convert_glsl behind the scenes, and may also perform additional string based transformations, such as appending utility functions or performing simple string substitutions ''' input_glsl = peg.parse(input_text, glsl.code) # output_glsl = convert_glsl(input_glsl) output_text = peg.compose(input_glsl, glsl.code, autoblank=False) return output_text
def test_fromcompact2(self): tx = Transaction.from_compact("zeta_brousouf", tx_compact_2) self.assertEqual(tx.version, 2) self.assertEqual(tx.currency, "zeta_brousouf") self.assertEqual(len(tx.issuers), 1) self.assertEqual(len(tx.inputs), 1) self.assertEqual(len(tx.unlocks), 1) self.assertEqual(len(tx.outputs), 2) self.assertEqual(tx.issuers[0], "GNPdPNwSJAYw7ixkDeibo3YpdELgLmrZ2Q86HF4cyg92") self.assertEqual(tx.inputs[0].source, 'D') self.assertEqual(tx.inputs[0].origin_id, "GNPdPNwSJAYw7ixkDeibo3YpdELgLmrZ2Q86HF4cyg92") self.assertEqual(tx.inputs[0].index, 471) self.assertEqual(tx.unlocks[0].index, 0) self.assertEqual(str(tx.unlocks[0].parameters[0]), "SIG(0)") self.assertEqual(tx.outputs[0].amount, 90) self.assertEqual(tx.outputs[0].base, 0) self.assertEqual( pypeg2.compose(tx.outputs[0].conditions, output.Condition), "SIG(5zDvFjJB1PGDQNiExpfzL9c1tQGs6xPA8mf1phr3VoVi)") self.assertEqual(type(tx.outputs[0].conditions.left), output.SIG) self.assertEqual(tx.outputs[1].amount, 10) self.assertEqual(tx.outputs[1].base, 0) self.assertEqual( pypeg2.compose(tx.outputs[1].conditions, output.Condition), "SIG(GNPdPNwSJAYw7ixkDeibo3YpdELgLmrZ2Q86HF4cyg92)") self.assertEqual(type(tx.outputs[1].conditions.left), output.SIG) self.assertEqual( tx.signatures[0], "XDQeEMcJDd+XVGaFIZc8d4kKRJgsPuWAPVNG5UKNk8mDZx2oE1kTP/hbxiFx6yDouBELCswuf/X6POK9ES7JCA==" )