GCC Code Coverage Report


Directory: src/
File: script_interpreter/interpreter.cpp
Date: 2024-12-30 15:39:09
Exec Total Coverage
Lines: 47 53 88.7%
Functions: 5 5 100.0%
Branches: 33 60 55.0%

Line Branch Exec Source
1 #include "script.h"
2 #include <cstdlib>
3 #include <filesystem>
4 #include <fstream>
5 #include <iostream>
6 #include <string>
7
8 2 inline void print_error(const char* msg)
9 {
10 2 std::cout << "\033[31m";
11 2 std::cout << "Error: " << msg << std::endl;
12 2 std::cout << "\033[0m";
13 2 }
14
15 10 inline void print_command(const std::string& msg)
16 {
17 10 std::cout << msg << std::endl;
18 10 }
19
20 3 void print(const char* msg)
21 {
22 3 std::cout << "\033[32m";
23 3 std::cout << msg << std::endl;
24 3 std::cout << "\033[0m";
25 3 }
26
27 2 auto run_interpreter(const std::string& arg)
28 {
29
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 std::filesystem::path path(arg);
30
31
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!path.is_absolute())
32 path = std::filesystem::absolute(path);
33
34
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 std::ifstream file{path};
35
36
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (!file.is_open())
37 return false;
38
39
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 script::engine eng{print};
40 script::command cmd;
41 2 std::string operand;
42
43 2 std::string line;
44
4/6
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 1 times.
15 while (std::getline(file, line))
45 {
46
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
14 if (line.empty())
47 4 continue;
48
49
3/6
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
12 if ((line.back() == '\r' || line.back() == '\n'))
50 line.erase(line.size() - 1);
51
52
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (line.empty())
53 continue;
54
55
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 script::parse(line, cmd, operand);
56
57 // todo: make argument to print comments on demand
58
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (cmd == script::command::COMMENT)
59 2 continue;
60
61 // todo: make option
62
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 print_command(line);
63
64
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (cmd == script::command::INVALID)
65 {
66
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 print_error("invalid command");
67 1 return false;
68 }
69
70
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 const auto res = eng.run(cmd, operand);
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!res.empty())
72 {
73 print_error(res.c_str());
74 return false;
75 }
76
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 }
77
78
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 file.close();
79
80 1 return true;
81 2 }
82
83 3 int main(int argc, char* argv[])
84 {
85
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (argc == 1)
86 {
87 1 print_error("Missing command line argument.");
88 1 return EXIT_FAILURE;
89 }
90
91 // todo: make option
92 2 std::cout << argv[1] << "\n";
93
94
4/6
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
2 if (!run_interpreter(argv[1]))
95 1 return EXIT_FAILURE;
96
97 1 return EXIT_SUCCESS;
98 }
99