From bd45eff13ce49f57961518e0e1c9734968e9af55 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Mon, 14 Jul 2025 01:32:58 +0800 Subject: [PATCH] Fix generated files destination --- tool/rawstr4c/lib/Generator.rakumod | 28 +++++++++++++++++----------- tool/rawstr4c/lib/Parser.rakumod | 14 +++++++++----- tool/rawstr4c/rawstr4c.raku | 9 +++------ 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/tool/rawstr4c/lib/Generator.rakumod b/tool/rawstr4c/lib/Generator.rakumod index 6941212..a2b63f9 100644 --- a/tool/rawstr4c/lib/Generator.rakumod +++ b/tool/rawstr4c/lib/Generator.rakumod @@ -4,7 +4,7 @@ # File Authors : Aoran Zeng # Contributors : Nul None # Created On : <2025-07-12> -# Last Modified : <2025-07-13> +# Last Modified : <2025-07-14> # # Generates C code from raw string # --------------------------------------------------------------- @@ -121,7 +121,7 @@ my class CVariableGenerator { /** * Generated by rawstr4c * - * Date: {DateTime.now.Str} + * Date: {DateTime.now.Str} */ EOF @@ -146,14 +146,14 @@ my class CVariableGenerator { method generate-c-source-file() { my $source = qq:to/EOF/; - #include "{$.c-header-filename}" - /** * Generated by rawstr4c * - * Date: {DateTime.now.Str} + * Date: {DateTime.now.Str} */ + #include "{$.c-header-filename}" + EOF for @.variables -> $var { @@ -165,16 +165,21 @@ my class CVariableGenerator { return $source; } - method save-files($output-mode) { - $.c-header-filename.IO.spurt(self.generate-c-header-file($output-mode)); - say "Generated C header file: $.c-header-filename"; + + method save-files($output-mode, $dest-dir) { + + my $c-header-file = $dest-dir.IO.child($.c-header-filename).Str; + + $c-header-file.IO.spurt(self.generate-c-header-file($output-mode)); + say "Generated C header file: $c-header-file"; if $output-mode eq 'global-variable' { my $has-globals = @.variables.grep({ $_ eq 'global-variable' }).elems > 0; if $has-globals { my $c-source-filename = $.c-header-filename.subst(/'.h'$/, '.c'); - $c-source-filename.IO.spurt(self.generate-c-source-file()); - say "Generated C source file: $c-source-filename"; + my $c-source-file = $dest-dir.IO.child($c-source-filename).Str; + $c-source-file.IO.spurt(self.generate-c-source-file()); + say "Generated C source file: $c-source-file"; } } } @@ -261,7 +266,8 @@ class Generator { # 最后把累计到 @variables 的内容输出到文件 if $output-mode ne 'terminal' { - $.variable-generator.save-files($output-mode); + 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 7f172bc..41fd9ba 100644 --- a/tool/rawstr4c/lib/Parser.rakumod +++ b/tool/rawstr4c/lib/Parser.rakumod @@ -4,7 +4,7 @@ # File Authors : Aoran Zeng # Contributors : Nul None # Created On : <2025-07-12> -# Last Modified : <2025-07-13> +# Last Modified : <2025-07-14> # # rawstr4c.md parsing # --------------------------------------------------------------- @@ -107,8 +107,9 @@ my class Config { 我们要求,在 Global dom 里,只存在配置,不存在 code block. 而 code block 只能在 Section dom 中存在。 因此,Parser 解析完毕后将包含: - - $global-config - - @sections (多个 $section) + - IO::Path $input-file + - Hash $global-config + - @sections is Array[Hash] (多个 $section) 一个 $section 是 Hash,其包含: - title @@ -117,11 +118,13 @@ my class Config { - config ) class Parser { + has $.input-file is rw; has $.global-config; has @.sections; - method new() { + method new(:$input-file) { self.bless( + :$input-file, global-config => Config.new(), sections => [] ); @@ -139,7 +142,8 @@ class Parser { return False; } - method parse($content) { + method parse() { + my $content = $.input-file.slurp; my @lines = $content.lines; my $current-section; diff --git a/tool/rawstr4c/rawstr4c.raku b/tool/rawstr4c/rawstr4c.raku index fc87819..019808d 100644 --- a/tool/rawstr4c/rawstr4c.raku +++ b/tool/rawstr4c/rawstr4c.raku @@ -7,7 +7,7 @@ # File Authors : Aoran Zeng # Contributors : Nul None # Created On : <2025-07-12> -# Last Modified : <2025-07-13> +# Last Modified : <2025-07-14> # # rawstr4c: # @@ -41,11 +41,8 @@ sub MAIN( die "Error: '$input-path' is neither a file nor a directory\n"; } - my $content = $markdown-file.IO.slurp; - - my $parser = Parser::Parser.new(); - - $parser.parse($content); + my $parser = Parser::Parser.new(input-file=>$markdown-file); + $parser.parse; $parser.debug if $debug; Generator::Generator.new.generate($parser);