Example #1
0
void final_token_check(void)
{
    if(acquired_tokens.size() == 0 &&
            generated_tokens.size() == nr_of_generated_tokens)
    {
        std::cout << "SUCCESS: All acquired tokens have been released" << std::endl;
    }
    else
    {
        std::cout << "FAILURE: Not all tokens have been released" << std::endl;
        check_tokens();
    }
}
Example #2
0
void test_tokenize(CuTest *tc) {
	char *file1 = "float bmi = weight/height;";
	char *file2 = "float pi = 3.14159265;";
	char *file3 = "print \"Hello World!\";\nfoo();\n";
	char *file4 = "print \"\\\"Hello World!\\\"\";\nint i = 0;\n\nbar(i++);\\";

	char *file1_expected[7] = {"float", "bmi", "=", "weight", "/", "height", ";"};
	char *file2_expected[5] = {"float", "pi", "=", "3.14159265", ";"};
	char *file3_expected[7] = {"print", "\"Hello World!\"", ";", "foo", "(", ")", ";"};
	char *file4_expected[15] = {"print", "\"\\\"Hello World!\\\"\"", ";", "int", "i", "=",\
					 "0", ";", "bar", "(", "i", "++", ")", ";", "\\" };

	token_t *head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file1, strlen(file1), &head));
	
	check_tokens(tc, head,  file1_expected, 7);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file2, strlen(file2), &head));

	check_tokens(tc, head,  file2_expected, 5);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file3, strlen(file3), &head));

	check_tokens(tc, head, file3_expected, 7);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file4, strlen(file4), &head));

	check_tokens(tc, head, file4_expected, 15);
	free_tokens(head);
	head = NULL;
}
Example #3
0
int long_reduction(Resources *res, TStack *stack, int rule) {
    args_assert(res != NULL && stack != NULL, INTERNAL_ERROR);
                
    TToken *reduced_tokens[MAX_RULE_LENGTH];
    int original_type;
    int err;
    debug_print("%s\n", "LONG REDUCTION");
    
    catch_internal_error(
        dereference_structure(&res->struct_buff, stack->top, (void **)&reduced_tokens[2]),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );

    catch_internal_error(
        dereference_structure(&res->struct_buff, reduced_tokens[2]->next, (void **)&reduced_tokens[1]),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );
    catch_internal_error(
        dereference_structure(&res->struct_buff, reduced_tokens[1]->next, (void **)&reduced_tokens[0]),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );
 
    if (rule == 10)       // rule ( RVALUE )
        original_type = get_original_type(reduced_tokens[1]);
    else {

        err = check_tokens(res, reduced_tokens[1]->next, stack->top);
        
        switch(err) {
            case INTERNAL_ERROR:
                  return INTERNAL_ERROR;
                  break;

            case SEMANTIC_ERROR:
                debug_print("%s\n", "SEMANTIC_ERROR");
                return err;
                break;
            
            case TYPE_ERROR:
                debug_print("%s\n", "TYPE_ERROR");
                return err;
                break;
           
            case TYPE_CAST_FIRST:
                original_type = get_original_type(reduced_tokens[2]);
                debug_print("%s\n", "TYPE CAST FIRST");
                if (original_type == L_INT)
                    catch_internal_error(new_instruction_int_int(&res->instruction_buffer, 0lu, BELOW_TOP, 0, CAST_DBL_MEM),
                                         INTERNAL_ERROR, "Failed to generate new instruction");
                else if (original_type == L_DOUBLE)
                    catch_internal_error(new_instruction_int_int(&res->instruction_buffer, 0lu, BELOW_TOP, 0, CAST_INT_MEM),
                                         INTERNAL_ERROR, "Failed to generate new instruction");
                break;
           
            case TYPE_CAST_SECOND:
                original_type = get_original_type(reduced_tokens[0]);
                debug_print("%s\n", "TYPE CAST SECOND");
                if (original_type == L_INT)
                    catch_internal_error(new_instruction_int_int(&res->instruction_buffer, 0lu, STACK_TOP, 0, CAST_DBL_MEM),
                                         INTERNAL_ERROR, "Failed to generate new instruction");
                else if (original_type == L_DOUBLE)
                    catch_internal_error(new_instruction_int_int(&res->instruction_buffer, 0lu, STACK_TOP, 0, CAST_INT_MEM),
                                         INTERNAL_ERROR, "Failed to generate new instruction");
                break;
           
            case RETURN_OK:
                original_type = get_original_type(reduced_tokens[0]);
                debug_print("%s\n", "TYPES OK");
            default:
                original_type = get_original_type(reduced_tokens[0]);

        }

        catch_internal_error(new_instruction_empty(&res->instruction_buffer, token_to_ins(reduced_tokens[1]->token_type, original_type)),
                             INTERNAL_ERROR, "Failed to generate instruction");

        if (rule >= 0 && rule <= 5) // Relational operation type of result is L_INT
                original_type = L_INT;
        if (rule > 5 && original_type == L_STRING)
            return TYPE_ERROR;
    }

    err = reduce(&res->struct_buff, stack, original_type);
    catch_internal_error(err, INTERNAL_ERROR, "Failed to reduce");
    catch_syntax_error(err, SYNTAX_ERROR, "Failed to reduce", 1);
    return RETURN_OK;
}