2017年6月7日水曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 2(Moving Forward)、Chapter 13(Regexes and Grammars)の Named Rules (a.k.a. Subrules)、Exercise 13-1: Getting the Feburary Dates Right.を取り組んでみる。

Exercise 13-1: Getting the Feburary Dates Right.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

my token year { \d ** 4 }
my token month { 1 <[0.2]>
                 || 0 <[1..9]> }
my token day { (\d ** 2) <?{1 <= $0 <= 31}> }
my token sep { '/' || '-'}
my rule date {[    <year> (<sep>) <month> $0 <day>
                || <day> (<sep>) <month> $0 <year>
                || <month>\s<day> ',' <year>
              ] <!{ ($<day> > 30 and $<month> == 2|4|6|9|11) or
                    ($<day> == 30 and $<month> == 2)}>
             }

for ("Chrimstmas : 2016-12-25.", "2016-02-28", "2016-02-30") -> $string {
  say $string;                 
  if $string ~~ /<date>/ {
    say ~$/;
    say "Day\t= ", ~$/<date><day>;
    say "Month\t= ", ~$/<date><month>;
    say "Year\t= ", ~$/<date><year>;
  }
  say '';
}

入出力結果(Terminal, REPL)

$ ./sample1.pl
Chrimstmas : 2016-12-25.
2016-12-25
Day = 25
Month = 12
Year = 2016

2016-02-28
2016-02-28
Day = 28
Month = 02
Year = 2016

2016-02-30

$

0 コメント:

コメントを投稿