diff options
| -rw-r--r-- | challenge-287/barroff/raku/ch-1.p6 | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-287/barroff/raku/ch-1.p6 b/challenge-287/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..bb4e0a14fa --- /dev/null +++ b/challenge-287/barroff/raku/ch-1.p6 @@ -0,0 +1,36 @@ +#!/usr/bin/env raku + +use v6.d; + +grammar Valid-Number { + token TOP { [ <integer>|<decimal> ] <exponent>? } + token sign { [ '+'|'-' ] } + token num { \d+ } + token dot { '.' } + token integer { <sign>? <num> } + token decimal { <sign>? [ <num> <dot> <num>? || <dot> <num> ] } + token exponent { [ 'e'|'E' ] <integer>} +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 7; + + ok Valid-Number.parse('1'), 'works for "1"'; + nok Valid-Number.parse('a'), 'works for "a"'; + nok Valid-Number.parse('.'), 'works for "."'; + nok Valid-Number.parse('1.2e4.2'), 'works for "1.2e4.2"'; + ok Valid-Number.parse('-1.'), 'works for "-1."'; + ok Valid-Number.parse('+1E-8'), 'works for "+1E-8"'; + ok Valid-Number.parse('.44'), 'works for ".44"'; +} + +#| Take user provided string like "'1.2e-3.4" +multi sub MAIN(Str $str) { + if Valid-Number.parse($str) { + say "a valid number"; + } else { + say "not a valid number"; + }; +} |
