def to_expression(expr): "Transform text/expression/condition into Expression type" if isinstance(expr, (ExpressionAST, ArithmeticExpression)): return expr else: return ArithmeticExpression(parse_expression(expr))
def parse(code): """Parses the input string as Layout variety language. Args: code: Code which is valid under the Layout variety specifications. Returns: Dictionary of windows. Each window is of the Widget type. If the first "window" of the code has no assignment into a name, the default name "main_window" is assigned to it. """ global sub_programs, global_variables sub_programs = {} global_variables = {} # Check that we have at least one sub_program here. If not, make the only # program be "main_window" if code.find("<-") == -1 or not re.match(tt_NAME, code.split("<-", 1)[0].strip()): code = "main_window <- " + code return parser.parse(code.strip(), lexer=lexer) if __name__ == '__main__': test_code = """ main_window <- (label : 30x30)[a=5+xy9,b={(a) => (f(a)), otherwise (0)}] | ( (textbox) -------------- ()) """ print parse_expression("5*a+9") print parse(test_code)
"""Parses the input string as Layout variety language. Args: code: Code which is valid under the Layout variety specifications. Returns: Dictionary of windows. Each window is of the Widget type. If the first "window" of the code has no assignment into a name, the default name "main_window" is assigned to it. """ global sub_programs, global_variables sub_programs = {} global_variables = {} # Check that we have at least one sub_program here. If not, make the only # program be "main_window" if code.find("<-") == -1 or not re.match(tt_NAME, code.split("<-", 1)[0].strip()): code = "main_window <- " + code return parser.parse(code.strip(), lexer=lexer) if __name__ == '__main__': test_code = """ main_window <- (label : 30x30)[a=5+xy9,b={(a) => (f(a)), otherwise (0)}] | ( (textbox) -------------- ()) """ print parse_expression("5*a+9") print parse(test_code)