defmodule GeneralTester do @moduledoc """ Module used to perform general tests. """ def insert_token_list(output_token_list, [], _index) do output_token_list end def insert_token_list(output_token_list, list, index) do {token, list} = List.pop_at(list, 0) otl = List.insert_at(output_token_list, index, token) insert_token_list(otl, list, index + 1) end def delete_token_from_otl(output_token_list, index) do List.delete_at(output_token_list, index) end def start_general_test_compilation(source_code_string) do general_abstract_syntax_tree = get_c_structures_content() |> Reader._generate_general_ast() general_token_list = get_c_tokens_content() |> Reader._generate_general_token_list() {output_token_list, lexer_token} = Lexer.tokenize({source_code_string, general_token_list}) case lexer_token do :error -> Common.StringElements.lexer_error_invalid_token() :ok -> evaluate_parser(output_token_list, general_abstract_syntax_tree) end end defp evaluate_parser(output_token_list, general_abstract_syntax_tree) do {parser_token,output_abstract_syntax_tree,_token_list,_error_cause} = Parser.parse(output_token_list, general_abstract_syntax_tree) case parser_token do :token_missing_error -> Common.StringElements.parser_error_missing_token() :token_not_absorbed_error -> Common.StringElements.parser_error_token_not_absorbed() :ok -> continue_from_parser(output_abstract_syntax_tree) end end defp continue_from_parser(output_abstract_syntax_tree) do CodeGenerator.generate_code(output_abstract_syntax_tree, false) |> CodeOptimizer.optimize(false) |> CodeConnector.connect(false) |> Writer.write_file() |> Invoker.invoke_gcc("") end def get_c_tokens_content() do """ int\\s+ main \\( \\) { } return\\s+ [0-9]+ ; - ~ \\+ \\* / \\&\\& \\|\\| != ! == <= < >= > """ end def get_c_structures_content() do """ data-type function-name args-opener args-closer function-opener operation function-closer program-root movq %:5, %:r int data-type main function-name parenthesis-open group-opener args-opener parenthesis-close group-closer args-closer bracket-open function-opener bracket-close function-closer return-word exp line-ender operation returner movq %:1, %:r return return-word logical-and-exp logical-or-operator exp exp cmp $0, %:0 je _:uclause2 movq $1, %:r jmp _:uend _:uclause2: cmp $0, %:2 movq $0, %rax setne %al movq %rax, %:r _:uend: equality-exp logical-and-operator logical-and-exp logical-and-exp cmp $0, %:0 jne _:uclause2 movq $0, %:r jmp _:uend _:uclause2: cmp $0, %:2 movq $0, %rax setne %al movq %rax, %:r _:uend: relational-exp not-equal-operator equality-exp equality-exp cmp %:0, %:2 mov $0, %rax setne %al movq %rax, %:r relational-exp equal-operator equality-exp equality-exp cmp %:0, %:2 mov $0, %rax sete %al movq %rax, %:r additive-exp le-operator relational-exp relational-exp cmp %:2, %:0 movq $0, %rax setl %al movq %rax, %:r additive-exp ge-operator relational-exp relational-exp cmp %:2, %:0 movq $0, %rax setg %al movq %rax, %:r additive-exp leq-operator relational-exp relational-exp cmp %:2, %:0 movq $0, %rax setle %al movq %rax, %:r additive-exp geq-operator relational-exp relational-exp cmp %:2, %:0 movq $0, %rax setge %al movq %rax, %:r term sum-operator additive-exp additive-exp movq %:0, %:r add %:2, %:r term minus-operator additive-exp additive-exp movq %:0, %:r sub %:2, %:r factor division-operator term term movq %:0, %rax movq $0, %rdx idiv %:2 movq %rax, %:r factor multiplication-operator term term movq %:0, %:r imul %:2, %:r negative-operator factor factor neg %:1 movq %:1, %:r complement-operator factor factor not %:1 movq %:1, %:r negation-operator factor factor cmp $0, %:1 movq $0, %rax sete %al movq %rax, %:r logical-and-exp exp movq %:0, %:r equality-exp logical-and-exp movq %:0, %:r relational-exp equality-exp movq %:0, %:r additive-exp relational-exp movq %:0, %:r term additive-exp movq %:0, %:r factor term movq %:0, %:r group-opener exp group-closer factor movq %:1, %:r literal factor movq $:t, %:r semicolon line-ender minus negative-operator minus-operator complement complement-operator negation negation-operator plus sum-operator slant division-operator asterix multiplication-operator or logical-or-operator and logical-and-operator neq not-equal-operator eq equal-operator le le-operator leq leq-operator ge ge-operator geq geq-operator """ end end