mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-22 08:10:06 +08:00
Fix multiple output kinds
This commit is contained in:
parent
b1a45fb4d7
commit
85d8ee940c
@ -134,7 +134,8 @@ my class CVariableGenerator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
method generate-c-header-file($output-mode = 'global-variable') {
|
#| 生成 C 头文件的内容
|
||||||
|
method generate-c-header-file() {
|
||||||
my $header = qq:to/EOF/;
|
my $header = qq:to/EOF/;
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@ -148,22 +149,25 @@ my class CVariableGenerator {
|
|||||||
|
|
||||||
for @.variables -> $var {
|
for @.variables -> $var {
|
||||||
given $var<kind> {
|
given $var<kind> {
|
||||||
|
when 'global-variable-only-header' {
|
||||||
|
$header ~= "char {$var<name>}[] = \"{$var<value>}\";\n\n";
|
||||||
|
}
|
||||||
when 'global-variable' {
|
when 'global-variable' {
|
||||||
if $output-mode eq 'global-variable-only-header' {
|
$header ~= "extern char {$var<name>}[];\n";
|
||||||
$header ~= "char {$var<name>}[] = \"{$var<value>}\";\n\n";
|
|
||||||
} else {
|
|
||||||
$header ~= "extern char {$var<name>}[];\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
when 'macro' {
|
when 'macro' {
|
||||||
$header ~= "#define {$var<name>.uc} \"{$var<value>}\"\n\n";
|
$header ~= "#define {$var<name>.uc} \"{$var<value>}\"\n\n";
|
||||||
}
|
}
|
||||||
|
default {
|
||||||
|
die "Unknown variable kind: {$var<kind>}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $header;
|
return $header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#| 生成 C 源文件的内容
|
||||||
method generate-c-source-file() {
|
method generate-c-source-file() {
|
||||||
my $source = qq:to/EOF/;
|
my $source = qq:to/EOF/;
|
||||||
/**
|
/**
|
||||||
@ -181,26 +185,25 @@ my class CVariableGenerator {
|
|||||||
$source ~= "char {$var<name>}[] = \"{$var<value>}\";\n";
|
$source ~= "char {$var<name>}[] = \"{$var<value>}\";\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $source;
|
return $source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
method save-files($output-mode, $dest-dir) {
|
method save-files($dest-dir) {
|
||||||
|
|
||||||
my $c-header-file = $dest-dir.IO.child($.c-header-filename).Str;
|
my $c-header-file = $dest-dir.IO.child($.c-header-filename).Str;
|
||||||
|
|
||||||
$c-header-file.IO.spurt(self.generate-c-header-file($output-mode));
|
$c-header-file.IO.spurt(self.generate-c-header-file());
|
||||||
say "Generated C header file: $c-header-file";
|
say "Generated C header file: $c-header-file";
|
||||||
|
|
||||||
if $output-mode eq 'global-variable' {
|
# 检查是否有 "头、源并存的变量",如果有就使用并存的头文件和源文件模式
|
||||||
my $has-globals = @.variables.grep({ $_<kind> eq 'global-variable' }).elems > 0;
|
my $need-gen-c-source-file = @.variables.grep({ $_<kind> eq 'global-variable' }).elems > 0;
|
||||||
if $has-globals {
|
|
||||||
my $c-source-filename = $.c-header-filename.subst(/'.h'$/, '.c');
|
if $need-gen-c-source-file {
|
||||||
my $c-source-file = $dest-dir.IO.child($c-source-filename).Str;
|
my $c-source-filename = $.c-header-filename.subst(/'.h'$/, '.c');
|
||||||
$c-source-file.IO.spurt(self.generate-c-source-file());
|
my $c-source-file = $dest-dir.IO.child($c-source-filename).Str;
|
||||||
say "Generated C source file: $c-source-file";
|
$c-source-file.IO.spurt(self.generate-c-source-file());
|
||||||
}
|
say "Generated C source file: $c-source-file";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -258,9 +261,12 @@ class Generator {
|
|||||||
say 'char ' ~ $varname ~ '[] = "' ~ $c-string ~ '";';
|
say 'char ' ~ $varname ~ '[] = "' ~ $c-string ~ '";';
|
||||||
say "";
|
say "";
|
||||||
}
|
}
|
||||||
when 'global-variable' | 'global-variable-only-header' {
|
when 'global-variable' {
|
||||||
$.variable-generator.add-variable($varname, $c-string, 'global-variable');
|
$.variable-generator.add-variable($varname, $c-string, 'global-variable');
|
||||||
}
|
}
|
||||||
|
when 'global-variable-only-header' {
|
||||||
|
$.variable-generator.add-variable($varname, $c-string, 'global-variable-only-header');
|
||||||
|
}
|
||||||
when 'macro' {
|
when 'macro' {
|
||||||
$.variable-generator.add-variable($varname, $c-string, 'macro');
|
$.variable-generator.add-variable($varname, $c-string, 'macro');
|
||||||
}
|
}
|
||||||
@ -286,12 +292,7 @@ class Generator {
|
|||||||
# 如果有任何变量被添加 (没有被输出到终端),就保存文件
|
# 如果有任何变量被添加 (没有被输出到终端),就保存文件
|
||||||
if $.variable-generator.variables.elems > 0 {
|
if $.variable-generator.variables.elems > 0 {
|
||||||
my $dest-dir = $.parser.input-file.IO.dirname.Str;
|
my $dest-dir = $.parser.input-file.IO.dirname.Str;
|
||||||
|
$.variable-generator.save-files($dest-dir);
|
||||||
# 检查是否有 "头、源并存的变量",如果有就使用并存的头文件和源文件模式
|
|
||||||
my $has-global-vars = $.variable-generator.variables.grep({ $_<kind> eq 'global-variable' }).elems > 0;
|
|
||||||
my $output-mode = $has-global-vars ?? 'global-variable' !! 'global-variable-only-header';
|
|
||||||
|
|
||||||
$.variable-generator.save-files($output-mode, $dest-dir);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user