mirror of
https://github.com/RubyMetric/chsrc
synced 2025-07-21 23:59:33 +08:00
Add type
This commit is contained in:
parent
5ac415801c
commit
02f08acf5c
@ -88,14 +88,14 @@ my class CVariableNameGenerator {
|
|||||||
$name = $name.subst(/_+$/, '');
|
$name = $name.subst(/_+$/, '');
|
||||||
|
|
||||||
# 组装变量名
|
# 组装变量名
|
||||||
my $var-name = "";
|
my $varname = "";
|
||||||
$var-name ~= $prefix if $keep-prefix && $prefix;
|
$varname ~= $prefix if $keep-prefix && $prefix;
|
||||||
$var-name ~= "_" if $var-name && $name;
|
$varname ~= "_" if $varname && $name;
|
||||||
$var-name ~= $name if $name;
|
$varname ~= $name if $name;
|
||||||
$var-name ~= "_" if $var-name && $postfix && $keep-postfix;
|
$varname ~= "_" if $varname && $postfix && $keep-postfix;
|
||||||
$var-name ~= $postfix if $postfix && $keep-postfix;
|
$varname ~= $postfix if $postfix && $keep-postfix;
|
||||||
|
|
||||||
return $var-name || "unnamed_var";
|
return $varname || "unnamed_var";
|
||||||
}
|
}
|
||||||
|
|
||||||
method resolve-postfix($global-config, $section-config, $language) {
|
method resolve-postfix($global-config, $section-config, $language) {
|
||||||
@ -115,8 +115,8 @@ my class CVariableNameGenerator {
|
|||||||
|
|
||||||
#| 生成 .h 文件或/和 .c 文件,或存储到 @variables 中
|
#| 生成 .h 文件或/和 .c 文件,或存储到 @variables 中
|
||||||
my class CVariableGenerator {
|
my class CVariableGenerator {
|
||||||
has @.variables;
|
has Hash @.variables;
|
||||||
has $.c-header-filename;
|
has Str $.c-header-filename;
|
||||||
|
|
||||||
method new() {
|
method new() {
|
||||||
|
|
||||||
@ -125,11 +125,12 @@ my class CVariableGenerator {
|
|||||||
self.bless(:$c-header-filename, :variables([]));
|
self.bless(:$c-header-filename, :variables([]));
|
||||||
}
|
}
|
||||||
|
|
||||||
method add-variable($name, $content, $type) {
|
#| C变量名,C变量值, 生成类型
|
||||||
|
method add-variable($name, $value, $kind) {
|
||||||
@.variables.push: {
|
@.variables.push: {
|
||||||
name => $name,
|
name => $name,
|
||||||
content => $content,
|
value => $value,
|
||||||
type => $type
|
kind => $kind
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,16 +147,16 @@ my class CVariableGenerator {
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
for @.variables -> $var {
|
for @.variables -> $var {
|
||||||
given $var<type> {
|
given $var<kind> {
|
||||||
when 'global-variable' {
|
when 'global-variable' {
|
||||||
if $output-mode eq 'global-variable-only-header' {
|
if $output-mode eq 'global-variable-only-header' {
|
||||||
$header ~= "char {$var<name>}[] = \"{$var<content>}\";\n\n";
|
$header ~= "char {$var<name>}[] = \"{$var<value>}\";\n\n";
|
||||||
} else {
|
} else {
|
||||||
$header ~= "extern char {$var<name>}[];\n";
|
$header ~= "extern char {$var<name>}[];\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
when 'macro' {
|
when 'macro' {
|
||||||
$header ~= "#define {$var<name>.uc} \"{$var<content>}\"\n\n";
|
$header ~= "#define {$var<name>.uc} \"{$var<value>}\"\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,8 +177,8 @@ my class CVariableGenerator {
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
for @.variables -> $var {
|
for @.variables -> $var {
|
||||||
if $var<type> eq 'global-variable' {
|
if $var<kind> eq 'global-variable' {
|
||||||
$source ~= "char {$var<name>}[] = \"{$var<content>}\";\n";
|
$source ~= "char {$var<name>}[] = \"{$var<value>}\";\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +194,7 @@ my class CVariableGenerator {
|
|||||||
say "Generated C header file: $c-header-file";
|
say "Generated C header file: $c-header-file";
|
||||||
|
|
||||||
if $output-mode eq 'global-variable' {
|
if $output-mode eq 'global-variable' {
|
||||||
my $has-globals = @.variables.grep({ $_<type> eq 'global-variable' }).elems > 0;
|
my $has-globals = @.variables.grep({ $_<kind> eq 'global-variable' }).elems > 0;
|
||||||
if $has-globals {
|
if $has-globals {
|
||||||
my $c-source-filename = $.c-header-filename.subst(/'.h'$/, '.c');
|
my $c-source-filename = $.c-header-filename.subst(/'.h'$/, '.c');
|
||||||
my $c-source-file = $dest-dir.IO.child($c-source-filename).Str;
|
my $c-source-file = $dest-dir.IO.child($c-source-filename).Str;
|
||||||
@ -206,10 +207,11 @@ my class CVariableGenerator {
|
|||||||
|
|
||||||
|
|
||||||
class Generator {
|
class Generator {
|
||||||
has $.parser;
|
|
||||||
has $.cstring-converter;
|
has Parser::Parser $.parser;
|
||||||
has $.varname-generator;
|
has CStringConverter $.cstring-converter;
|
||||||
has $.variable-generator;
|
has CVariableNameGenerator $.varname-generator;
|
||||||
|
has CVariableGenerator $.variable-generator;
|
||||||
|
|
||||||
method new($parser) {
|
method new($parser) {
|
||||||
self.bless(
|
self.bless(
|
||||||
@ -246,13 +248,13 @@ class Generator {
|
|||||||
$global-config, $section-config, 'translate', ':escape'
|
$global-config, $section-config, 'translate', ':escape'
|
||||||
).as-mode();
|
).as-mode();
|
||||||
|
|
||||||
my $var-name = $.varname-generator.generate($global-config, $section-config, $title);
|
my $varname = $.varname-generator.generate($global-config, $section-config, $title);
|
||||||
|
|
||||||
my $output-mode = $global-config.get('output', ':terminal').as-mode();
|
my $output-mode = $global-config.get('output', ':terminal').as-mode();
|
||||||
|
|
||||||
if $debug-parser {
|
if $debug-parser {
|
||||||
say "--- Section: $title ---";
|
say "--- Section: $title ---";
|
||||||
say "Variable name = $var-name";
|
say "Variable name = $varname";
|
||||||
say "Translation mode = $translate-mode";
|
say "Translation mode = $translate-mode";
|
||||||
say "Output mode = $output-mode";
|
say "Output mode = $output-mode";
|
||||||
|
|
||||||
@ -267,14 +269,14 @@ class Generator {
|
|||||||
|
|
||||||
given $output-mode {
|
given $output-mode {
|
||||||
when 'terminal' {
|
when 'terminal' {
|
||||||
say 'char ' ~ $var-name ~ '[] = "' ~ $c-string ~ '";';
|
say 'char ' ~ $varname ~ '[] = "' ~ $c-string ~ '";';
|
||||||
say "";
|
say "";
|
||||||
}
|
}
|
||||||
when 'global-variable' | 'global-variable-only-header' {
|
when 'global-variable' | 'global-variable-only-header' {
|
||||||
$.variable-generator.add-variable($var-name, $c-string, 'global-variable');
|
$.variable-generator.add-variable($varname, $c-string, 'global-variable');
|
||||||
}
|
}
|
||||||
when 'macro' {
|
when 'macro' {
|
||||||
$.variable-generator.add-variable($var-name, $c-string, 'macro');
|
$.variable-generator.add-variable($varname, $c-string, 'macro');
|
||||||
}
|
}
|
||||||
default {
|
default {
|
||||||
die "Illegal output mode: $output-mode";
|
die "Illegal output mode: $output-mode";
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# File Authors : Aoran Zeng <ccmywish@qq.com>
|
# File Authors : Aoran Zeng <ccmywish@qq.com>
|
||||||
# Contributors : Nul None <nul@none.org>
|
# Contributors : Nul None <nul@none.org>
|
||||||
# Created On : <2025-07-12>
|
# Created On : <2025-07-12>
|
||||||
# Last Modified : <2025-07-14>
|
# Last Modified : <2025-07-15>
|
||||||
#
|
#
|
||||||
# rawstr4c.md parsing
|
# rawstr4c.md parsing
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
@ -18,8 +18,8 @@ my enum ConfigValueType <String Mode Boolean>;
|
|||||||
#| 配置项的值
|
#| 配置项的值
|
||||||
my class ConfigValue {
|
my class ConfigValue {
|
||||||
has ConfigValueType $.type;
|
has ConfigValueType $.type;
|
||||||
has $.raw-value;
|
has Str $.raw-value;
|
||||||
has $.parsed-value;
|
has Any $.parsed-value;
|
||||||
|
|
||||||
method new($raw-text) {
|
method new($raw-text) {
|
||||||
my $type;
|
my $type;
|
||||||
@ -108,20 +108,20 @@ my class Config {
|
|||||||
我们要求,在 Global dom 里,只存在配置,不存在 code block. 而 code block 只能在 Section dom 中存在。
|
我们要求,在 Global dom 里,只存在配置,不存在 code block. 而 code block 只能在 Section dom 中存在。
|
||||||
|
|
||||||
因此,Parser 解析完毕后将包含:
|
因此,Parser 解析完毕后将包含:
|
||||||
- IO::Path $input-file
|
- input-file
|
||||||
- Hash $global-config
|
- global-config
|
||||||
- @sections is Array[Hash] (多个 $section)
|
- sections (多个 section)
|
||||||
|
|
||||||
一个 $section 是 Hash,其包含:
|
一个 section 是 Hash,其包含:
|
||||||
- title
|
- title
|
||||||
- level
|
- level
|
||||||
- raw-string
|
- raw-string
|
||||||
- config
|
- config
|
||||||
)
|
)
|
||||||
class Parser {
|
class Parser {
|
||||||
has $.input-file is rw;
|
has IO::Path $.input-file is rw;
|
||||||
has $.global-config;
|
has Config $.global-config;
|
||||||
has @.sections;
|
has Hash @.sections;
|
||||||
|
|
||||||
method new($input-file) {
|
method new($input-file) {
|
||||||
self.bless(
|
self.bless(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user