From 1ab36f4b259edef1e2e5d3475ba548eea9e2445d Mon Sep 17 00:00:00 2001 From: Fernando Correa de Oliveira Date: Wed, 19 Feb 2020 14:22:13 +0000 Subject: [PATCH] First end to end workflow working storing the right properties for i in '{ "type" : "bla", "a" : 42 }' '{ "type" : "bla", "a" : 13 }'; do echo $i; done | bin/eel -e='event ble { has $a1 = #1.a; has $a2 = #2.a; match { bla(#1, a == 42) bla(#2, a == 13) } }' {"type":"ble","a1":42,"timestamp":"2020-02-19T14:18:53.986815Z","a2":13} --- bin/eel | 8 +++-- lib/Event/AST/EventDeclaration.pm6 | 3 +- lib/Event/Runner.pm6 | 4 +-- lib/EventAction.pm6 | 3 +- lib/EventGrammar.pm6 | 1 + lib/EventTranslator.pm6 | 57 ++++++++++++++++++++++-------- 6 files changed, 55 insertions(+), 21 deletions(-) diff --git a/bin/eel b/bin/eel index 5317c95..24ad025 100755 --- a/bin/eel +++ b/bin/eel @@ -8,9 +8,13 @@ use JSON::Fast; subset File of Str where .IO.f; multi MAIN(File $file) { - eel($*IN.Supply.lines.map({ .&from-json }), :$file).tap: *.say; + my Supplier $s .= new; + eel($s.Supply.map({ .&from-json }), :$file).tap: *.&to-json(:!pretty).say; + lines.map: { $s.emit: $_ } } multi MAIN(Str :e(:eval(:evaluate(:$code)))!) { - eel($*IN.Supply.lines.map({ .&from-json }), :$code).tap: *.say; + my Supplier $s .= new; + eel($s.Supply.map({ .&from-json }), :$code).tap: *.&to-json(:!pretty).say; + lines.map: { $s.emit: $_ } } \ No newline at end of file diff --git a/lib/Event/AST/EventDeclaration.pm6 b/lib/Event/AST/EventDeclaration.pm6 index 326c192..946a00c 100644 --- a/lib/Event/AST/EventDeclaration.pm6 +++ b/lib/Event/AST/EventDeclaration.pm6 @@ -2,4 +2,5 @@ use Event::AST; unit class Event::AST::EventDeclaration does Event::AST; has Str $.name is required; has %.attrs; -has $.body; \ No newline at end of file +has $.body; +has %.store; \ No newline at end of file diff --git a/lib/Event/Runner.pm6 b/lib/Event/Runner.pm6 index 372eb51..7e46e18 100644 --- a/lib/Event/Runner.pm6 +++ b/lib/Event/Runner.pm6 @@ -19,10 +19,10 @@ multi method exec(|c) { die "unrecognised: { c.perl }" } proto method init-event(% --> Hash()) {*} multi method init-event(%event (:$timestamp where *.defined, |)) { %event } -multi method init-event(%event) { { |%event, :timestamp(now) } } +multi method init-event(%event) { %( |%event, :timestamp(now) ) } method run() { - dd @!rules; +# dd @!rules; for @!rules -> %cmd { self.exec: %cmd } diff --git a/lib/EventAction.pm6 b/lib/EventAction.pm6 index b2b2cfb..b42ca1a 100644 --- a/lib/EventAction.pm6 +++ b/lib/EventAction.pm6 @@ -16,6 +16,7 @@ method TOP($/) { make $>>.made.grep: *.defined } method name($/) { make ~$/ } method mp-name($/) { + %*store.push: ~$ => $.head.made; make Event::AST::LocalVar.new: :var-id(~$), :path($>>.made) @@ -33,6 +34,7 @@ method declarator:sym($/) { :$name, :%attrs, :@body, + :%*store, ; } @@ -63,7 +65,6 @@ method statement-time-mod($/) { } method statement:sym($/) { - say $; make $ ?? Event::AST::Infix.new: :left($.made), diff --git a/lib/EventGrammar.pm6 b/lib/EventGrammar.pm6 index b5daac1..fd87316 100644 --- a/lib/EventGrammar.pm6 +++ b/lib/EventGrammar.pm6 @@ -22,6 +22,7 @@ proto rule declarator {*} rule declarator:sym { :my %*local-vars := SetHash.new; :my $*event-name; + :my %*store; { $*event-name = ~$; diff --git a/lib/EventTranslator.pm6 b/lib/EventTranslator.pm6 index 7990562..d01104d 100644 --- a/lib/EventTranslator.pm6 +++ b/lib/EventTranslator.pm6 @@ -2,36 +2,63 @@ use Event::AST; use Event::AST::EventDeclaration; use Event::AST::EventMatcher; use Event::AST::Condition; +use Event::AST::LocalVar; use Event::AST::Group; use Event::AST::Infix; use Event::AST::Value; unit class EventTranslator; multi method translate(Event::AST @ast --> Array()) { - say $?LINE; - dd @ast; +# say $?LINE; +# dd @ast; @ = @ast.map: { self.translate: $_ } } +multi method translate(Event::AST::LocalVar $ast) { + -> %state { +# dd [:%state, :$ast]; + my $root = %state{ $ast.var-id }; + for $ast.path -> $next { + $root = $root{ $next } // Nil + } + $root<> + } +} + multi method translate(%attrs) { - say $?LINE; +# say $?LINE; + my %attrs-callable = %attrs.kv.map: -> $key, $value { + $key => self.translate: $value + } %( :cmd, - :data(-> %state { %() }) + :data(-> %state { + %attrs-callable.kv.map(-> $key, $value { + $key => do given $value { + when Callable { + .(%state) + } + default { + .self + } + } + }).Hash + }) ) } multi method translate(Event::AST::EventDeclaration $_ where not .body) { - say $?LINE; +# say $?LINE; } -multi method translate(Event::AST::EventDeclaration $_ --> Hash()) { - say $?LINE; - self.translate: .body, (self.translate: $_ with .attrs) +multi method translate(Event::AST::EventDeclaration $ast --> Hash()) { +# say $?LINE; + my %*store = $ast.store; + self.translate: $ast.body, (self.translate: %( :type(ast $ast.name), |$_ ) with $ast.attrs) } multi method translate([Event::AST::EventMatcher $_, *@next ($)], %disp?) { - say $?LINE; +# say $?LINE; %( |self.translate($_), :next(self.translate: @next, %disp) @@ -41,11 +68,11 @@ multi method translate([Event::AST::EventMatcher $_, *@next ($)], %disp?) { multi method translate([Event::AST::EventMatcher $_], %disp?) { self.translate: $_, %disp } multi method translate(Event::AST::EventMatcher $_, %next?) { - say $?LINE; +# say $?LINE; %( :cmd, |(:id($_) with .id), - :store, + |(:store($_) with %*store{ .id }), |( :query(%( |(:type("==" => $_) with .name), @@ -59,18 +86,18 @@ multi method translate(Event::AST::EventMatcher $_, %next?) { multi method translate([]) {} multi method translate(Event::AST::Condition $_) { - say $?LINE; +# say $?LINE; .var => (.op => self.translate: .value) } multi method translate(Event::AST::Value $_) { - say $?LINE; +# say $?LINE; .&ast-value } multi method translate(Event::AST:D $_) { - say $?LINE; - .ⅆ +# say $?LINE; +# .ⅆ { cmd => "query", query => %(