From e3dd337b2856b859bdad68f718526b55ae338b3c Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Mon, 14 Jul 2025 01:42:13 +0800 Subject: [PATCH] Refined output --- tool/rawstr4c/lib/Generator.rakumod | 30 +++++++++++++++-------------- tool/rawstr4c/lib/Parser.rakumod | 6 +++--- tool/rawstr4c/rawstr4c.raku | 4 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/tool/rawstr4c/lib/Generator.rakumod b/tool/rawstr4c/lib/Generator.rakumod index a2b63f9..50a1837 100644 --- a/tool/rawstr4c/lib/Generator.rakumod +++ b/tool/rawstr4c/lib/Generator.rakumod @@ -130,13 +130,13 @@ my class CVariableGenerator { given $var { when 'global-variable' { if $output-mode eq 'global-variable-only-header' { - $header ~= "char {$var}[] = \"{$var}\";\n"; + $header ~= "char {$var}[] = \"{$var}\";\n\n"; } else { $header ~= "extern char {$var}[];\n"; } } when 'macro' { - $header ~= "#define {$var.uc} \"{$var}\"\n"; + $header ~= "#define {$var.uc} \"{$var}\"\n\n"; } } } @@ -187,12 +187,14 @@ my class CVariableGenerator { class Generator { + has $.parser; has $.cstring-converter; has $.varname-generator; has $.variable-generator; - method new() { + method new($parser) { self.bless( + :$parser, :cstring-converter(CStringConverter.new), :varname-generator(CVariableNameGenerator.new), :variable-generator(CVariableGenerator.new) @@ -214,8 +216,6 @@ class Generator { return unless $code; - say "=== Section: $title ==="; - my $translate-mode = self.get-config-value( $global-config, $section-config, 'translate', ':escape' ).as-mode(); @@ -225,12 +225,13 @@ class Generator { my $output-mode = $global-config.get('output', ':terminal').as-mode(); if $debug-parser { - say "Variable name: $var-name"; - say "Translation mode: $translate-mode"; - say "Output mode: $output-mode"; + say "--- Section: $title ---"; + say "Variable name = $var-name"; + say "Translation mode = $translate-mode"; + say "Output mode = $output-mode"; my $language = $section-config.get('language', 'None').as-string(); - say "Language: $language"; + say "Language = $language"; say ''; } @@ -239,6 +240,7 @@ class Generator { given $output-mode { when 'terminal' { say 'char ' ~ $var-name ~ '[] = "' ~ $c-string ~ '";'; + say ""; } when 'global-variable' | 'global-variable-only-header' { $.variable-generator.add-variable($var-name, $c-string, 'global-variable'); @@ -250,15 +252,15 @@ class Generator { die "Illegal output mode: $output-mode"; } } - say "\n"; } - method generate($parser) { - my $global-config = $parser.global-config; + method generate() { + + my $global-config = $.parser.global-config; # 这个 generate-for-section() 要么把变量输出到终端,要么累计到 @variabels 中 - for $parser.sections -> $section { + for $.parser.sections -> $section { self.generate-for-section($global-config, $section); } @@ -266,7 +268,7 @@ class Generator { # 最后把累计到 @variables 的内容输出到文件 if $output-mode ne 'terminal' { - my $dest-dir = $parser.input-file.dirname.Str; + my $dest-dir = $.parser.input-file.dirname.Str; $.variable-generator.save-files($output-mode, $dest-dir); } } diff --git a/tool/rawstr4c/lib/Parser.rakumod b/tool/rawstr4c/lib/Parser.rakumod index 41fd9ba..7cae441 100644 --- a/tool/rawstr4c/lib/Parser.rakumod +++ b/tool/rawstr4c/lib/Parser.rakumod @@ -122,7 +122,7 @@ class Parser { has $.global-config; has @.sections; - method new(:$input-file) { + method new($input-file) { self.bless( :$input-file, global-config => Config.new(), @@ -231,10 +231,10 @@ class Parser { # 输出 config, 包含 global config 以及 section config method debug() { - say "Global config:"; + say "--- Global config ---"; for $.global-config.keys.sort -> $item { my $value = $.global-config.get($item); - say " $item = {$value.as-string} (type: {$value.type})"; + say "$item = {$value.as-string} (type: {$value.type})"; } say ""; diff --git a/tool/rawstr4c/rawstr4c.raku b/tool/rawstr4c/rawstr4c.raku index 019808d..d8c7713 100644 --- a/tool/rawstr4c/rawstr4c.raku +++ b/tool/rawstr4c/rawstr4c.raku @@ -41,9 +41,9 @@ sub MAIN( die "Error: '$input-path' is neither a file nor a directory\n"; } - my $parser = Parser::Parser.new(input-file=>$markdown-file); + my $parser = Parser::Parser.new($markdown-file); $parser.parse; $parser.debug if $debug; - Generator::Generator.new.generate($parser); + Generator::Generator.new($parser).generate; }