blib/lib/Acme/Aheui.pm

filter: Statementstmt 83.8% 161 / 192 Branchbran 74.4% 73 / 98 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 22 / 22 Podpod 50.0% 2 / 4 totaltotal 81.6% 275 / 337 CC 58 SCAR 30.7
CC
58
Cov
81%
_loop_steps
51.3
_get_deltas_upon_jung
16.5
_move_cursor
6.0
CRAP 21.5
↑ Summary Build »
1 package Acme::Aheui;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode qw/encode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 =encoding utf8
9
10 =head1 NAME
11
12 Acme::Aheui - an aheui interpreter
13
14 =head1 VERSION
15
16 Version 0.03
17
18 =cut
19
20 1 our $VERSION = '0.03';
21
22
23 =head1 SYNOPSIS
24
25 use utf8;
26 use Acme::Aheui;
27 my $interpreter = Acme::Aheui->new( source => '아희' );
28 $interpreter->execute();
29
30 =head1 DESCRIPTION
31
32 An aheui interpreter.
33
34 See aheui language specification at L<https://aheui.github.io/specification.en>
35
36 Most logic is based on the reference implementation by Puzzlet Chung.
37 (L<https://github.com/aheui/jsaheui>)
38
39 =cut
40
41 use constant {
42 1 JONG_STROKE_NUMS =>
43 [0, 2, 4, 4, 2, 5, 5, 3, 5, 7, 9, 9, 7, 9,
44 9, 8, 4, 4, 6, 2, 4, 1, 3, 4, 3, 4, 4, 3],
45 REQUIRED_ELEM_NUMS =>
46 [0, 0, 2, 2, 2, 2, 1, 0, 1, 0, 1, 0, 2, 0, 1, 0, 2, 2, 0],
47 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
48
49 =head1 PUBLIC METHODS
50
51 =head2 new
52
53 my $interpreter = Acme::Aheui->new( source => '아희' );
54
55 This method will create and return C<Acme::Aheui> object.
56
57 =cut
58
59 sub new {
60 9 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
61 9 my %args = @_;
62
63 9 ▶ my $source = $args{source} || '';
Condition100%2/2
$args{'source'} || ''
l!l
72
MC/DC100%1/1
$args{'source'} || ''
$args{'source'}
Truth table100%2/2
A
A= $args{'source'}
Aresult
11
00
64 9 my $codespace = build_codespace($source);
65
66 9 my $self = {
67 _codespace => $codespace,
68 _stacks => [],
69 _stack_index => 0,
70 _x => 0,
71 _y => 0,
72 _dx => 0,
73 _dy => 1,
74 };
75 9 bless $self, $class;
76
77 9 return $self;
78 }
79
80 sub build_codespace {
81 9 ▶ my ($source) = @_;
Subroutine build_codespace: called CC=3 SCAR=11.0
Pod undocumented
82
83 9 my @lines = split /\r?\n/, $source;
84 9 my @rows = ();
85 9 for my $line (@lines) {
86 25 my @row = ();
87 25 for my $char (split //, $line) {
88 112 my $disassembled = disassemble_hangul_char($char);
89 112 push @row, $disassembled;
90 }
91 25 push @rows, \@row;
92 }
93 9 return \@rows;
94 }
95
96 sub disassemble_hangul_char {
97 112 ▶ my ($char) = @_;
Subroutine disassemble_hangul_char: called CC=2 SCAR=6.9
Pod undocumented
98
99 112 ▶ if ($char =~ /[가-힣]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /[\x{ac00}-\x{d7a3}]/) { } 99 13 112
100 99 my $code = unpack 'U', $char;
101 99 $code -= 0xAC00;
102 99 my ($cho, $jung, $jong) = (int($code/28/21), ($code/28)%21, $code%28);
103 99 return {cho => $cho, jung => $jung, jong => $jong};
104 }
105 else {
106 13 return {cho => -1, jung => -1, jong => -1};
107 }
108 }
109
110 =head2 execute
111
112 $interpreter->execute();
113
114 This method will execute the aheui program and return the exit code.
115 It may use C<STDIN> and/or C<STDOUT> if the aheui program uses I/O.
116
117 =cut
118
119 sub execute {
120 6 ▶ my ($self) = @_;
Subroutine execute: called CC=2 SCAR=6.9
Pod documented
121
122 6 ▶ return 0 unless $self->_has_initial_command();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_initial_command 2 4 6
123 4 return $self->_loop_steps();
124 }
125
126 sub _has_initial_command {
127 6 ▶ my ($self) = @_;
Subroutine _has_initial_command: called CC=4 SCAR=13.9
128
129 6 for my $row (@{ $self->{_codespace} }) {
130 10 my $first_command = @$row[0];
131 10 ▶ if ($first_command && $$first_command{cho} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_command and $first_command->{"cho"} != -1) 4 6 10
Condition100%3/3
$first_command and $first_command->{"cho"} != -1
!ll&&!rl&&r
244
MC/DC100%2/2
$first_command and $first_command->{"cho"} != -1
$first_command $first_command->{"cho"} != -1
Truth table100%3/3
A and B
A= $first_commandB= $first_command->{"cho"} != -1
ABresult
0X0
100
111
132 4 return 1;
133 }
134 }
135 2 return 0;
136 }
137
138 sub _loop_steps {
139 4 ▶ my ($self) = @_;
Subroutine _loop_steps: called CC=27 SCAR=39.4
140
141 4 while (1) {
142 76 my $codespace = $self->{_codespace};
143 76 my ($x, $y) = ($self->{_x}, $self->{_y});
144
145 76 ▶ if ($x > $#{$$codespace[$y]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $#{$$codespace[$y];}) 0 76 76
146 0 $self->_move_cursor();
147 0 next;
148 }
149
150 76 my $c = $$codespace[$y][$x];
151
152 76 ▶ if (!$c || $c->{cho} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c or $c->{"cho"} == -1) 0 76 76
Condition33%1/3
not $c or $c->{"cho"} == -1
l!l&&r!l&&!r
0076
MC/DC0%0/2
not $c or $c->{"cho"} == -1
not $c $c->{"cho"} == -1
Truth table33%1/3
not(A) or B
A= not $cB= $c->{"cho"} == -1
ABresult
1X1
011
000
153 0 $self->_move_cursor();
154 0 next;
155 }
156
157 76 my $cho = $c->{cho};
158 76 my $jung = $c->{jung};
159 76 my $jong = $c->{jong};
160 76 my $si = $self->{_stack_index};
161
162 76 my ($dx, $dy) = $self->_get_deltas_upon_jung($jung);
163 76 $self->{_dx} = $dx;
164 76 $self->{_dy} = $dy;
165
166 76 my $stack = $self->{_stacks}->[$si];
167 76 ▶ my $elem_num = ($stack) ? scalar @{$stack} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$stack ? : 72 4 76
168 76 ▶ if ($elem_num < REQUIRED_ELEM_NUMS->[$cho]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_num < [0, 0, 2, 2, 2, 2, 1, 0, 1, 0, 1, 0, 2, 0, 1, 0, 2, 2, 0]->[$cho]) { } 0 76 76
169 0 $self->{_dx} = -($self->{_dx});
170 0 $self->{_dy} = -($self->{_dy});
171 }
172 else {
173 76 ▶ if ($cho == 2) { # ㄴ
Branch: 14 branches
BranchTrueFalseTotal
if ($cho == 2) { } 0 76 76
elsif ($cho == 3) { } 8 68 76
elsif ($cho == 16) { } 3 65 68
elsif ($cho == 4) { } 6 59 65
elsif ($cho == 5) { } 0 59 59
elsif ($cho == 6) { } 18 41 59
elsif ($cho == 7) { } 24 17 41
elsif ($cho == 8) { } 12 5 17
elsif ($cho == 17) { } 1 4 5
elsif ($cho == 9) { } 0 4 4
elsif ($cho == 10) { } 0 4 4
elsif ($cho == 12) { } 0 4 4
elsif ($cho == 14) { } 0 4 4
elsif ($cho == 18) { } 4 0 4
174 0 my $m = $self->_pop($si);
175 0 my $n = $self->_pop($si);
176 0 $self->_push($si, int($n/$m));
177 }
178 elsif ($cho == 3) { # ㄷ
179 8 my $m = $self->_pop($si);
180 8 my $n = $self->_pop($si);
181 8 $self->_push($si, $n+$m);
182 }
183 elsif ($cho == 16) { # ㅌ
184 3 my $m = $self->_pop($si);
185 3 my $n = $self->_pop($si);
186 3 $self->_push($si, $n-$m);
187 }
188 elsif ($cho == 4) { # ㄸ
189 6 my $m = $self->_pop($si);
190 6 my $n = $self->_pop($si);
191 6 $self->_push($si, $n*$m);
192 }
193 elsif ($cho == 5) { # ㄹ
194 0 my $m = $self->_pop($si);
195 0 my $n = $self->_pop($si);
196 0 $self->_push($si, $n%$m);
197 }
198 elsif ($cho == 6) { # ㅁ
199 18 my $v = $self->_pop($si);
200 18 ▶ if ($jong == 21) { # jongㅇ
Branch: 2 branches
BranchTrueFalseTotal
if ($jong == 21) { } 2 16 18
elsif ($jong == 27) { } 16 0 16
201 2 $self->_output_number($v);
202 }
203 elsif ($jong == 27) { # jongㅎ
204 16 $self->_output_code_as_character($v);
205 }
206 }
207 elsif ($cho == 7) { # ㅂ
208 24 my $v = 0;
209 24 ▶ if ($jong == 21) { # jongㅇ
Branch: 2 branches
BranchTrueFalseTotal
if ($jong == 21) { } 1 23 24
elsif ($jong == 27) { } 2 21 23
210 1 $v = $self->_get_input_number();
211 }
212 elsif ($jong == 27) { # jongㅎ
213 2 $v = $self->_get_input_character_as_code();
214 }
215 else { # the other jongs
216 21 $v = JONG_STROKE_NUMS->[$jong];
217 }
218 24 $self->_push($si, $v);
219 }
220 elsif ($cho == 8) { # ㅃ
221 12 $self->_duplicate($si);
222 }
223 elsif ($cho == 17) { # ㅍ
224 1 $self->_swap($si);
225 }
226 elsif ($cho == 9) { # ㅅ
227 0 $self->{_stack_index} = $jong;
228 }
229 elsif ($cho == 10) { # ㅆ
230 0 $self->_push($jong, $self->_pop($si));
231 }
232 elsif ($cho == 12) { # ㅈ
233 0 my $m = $self->_pop($si);
234 0 my $n = $self->_pop($si);
235 0 ▶ my $in = ($n >= $m) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n >= $m ? : 0 0 0
236 0 $self->_push($si, $in);
237 }
238 elsif ($cho == 14) { # ㅊ
239 0 ▶ if ($self->_pop($si) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pop($si) == 0) 0 0 0
240 0 $self->{_dx} = -($self->{_dx});
241 0 $self->{_dy} = -($self->{_dy});
242 }
243 }
244 elsif ($cho == 18) { # ㅎ
245 4 ▶ my $ret = $self->_pop($si) || 0;
Condition100%2/2
$self->_pop($si) || 0
l!l
13
MC/DC100%1/1
$self->_pop($si) || 0
$self->_pop($si)
Truth table100%2/2
A
A= $self->_pop($si)
Aresult
11
00
246 4 return $ret;
247 }
248 }
249
250 72 $self->_move_cursor();
251 }
252 }
253
254 sub _move_cursor {
255 101 ▶ my ($self) = @_;
Subroutine _move_cursor: called CC=6 SCAR=17.9
256 101 my $codespace = $self->{_codespace};
257
258 101 $self->{_x} += $self->{_dx};
259 101 $self->{_y} += $self->{_dy};
260
261 101 my $last_row_index = $#{ $codespace };
262 101 ▶ if ($self->{_y} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_y"} < 0) 2 99 101
263 2 $self->{_y} = $last_row_index;
264 }
265 101 ▶ if ($self->{_y} > $last_row_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_y"} > $last_row_index) 2 99 101
266 2 $self->{_y} = 0;
267 }
268
269 101 my $last_char_index = $#{ @$codespace[$self->{_y}] };
270 101 ▶ if ($self->{_x} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_x"} < 0) 4 97 101
271 4 $self->{_x} = $last_char_index;
272 }
273 101 ▶ if ($self->{_x} > $last_char_index &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_x"} > $last_char_index and $self->{"_dx"} != 0) 3 98 101
Condition100%3/3
$self->{"_x"} > $last_char_index and $self->{"_dx"} != 0
!ll&&!rl&&r
9533
MC/DC100%2/2
$self->{"_x"} > $last_char_index and $self->{"_dx"} != 0
$self->{"_x"} > $last_char_index $self->{"_dx"} != 0
Truth table100%3/3
A and B
A= $self->{"_x"} > $last_char_indexB= $self->{"_dx"} != 0
ABresult
0X0
100
111
274 $self->{_dx} != 0) {
275 3 $self->{_x} = 0;
276 }
277 }
278
279 sub _get_deltas_upon_jung {
280 76 ▶ my ($self, $jung) = @_;
Subroutine _get_deltas_upon_jung: called CC=12 SCAR=28.1
281
282 76 my $dx = $self->{_dx};
283 76 my $dy = $self->{_dy};
284
285 76 ▶ if ($jung == 0) {
Branch: 11 branches
BranchTrueFalseTotal
if ($jung == 0) { } 28 48 76
elsif ($jung == 2) { } 0 48 48
elsif ($jung == 4) { } 16 32 48
elsif ($jung == 6) { } 0 32 32
elsif ($jung == 8) { } 12 20 32
elsif ($jung == 12) { } 0 20 20
elsif ($jung == 13) { } 16 4 20
elsif ($jung == 17) { } 0 4 4
elsif ($jung == 18) { } 0 4 4
elsif ($jung == 19) { } 1 3 4
elsif ($jung == 20) { } 3 0 3
286 28 return (1, 0); # ㅏ
287 }
288 elsif ($jung == 2) {
289 0 return (2, 0); # ㅑ
290 }
291 elsif ($jung == 4) {
292 16 return (-1, 0); # ㅓ
293 }
294 elsif ($jung == 6) {
295 0 return (-2, 0); # ㅕ
296 }
297 elsif ($jung == 8) {
298 12 return (0, -1); # ㅗ
299 }
300 elsif ($jung == 12) {
301 0 return (0, -2); # ㅛ
302 }
303 elsif ($jung == 13) {
304 16 return (0, 1); # ㅜ
305 }
306 elsif ($jung == 17) {
307 0 return (0, 2); # ㅠ
308 }
309 elsif ($jung == 18) {
310 0 return ($dx, -$dy); # ㅡ
311 }
312 elsif ($jung == 19) {
313 1 return (-$dx, -$dy); # ㅢ
314 }
315 elsif ($jung == 20) {
316 3 return (-$dx, $dy); # ㅣ
317 }
318 else {
319 0 return ($dx, $dy);
320 }
321 }
322
323 sub _push {
324 257 ▶ my ($self, $i, $n) = @_;
Subroutine _push: called CC=2 SCAR=7.4
325
326 257 ▶ if ($i == 27) { # ㅎ
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 27) { } 0 257 257
327 0 return;
328 }
329 else {
330 257 push @{$self->{_stacks}->[$i]}, $n;
331 }
332 }
333
334 sub _pop {
335 299 ▶ my ($self, $i) = @_;
Subroutine _pop: called CC=3 SCAR=11.2
336 299 my $stack = $self->{_stacks}->[$i];
337
338 299 ▶ if ($i == 21) { # ㅇ
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 21) { } 9 290 299
elsif ($i == 27) { } 0 290 290
339 9 return shift @$stack;
340 }
341 elsif ($i == 27) { # ㅎ
342 0 return;
343 }
344 else {
345 290 return pop @$stack;
346 }
347 }
348
349 sub _duplicate {
350 39 ▶ my ($self, $i) = @_;
Subroutine _duplicate: called CC=3 SCAR=11.1
351 39 my $stack = $self->{_stacks}->[$i];
352
353 39 ▶ if ($i == 21) { # ㅇ
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 21) { } 1 38 39
elsif ($i == 27) { } 0 38 38
354 1 my $first = $$stack[0];
355 1 unshift @$stack, $first;
356 }
357 elsif ($i == 27) { # ㅎ
358 0 return;
359 }
360 else {
361 38 my $last = $$stack[-1];
362 38 push @$stack, $last;
363 }
364 }
365
366 sub _swap {
367 28 ▶ my ($self, $i) = @_;
Subroutine _swap: called CC=3 SCAR=11.0
368 28 my $stack = $self->{_stacks}->[$i];
369
370 28 ▶ if ($i == 21) { # ㅇ
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 21) { } 1 27 28
elsif ($i == 27) { } 0 27 27
371 1 my $first = $$stack[0];
372 1 my $second = $$stack[1];
373 1 $$stack[0] = $second;
374 1 $$stack[1] = $first;
375 }
376 elsif ($i == 27) { # ㅎ
377 0 return;
378 }
379 else {
380 27 my $last = $$stack[-1];
381 27 my $next = $$stack[-2];
382 27 $$stack[-1] = $next;
383 27 $$stack[-2] = $last;
384 }
385 }
386
387 sub _output_number {
388 2 ▶ my ($self, $number) = @_;
Subroutine _output_number: called CC=1 SCAR=0.0
389
390 2 print $number;
391 }
392
393 sub _output_code_as_character {
394 16 ▶ my ($self, $code) = @_;
Subroutine _output_code_as_character: called CC=1 SCAR=0.0
395
396 16 my $unichar = pack 'U', $code;
397 16 print encode('utf-8', $unichar);
398 }
399
400 sub _get_input_character_as_code {
401 2 ▶ my ($self) = @_;
Subroutine _get_input_character_as_code: called CC=1 SCAR=0.0
402
403 2 my $char = ReadKey(0);
404 2 return unpack 'U', $char;
405 }
406
407 sub _get_input_number {
408 1 ▶ my ($self) = @_;
Subroutine _get_input_number: called CC=1 SCAR=0.0
409
410 1 return int(ReadLine(0));
411 }
412
413 =head1 INSTALLATION
414
415 To install this module, run the following commands:
416
417 perl Build.PL
418 ./Build
419 ./Build test
420 ./Build install
421
422 =head1 AUTHOR
423
424 Rakjin Hwang, C<< <rakjin@cpan.org> >>
425
426 =head1 LICENSE
427
428 This program is free software; you can redistribute it and/or modify it
429 under the same terms as Perl itself.
430
431 =cut
432
433 1 1;
↑ Summary Build »