diff options
| author | Paul Buetow <paul@buetow.org> | 2024-02-22 10:40:39 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-02-22 10:40:39 +0200 |
| commit | c071bb3d0a971b67fe54dc0c7903a019421f53a2 (patch) | |
| tree | fcf9339598929369439f6c22c9b7ac2e37281665 /internal/generated | |
| parent | aba6780343525687add6337492ddfd5879cb9253 (diff) | |
add String method to OpId
Diffstat (limited to 'internal/generated')
| -rw-r--r-- | internal/generated/Makefile | 4 | ||||
| -rw-r--r-- | internal/generated/nqc.raku | 54 | ||||
| -rw-r--r-- | internal/generated/types/types.go | 27 |
3 files changed, 64 insertions, 21 deletions
diff --git a/internal/generated/Makefile b/internal/generated/Makefile index 12dd500..34ddf01 100644 --- a/internal/generated/Makefile +++ b/internal/generated/Makefile @@ -4,8 +4,8 @@ generate: types tracepoints .PHONY: types types: - cat ../c/types.h | raku nqc.raku | gofmt | goimports > types/types.go + cat ../c/types.h | raku nqc.raku | goimports | gofmt > types/types.go .PHONY: tracepoints tracepoints: - cat ../c/tracepoints/*.c | raku tracepoints.raku | gofmt | goimports > tracepoints/tracepoints.go + cat ../c/tracepoints/*.c | raku tracepoints.raku | goimports | gofmt > tracepoints/tracepoints.go diff --git a/internal/generated/nqc.raku b/internal/generated/nqc.raku index 65ff9ef..35227f0 100644 --- a/internal/generated/nqc.raku +++ b/internal/generated/nqc.raku @@ -22,31 +22,44 @@ grammar NQC { token identifier { <[a..z A..Z 0..9 _]>+ } token number { \d+ } } - + class NQCToGoActions { - has Str @.const-types is required; + has Str @!const-names; + has Bool $!constant-type-set; method TOP($/) { make "// This file was generated - don't change manually!\n" ~ "package types\n\n" - ~ @!const-types.map({ "type {self!const-camel-case($_)} int\n" }) - ~ $<construct>.map(*.made).join('') + ~ self!constant-go-string-method ~ "\n" + ~ $<construct>.map(*.made).join(''); } - method construct($/) { make $<constant>.made // $<statement>.made // '' } - method statement($/) { make "\n" ~ $<struct>.made ~ "\n"; } + method construct($/) { + make $<constant>.made // $<statement>.made // ''; + } - method constant($/) { - make 'const ' ~ $<identifier> ~ "{self!const-type($/)} = " ~ $<number> ~ "\n" + method statement($/) { + make "\n" ~ $<struct>.made ~ "\n"; } - method !const-type($/) returns Str { - my $suffix = @!const-types.grep({ $<identifier>.ends-with($_) }).first; - $suffix ?? ' ' ~ self!const-camel-case($suffix) !! '' + method constant($/) { + push @!const-names: ~$<identifier>; + my $const-type = $<identifier>.ends-with('_OP_ID') ?? ' OpId' !! ''; + make 'const ' ~ $<identifier> ~ $const-type ~ " = " ~ $<number> ~ "\n"; } - method !const-camel-case(Str $const-name) returns Str { - $const-name.lc.split('_').map(*.tc).join('') + method !constant-go-string-method returns Str { + return qq:to/END/; + type OpId uint32 + + func (o OpId) String() string \{ + \tswitch (o) \{ + \t{@!const-names.grep(/_OP_ID$/).map({ + "case $_:\n" ~ "\t\treturn \"{$_.subst('_OP_ID', '').lc}\"" + }).join("\n\t")} + \t\} + \} + END } method struct($/) { @@ -60,6 +73,7 @@ class NQCToGoActions { method !struct-go-string-method($/) returns Str { my Str $self-ref = $<identifier>.lc.substr(0,1); my Str @format = $<member>.map({ $_.<identifier>.made ~ ':%v' }); + my Str @args = $<member>.map({ my Str $ref = "$self-ref." ~ $_.<identifier>.made; # Need to convert char-arrays into a Go slice, and then convert via string(...) @@ -68,12 +82,14 @@ class NQCToGoActions { return qq:to/END/; func ({$self-ref} {$<identifier>.made}) String() string \{ - \treturn fmt.Sprintf("{@format.join(' ')}", {@args.join(', ')}) + return fmt.Sprintf("{@format.join(' ')}", {@args.join(', ')}) \} END } - method member($/) { make $<identifier>.made ~ ' ' ~ ($<arraysize> // '') ~ $<type>.made } + method member($/) { + make $<identifier>.made ~ ' ' ~ ($<arraysize> // '') ~ $<type>.made; + } method type($/) { make do given ~$/ { @@ -84,8 +100,10 @@ class NQCToGoActions { } } - # Convert identifier from snake_case (C) to CamelCase (Go) - method identifier($/) { make $/.Str.split('_').map(*.tc).join('') } + method identifier($/) { + # Convert identifier from snake_case (C) to CamelCase (Go) + make $/.Str.split('_').map(*.tc).join(''); + } } -say NQC.parse($*IN.slurp, actions => NQCToGoActions.new(const-types => <OP_ID>)).made; +say NQC.parse($*IN.slurp, actions => NQCToGoActions.new).made; diff --git a/internal/generated/types/types.go b/internal/generated/types/types.go index b3cc4d7..848ef6e 100644 --- a/internal/generated/types/types.go +++ b/internal/generated/types/types.go @@ -3,7 +3,32 @@ package types import "fmt" -type OpId int +type OpId uint32 + +func (o OpId) String() string { + switch o { + case OPENAT_ENTER_OP_ID: + return "openat_enter" + case OPENAT_EXIT_OP_ID: + return "openat_exit" + case OPEN_ENTER_OP_ID: + return "open_enter" + case OPEN_EXIT_OP_ID: + return "open_exit" + case CLOSE_ENTER_OP_ID: + return "close_enter" + case CLOSE_EXIT_OP_ID: + return "close_exit" + case WRITE_ENTER_OP_ID: + return "write_enter" + case WRITE_EXIT_OP_ID: + return "write_exit" + case WRITEV_ENTER_OP_ID: + return "writev_enter" + case WRITEV_EXIT_OP_ID: + return "writev_exit" + } +} const MAX_FILENAME_LENGTH = 256 const MAX_PROGNAME_LENGTH = 16 |
