#!/usr/bin/env raku # # This Raku program takes a list of C struct and constant definitions and converts # it to valid Go code. use v6.d; #use Grammar::Debugger; # Not quite C grammar NQC { rule TOP { * } rule construct { | | } rule constant { '#define' } rule statement { ';' } rule struct { 'struct' '{' + %% ';' '}' } rule member { ? } rule comment { | } rule single-line-comment { '//' <-[\n]>+ } rule multi-line-comment { '/*' .*? '*/' } token arraysize { '[' ']' } token type { 'char' | '__s32' | '__u32' | '__u64' } token identifier { <[a..z A..Z 0..9 _]>+ } token number { \d+ } } class NQCToGoActions { 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" ~ self!constant-go-string-method ~ "\n" ~ $.map(*.made).join(''); } method construct($/) { make $.made // $.made // ''; } method statement($/) { make "\n" ~ $.made; } method constant($/) { push @!const-names: ~$; my $const-type = $.ends-with('_OP_ID') ?? ' OpId' !! ''; make qq:to/END/; const {$}$const-type = {$} END } method !constant-go-string-method returns Str { qq:to/END/; type OpId uint32 func (o OpId) String() string \{ switch (o) \{ {@!const-names.grep(/_OP_ID$/).map({ "case $_: return \"{$_.subst('_OP_ID', '').lc}\"" }).join('; ')} default: panic(fmt.Sprintf("Unknown OpId: %d", o)) \} \} END } method struct($/) { make qq:to/END/; type {$.made} struct \{ {$.map(*.made).join('; ')} \} {self!struct-go-string-method($/)} {($.made.ends-with('Event') ?? "\n" ~ self!struct-go-sync-pool($/) !! '')} END } # Generate String() method on the Go struct, for pretty printing. method !struct-go-string-method($/) returns Str { my Str $self-ref = $.lc.substr(0,1); my Str @format = $.map({ $_..made ~ ':%v' }); my Str @args = $.map({ my Str $ref = "$self-ref." ~ $_..made; # Need to convert char-arrays into a Go slice, and then convert via string(...) ($_. eq 'char' && $_.) ?? "string({$ref}[:])" !! $ref; }); qq:to/END/; func ($self-ref {$.made}) String() string \{ return fmt.Sprintf("{@format.join(' ')}", {@args.join(', ')}) \} END } method !struct-go-sync-pool($/) returns Str { my Str $identifier = $/.made; qq:to/END/; var poolOf{$identifier}s = sync.Pool\{ New: func() interface\{\} \{ return &$identifier\{\} \}, \} func New{$identifier}() *$identifier \{ return poolOf{$identifier}s.Get().(*$identifier); \} func Recycle{$identifier}(elem *$identifier) \{ poolOf{$identifier}s.Put(elem) \} END } method member($/) { my Str $type = $.made eq 'OpId' ?? 'OpId' !! $.made; make $.made ~ ' ' ~ ($ // '') ~ $type; } method type($/) { make do given ~$/ { when 'char' { 'byte' } when '__s32' { 'int32' } when '__u32' { 'uint32' } when '__u64' { 'uint64' } } } 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).made;