Ejemplo n.º 1
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))))
    
    tokens = tokens[0].split(";") + [tokens[1]]
    initialize, condition, update, statements = tokens
    
    assign_variable(initialize, instance_vars, stack)
    """
    evaluated_condition = evaluate_expression(condition, instance_vars, stack)
    if type(evaluated_condition) is not bool:
        raise InvalidForLoopException("Boolean condition is of wrong type")
    """
    while evaluate_expression(condition, instance_vars, stack):
        parse_eval(statements, instance_vars, stack)
        assign_variable(update, instance_vars, stack)
        
    # Assumes well-formed expression.
    var_name = initialize.split(" ")[1]
    get_variable_frame(var_name, instance_vars, stack).pop(var_name)
    
    return
Ejemplo n.º 2
0
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
Ejemplo n.º 3
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))))

    tokens = tokens[0].split(";") + [tokens[1]]
    initialize, condition, update, statements = tokens

    assign_variable(initialize, instance_vars, stack)
    """
    evaluated_condition = evaluate_expression(condition, instance_vars, stack)
    if type(evaluated_condition) is not bool:
        raise InvalidForLoopException("Boolean condition is of wrong type")
    """
    while evaluate_expression(condition, instance_vars, stack):
        parse_eval(statements, instance_vars, stack)
        assign_variable(update, instance_vars, stack)

    # Assumes well-formed expression.
    var_name = initialize.split(" ")[1]
    get_variable_frame(var_name, instance_vars, stack).pop(var_name)

    return
Ejemplo n.º 4
0
def handle_conditional_statements(if_else_block, instance_vars, stack):
    #import javarepl# import evaluate_expression
    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],
                                             instance_vars, stack)
        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
Ejemplo n.º 5
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))))
    
    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, instance_vars, stack):
        parse_eval(statements, instance_vars, stack)    # We will NOT support different scoping for variables inside.
        
    return  # Call parse_eval with instance_vars
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def split_declaration(declaration):
    declaration_tokens = clean_up_list_elems(declaration.split(" ")) 
    
    if len(declaration_tokens) == 1:   # Represents an assignment statement to a variable previously declared.
        datatype, name = None, declaration_tokens[0].strip()
    elif len(declaration_tokens) == 2:  # Represents an assignment statement to a variable being declared now.
        datatype, name = declaration_tokens[0].strip(), declaration_tokens[1].strip()
    else:
        raise InvalidDeclarationException("Invalid declaration provided: " + str(declaration))
    
    return datatype, name
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def split_declaration(declaration):
    declaration_tokens = clean_up_list_elems(declaration.split(" "))

    if len(
            declaration_tokens
    ) == 1:  # Represents an assignment statement to a variable previously declared.
        datatype, name = None, declaration_tokens[0].strip()
    elif len(
            declaration_tokens
    ) == 2:  # Represents an assignment statement to a variable being declared now.
        datatype, name = declaration_tokens[0].strip(
        ), declaration_tokens[1].strip()
    else:
        raise InvalidDeclarationException("Invalid declaration provided: " +
                                          str(declaration))

    return datatype, name