From 478a1eb094a7d9e050cef60f80d9a8af1835dfcf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 8 Mar 2024 09:25:49 +0200 Subject: refactor into roles --- internal/generated/nqc.raku | 165 +++++++++++++++++++++++--------------------- 1 file changed, 85 insertions(+), 80 deletions(-) diff --git a/internal/generated/nqc.raku b/internal/generated/nqc.raku index 77fef91..8df8666 100644 --- a/internal/generated/nqc.raku +++ b/internal/generated/nqc.raku @@ -28,85 +28,8 @@ class Constant { has Int $.value is required; } -class NQCToGoActions { - has Constant @!constants; - has Bool $!constant-type-set; - - method TOP($/) { - make qq:to/END/; - // Code generated - don't change manually! - package types - - {self!constant-go-methods} - {$.map(*.made).join('')} - END - } - - method construct($/) { - make $.made // $.made // ''; - } - - method statement($/) { - make "\n" ~ $.made; - } - - method constant($/) { - push @!constants: Constant.new(:name(~$), :value(+$)); - my $const-type = $.starts-with('SYS_') ?? ' TraceId ' !! ''; - - make qq:to/END/; - const {$}$const-type = {$} - END - } - - method !constant-go-methods returns Str { - qq:to/END/; - type EventType uint32 - type TraceId uint32 - - var traceId2String = map[TraceId]string\{ - {@!constants.grep({ $_.name ~~ /^SYS_/ }).map({ - "{$_.value}: \"{$_.name.subst('SYS_', '').lc}\"" - }).join(', ')}, - \} - - var traceId2Name = map[TraceId]string\{ - {@!constants.grep({ $_.name ~~ /^SYS_/ }).map({ - "{$_.value}: \"{$_.name.subst(/'SYS_ENTER_'|'SYS_EXIT_'/, '').lc}\"" - }).join(', ')}, - \} - - func (s TraceId) String() string \{ - str, ok := traceId2String[s] - if !ok \{ - panic(fmt.Sprintf("no string representation for trace ID %d found", s)) - \} - return str - \} - - func (s TraceId) Name() string \{ - str, ok := traceId2Name[s] - if !ok \{ - panic(fmt.Sprintf("no name for trace ID %d found", s)) - \} - return str - \} - END - } - - method struct($/) { - make qq:to/END/; - type {$.made} struct \{ - {$.map(*.made).join('; ')} - \} - - {self!struct-go-methods($/)} - {($.made.ends-with('Event') ?? "\n" ~ self!struct-go-sync-pool($/) !! '')} - END - } - - # Generate String() and other methods on the Go struct, for pretty printing. - method !struct-go-methods($/) returns Str { +role StructGoMethods { + method struct-go-methods($/) returns Str { my Str $self-ref = $.lc.substr(0,1); my Str @format = $.map({ $_..made ~ ':%v' }); @@ -143,7 +66,7 @@ class NQCToGoActions { END } - method !struct-go-sync-pool($/) returns Str { + method struct-go-sync-pool($/) returns Str { my Str $identifier = $/.made; my Str $self-ref = $identifier.lc.substr(0,1); @@ -166,6 +89,88 @@ class NQCToGoActions { \} END } +} + +role ConstantGoMethods { + has Constant @!constants; + + method constant-go-methods returns Str { + qq:to/END/; + type EventType uint32 + type TraceId uint32 + + var traceId2String = map[TraceId]string\{ + {@!constants.grep({ $_.name ~~ /^SYS_/ }).map({ + "{$_.value}: \"{$_.name.subst('SYS_', '').lc}\"" + }).join(', ')}, + \} + + var traceId2Name = map[TraceId]string\{ + {@!constants.grep({ $_.name ~~ /^SYS_/ }).map({ + "{$_.value}: \"{$_.name.subst(/'SYS_ENTER_'|'SYS_EXIT_'/, '').lc}\"" + }).join(', ')}, + \} + + func (s TraceId) String() string \{ + str, ok := traceId2String[s] + if !ok \{ + panic(fmt.Sprintf("no string representation for trace ID %d found", s)) + \} + return str + \} + + func (s TraceId) Name() string \{ + str, ok := traceId2Name[s] + if !ok \{ + panic(fmt.Sprintf("no name for trace ID %d found", s)) + \} + return str + \} + END + } + +} + +class NQCToGoActions does StructGoMethods does ConstantGoMethods { + has Bool $!constant-type-set; + + method TOP($/) { + make qq:to/END/; + // Code generated - don't change manually! + package types + + {self.constant-go-methods} + {$.map(*.made).join('')} + END + } + + method construct($/) { + make $.made // $.made // ''; + } + + method statement($/) { + make "\n" ~ $.made; + } + + method constant($/) { + push @!constants: Constant.new(:name(~$), :value(+$)); + my $const-type = $.starts-with('SYS_') ?? ' TraceId ' !! ''; + + make qq:to/END/; + const {$}$const-type = {$} + END + } + + method struct($/) { + make qq:to/END/; + type {$.made} struct \{ + {$.map(*.made).join('; ')} + \} + + {self.struct-go-methods($/)} + {($.made.ends-with('Event') ?? "\n" ~ self.struct-go-sync-pool($/) !! '')} + END + } method member($/) { my Str $type = $.made eq 'TraceId' ?? 'TraceId' !! $.made; -- cgit v1.2.3