GCC Code Coverage Report


Directory: src/
File: script_transcompiler/frontend.cpp
Date: 2024-12-30 15:39:09
Exec Total Coverage
Lines: 43 66 65.2%
Functions: 2 2 100.0%
Branches: 36 100 36.0%

Line Branch Exec Source
1 #include "frontend.h"
2 #include "script.h"
3 #include <cstring>
4 #include <fstream>
5
6 namespace
7 {
8
9 108 bool parse_line(const std::string& line, unsigned int number, data& data,
10 bool& memory_set)
11 {
12 script::command cmd;
13 108 std::string operand;
14
15
1/2
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
108 script::parse(line, cmd, operand);
16
17
5/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
108 switch (cmd)
18 {
19 case (script::command::INVALID):
20
21 data.add_issue(issue::type::ERROR, issue::phase::PARSING,
22 "Invalid line.", line, number);
23
24 return false;
25 break;
26
27 case (script::command::COMMENT):
28 {
29 auto arg = line;
30 arg.erase(0, 1);
31
32 data.add_cmd(intermediate::cmd::comment, arg, line, number);
33 break;
34 }
35 36 case (script::command::TEXT):
36 {
37
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 data.add_cmd(intermediate::cmd::text_memory, operand, line, number);
38 36 memory_set = true;
39 36 break;
40 }
41 28 case (script::command::PRINT):
42 {
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!memory_set)
44 {
45 data.add_issue(
46 issue::type::WARNING, issue::phase::PARSING,
47 "calling 'print' before setting memory. Line removed", line,
48 number);
49 }
50 else
51 {
52
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 data.add_cmd(intermediate::cmd::print_memory, operand, line,
53 number);
54 }
55
56 28 break;
57 }
58 24 case (script::command::PROCESS):
59 {
60
61
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (memory_set)
62
2/4
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
24 data.add_cmd(intermediate::cmd::process_memory, "", line, number);
63 else
64 data.add_issue(
65 issue::type::WARNING, issue::phase::PARSING,
66 "calling 'process' before setting memory. Line removed", line,
67 number);
68
69 24 break;
70 }
71 8 case (script::command::LOAD):
72 {
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (operand.empty())
74 {
75 data.add_issue(issue::type::ERROR, issue::phase::PARSING,
76 "No operand given for 'load'.", line, number);
77 return false;
78 }
79
80
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 data.add_cmd(intermediate::cmd::load_memory, operand, line, number);
81
82 8 memory_set = true;
83
84 8 break;
85 }
86 12 case (script::command::SAVE):
87 {
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (operand.empty())
89 {
90 data.add_issue(issue::type::ERROR, issue::phase::PARSING,
91 "No operand given for 'save'.", line, number);
92 return false;
93 }
94
95
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (memory_set)
96
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 data.add_cmd(intermediate::cmd::save_memory, operand, line, number);
97 else
98
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 data.add_cmd(intermediate::cmd::create_file, operand, line, number);
99
100 12 break;
101 }
102 default:
103 {
104 data.add_issue(issue::type::ERROR, issue::phase::PARSING,
105 "Unknown command.", line, number);
106
107 return false;
108 break;
109 }
110 }
111
112 108 return true;
113 108 }
114
115 } // namespace
116
117 4 void parse_source(const std::string& src, data& data)
118 {
119
120
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 std::ifstream source{src};
121
122
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (!source.is_open())
123 {
124 data.add_issue(issue::type::ERROR, issue::phase::PARSING,
125 "Could not open file.", src, 0);
126 data.success = false;
127 return;
128 }
129
130 4 auto lineNumber = 0;
131 4 auto memory_set = false;
132
133 4 std::string line;
134
4/6
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 180 times.
✓ Branch 7 taken 4 times.
184 while (std::getline(source, line))
135 {
136 180 lineNumber++;
137
138
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 108 times.
180 if (line.empty())
139 72 continue;
140
141
3/6
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 108 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 108 times.
108 if ((line.back() == '\r' || line.back() == '\n'))
142 line.erase(line.size() - 1);
143
144
2/4
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
108 if (!parse_line(line, lineNumber, data, memory_set))
145 return;
146 }
147
148
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 source.close();
149
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 }
150