~mclehman/app-innsmouth-query

72a9413a4d7f0e2e0f725c77b7800e99fc925bb5 — 0xFORDCOMMA 3 years ago master
Initial commit.
3 files changed, 49 insertions(+), 0 deletions(-)

A .gitignore
A META6.json
A bin/innsmouth-query
A  => .gitignore +1 -0
@@ 1,1 @@
.precomp

A  => META6.json +14 -0
@@ 1,14 @@
{
  "version": "2020.01.01",
  "production": false,
  "name": "app-innsmouth-query",
  "perl": "v6.*",
  "license": "https://opensource.org/licenses/GPL-3.0",
  "authors": [
    "0xford"
  ],
  "scripts": {
    "test": "zef test ."
  },
  "description": ""
}

A  => bin/innsmouth-query +34 -0
@@ 1,34 @@
#!/usr/bin/env raku

use Innsmouth::Remote;

my %ops = Q[<]   => &infix:<<   < >>,
          Q[>]   => &infix:<<   > >>,
          Q[<=]  => &infix:<<  <= >>,
          Q[>=]  => &infix:<<  >= >>,
          Q[==]  => &infix:<<  == >>,
          Q[!=]  => &infix:<<  != >>,
          Q[eq]  => &infix:<<  eq >>,
          Q[!eq] => &infix:<< !eq >>;

my regex op { '<' | '>' | '<=' | '>=' | '==' | '!=' | 'eq' | '!eq' }
my regex expr { '<' $<query> = (\S+)+ %% \s* '>'
                   \s* $<op>    = <op>
                   \s* $<rhs>   = (\S+) }
subset Expr of Str where * ~~ /<expr>/;

sub parse-expr($expr-str) {
    $expr-str ~~ /<expr>/;
    return { query => $<expr><query>.words,
             op    => %ops{$<expr><op>},
             rhs   => $<expr><rhs> }
}

sub MAIN(*@exprs, Bool :$any = False) {
    unless all(@exprs) ~~ Expr { note "Malformed expression." and exit 1 }
    @exprs .= map: &parse-expr;
    my &junction = do if $any { &any } else { &all };

    my $res = @exprs.map(-> %expr { %expr<op>(innsmouth-query(%expr<query>), %expr<rhs>) }).reduce(&junction) ?? 0 !! 1;
    exit $res;
}