def handle_conditional_statements(if_else_block, instance_vars, stack):
    has_else_clause = verify_if_else_syntax(if_else_block)
    
    # Tokenize if-else block: split conditions from statements
    tokens = clean_up_list_elems(flatten_list(re.split("\s*\}\s*else if\s*\(", if_else_block))) # Split by } else if (
    tokens = clean_up_list_elems(flatten_list(list(map(lambda elem: re.split("\s*if\s*\(", elem), tokens))))    # Split by if (
    tokens = clean_up_list_elems(flatten_list(list(map(lambda elem: re.split("\s*\}\s*else\s*\{", elem), tokens)))) # Split by } else {, insert True as condition
    if has_else_clause:
        tokens.insert(-1, "True")
    tokens = clean_up_list_elems(flatten_list(list(map(lambda elem: re.split("\s*\)\s*\{", elem), tokens))))    # Split  by ) {
    tokens = clean_up_list_elems(flatten_list(list(map(lambda elem: re.split("\}", elem), tokens))))    # Split by }
    
    # Split tokens into list of conditions and statements
    conditions_list, statements_list = [], []
    for index in range(0, len(tokens)):
        if index % 2 == 0:  # Even, is a condition.
            conditions_list.append(tokens[index])
        else:
            statements_list.append(tokens[index])
    
    assert len(conditions_list) == len(statements_list), "conditions_list and statements_list are of different lengths"
    
    # Check the conditions; if True, return the associated statements
    for index in range(0, len(conditions_list)):
        curr_condition = evaluate_expression(conditions_list[index])
        if type(curr_condition) is not bool:
            raise InvalidIfElseBlockException("Condition parsed was not a boolean expression.  Condition was: " + str(curr_condition))
        if curr_condition:
            return statements_list[index]
        
    return None
def handle_conditional_statements(if_else_block, instance_vars, stack):
    has_else_clause = verify_if_else_syntax(if_else_block)

    # Tokenize if-else block: split conditions from statements
    tokens = clean_up_list_elems(
        flatten_list(re.split("\s*\}\s*else if\s*\(",
                              if_else_block)))  # Split by } else if (
    tokens = clean_up_list_elems(
        flatten_list(
            list(map(lambda elem: re.split("\s*if\s*\(", elem),
                     tokens))))  # Split by if (
    tokens = clean_up_list_elems(
        flatten_list(
            list(map(lambda elem: re.split("\s*\}\s*else\s*\{", elem),
                     tokens))))  # Split by } else {, insert True as condition
    if has_else_clause:
        tokens.insert(-1, "True")
    tokens = clean_up_list_elems(
        flatten_list(
            list(map(lambda elem: re.split("\s*\)\s*\{", elem),
                     tokens))))  # Split  by ) {
    tokens = clean_up_list_elems(
        flatten_list(list(map(lambda elem: re.split("\}", elem),
                              tokens))))  # Split by }

    # Split tokens into list of conditions and statements
    conditions_list, statements_list = [], []
    for index in range(0, len(tokens)):
        if index % 2 == 0:  # Even, is a condition.
            conditions_list.append(tokens[index])
        else:
            statements_list.append(tokens[index])

    assert len(conditions_list) == len(
        statements_list
    ), "conditions_list and statements_list are of different lengths"

    # Check the conditions; if True, return the associated statements
    for index in range(0, len(conditions_list)):
        curr_condition = evaluate_expression(conditions_list[index])
        if type(curr_condition) is not bool:
            raise InvalidIfElseBlockException(
                "Condition parsed was not a boolean expression.  Condition was: "
                + str(curr_condition))
        if curr_condition:
            return statements_list[index]

    return None
示例#3
0
def handle_while(block, instance_vars, stack):
    validate_while_loop_syntax(block)
    
    tokens = clean_up_list_elems(flatten_list(re.split("while\s*\(", block)))
    tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\)\s*\{", x), tokens))))
    tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\}", x), tokens))))
    print("tokens: " + str(tokens))
    
    assert len(tokens) == 2, "There should be 2 tokens, but there are " + str(len(tokens))
    
    condition, statements = tokens[0], tokens[1]
    
    while evaluate_expression(condition):
        parse_eval(statements, instance_vars, stack)    # We will NOT support different scoping for variables inside.
        
    return  # Call parse_eval with instance_vars
示例#4
0
def update_variable(statement, instance_vars, stack, just_declared=False):
    from javarepl import evaluate_expression
    tokens = tokenize_assignment_statement(statement)
    if len(tokens) != 2:
        raise InvalidAssignmentException("Statement provided was: " + statement)
    variable_name, variable_value = tokens[0].strip(), tokens[1].strip()
    variable_frame = get_variable_frame(variable_name, instance_vars, stack)
    
    if variable_frame == None:
        raise InvalidAssignmentException("")
    
    # Compute the result to update the value of expression with.
    result = evaluate_expression(variable_value)
    result_type = type(result)
    stored_variable_type = variable_frame[variable_name].get_datatype()
    stored_variable_value = variable_frame[variable_name].get_value()
    
    # Check to ensure result datatype matches variable datatype
    verify_result_datatype(result, variable_name, just_declared, stored_variable_type, instance_vars, stack)

    # Now that we know it is  valid, write to variable.
    variable_frame[variable_name].set_value(result)
示例#5
0
def handle_for(block, instance_vars, stack):
    validate_for_loop_syntax(block)
    
    tokens = clean_up_list_elems(flatten_list(re.split("for\s*\(", block)))
    tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\)\s*\{", x), tokens))))
    tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\}", x), tokens))))
    
    print("tokens: " + str(tokens))
    tokens = tokens[0].split(";") + [tokens[1]]
    print("tokens: " + str(tokens))
    initialize, condition, update, statements = tokens
    print("tokens: " + str(tokens))
    
    assign_variable(initialize, instance_vars, stack)
    evaluated_condition = evaluate_expression(condition)
    if type(evaluated_condition) is not bool:
        raise InvalidForLoopException("Boolean condition is of wrong type")
    while evaluated_condition:
        parse_eval(statements, instance_vars, stack)
        assign_variable(update, instance_vars, stack)
        
    return
示例#6
0
def update_variable(statement, instance_vars, stack, just_declared=False):
    from javarepl import evaluate_expression
    tokens = tokenize_assignment_statement(statement)
    if len(tokens) != 2:
        raise InvalidAssignmentException("Statement provided was: " +
                                         statement)
    variable_name, variable_value = tokens[0].strip(), tokens[1].strip()
    variable_frame = get_variable_frame(variable_name, instance_vars, stack)

    if variable_frame == None:
        raise InvalidAssignmentException("")

    # Compute the result to update the value of expression with.
    result = evaluate_expression(variable_value)
    result_type = type(result)
    stored_variable_type = variable_frame[variable_name].get_datatype()
    stored_variable_value = variable_frame[variable_name].get_value()

    # Check to ensure result datatype matches variable datatype
    verify_result_datatype(result, variable_name, just_declared,
                           stored_variable_type, instance_vars, stack)

    # Now that we know it is  valid, write to variable.
    variable_frame[variable_name].set_value(result)