Don't print directly from the code generators, instead append to an output string for printing at a later time.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5374 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-07-03 21:51:23 +00:00
parent bf5230b419
commit 6e853b04d1
4 changed files with 137 additions and 128 deletions

View File

@ -8,16 +8,16 @@ use strict;
sub printStructFieldType sub printStructFieldType
{ {
my $field = $_[0]; my ($output, $field) = @_;
$_ = ${$field}{"type"}; $_ = ${$field}{"type"};
if (/count/) { print "unsigned int "; } if (/count/) { $$output .= "unsigned int "; }
elsif (/string/) { print "const char* "; } elsif (/string/) { $$output .= "const char* "; }
elsif (/real/) { print "float "; } elsif (/real/) { $$output .= "float "; }
elsif (/bool/) { print "bool "; } elsif (/bool/) { $$output .= "bool "; }
elsif (/set/) { print "bool "; } elsif (/set/) { $$output .= "bool "; }
elsif (/enum/) { print "${$field}{\"enum\"} "; } elsif (/enum/) { $$output .= "${$field}{\"enum\"} "; }
else { die "UKNOWN TYPE: $_"; } else { die "UKNOWN TYPE: $_"; }
} }
@ -49,7 +49,7 @@ sub preProcessField
sub postProcessField sub postProcessField
{ {
my ($field, $enumMap) = @_; my ($output, $field, $enumMap) = @_;
$_ = ${$field}{"type"}; $_ = ${$field}{"type"};
$_ = "" unless $_; $_ = "" unless $_;
@ -59,57 +59,58 @@ sub postProcessField
my $enum = ${$enumMap}{$enumName}; my $enum = ${$enumMap}{$enumName};
my $enumSize = @{${$enum}{"values"}}; my $enumSize = @{${$enum}{"values"}};
print "\[${enumSize}]" if (/set/); $$output .= "\[${enumSize}]" if (/set/);
} }
} }
sub printComments sub printComments
{ {
my ($comments, $indent) = @_; my ($output, $comments, $indent) = @_;
return unless @{$comments}; return unless @{$comments};
print "\t" if $indent; $$output .= "\t" if $indent;
print "/**\n"; $$output .= "/**\n";
foreach my $comment (@{$comments}) foreach my $comment (@{$comments})
{ {
print "\t" if $indent; $$output .= "\t" if $indent;
print " *${comment}\n"; $$output .= " *${comment}\n";
} }
print "\t" if $indent; $$output .= "\t" if $indent;
print " */\n"; $$output .= " */\n";
} }
sub printStructFields sub printStructFields
{ {
my @fields = @{${$_[0]}{"fields"}}; my $output = $_[0];
my $enumMap = $_[1]; my @fields = @{${$_[1]}{"fields"}};
my $enumMap = $_[2];
while (@fields) while (@fields)
{ {
my $field = shift(@fields); my $field = shift(@fields);
my @comments = @{${$field}{"comment"}}; my @comments = @{${$field}{"comment"}};
preProcessField $field, \@comments; preProcessField($field, \@comments);
printComments \@comments, 1; printComments($output, \@comments, 1);
print "\t"; $$output .= "\t";
printStructFieldType $field; printStructFieldType($output, $field);
print ${$field}{"name"}; $$output .= ${$field}{"name"};
postProcessField $field, $enumMap; postProcessField($output, $field, $enumMap);
print ";\n"; $$output .= ";\n";
print "\n" if @fields; $$output .= "\n" if @fields;
} }
} }
sub printStructContent sub printStructContent
{ {
my ($struct, $name, $prefix, $structMap, $enumMap) = @_; my ($output, $struct, $name, $prefix, $structMap, $enumMap) = @_;
foreach (keys %{${$struct}{"qualifiers"}}) foreach (keys %{${$struct}{"qualifiers"}})
{ {
@ -120,24 +121,25 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"}; my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName}; my $inheritStruct = ${$structMap}{$inheritName};
print "\t/* BEGIN of inherited \"$inheritName\" definition */\n"; $$output .= "\t/* BEGIN of inherited \"$inheritName\" definition */\n";
printStructContent($inheritStruct, $name, $prefix, $structMap, $enumMap); printStructContent($output, $inheritStruct, $name, $prefix, $structMap, $enumMap);
print "\t/* END of inherited \"$inheritName\" definition */\n\n"; $$output .= "\t/* END of inherited \"$inheritName\" definition */\n";
} }
} }
$$name = ${$struct}{"name"}; $$name = ${$struct}{"name"};
printStructFields($struct, $enumMap); printStructFields($output, $struct, $enumMap);
} }
sub printEnum() sub printEnum()
{ {
my ($enum) = @_; my ($output, $enum) = @_;
printComments ${$enum}{"comment"}, 0; printComments($output, ${$enum}{"comment"}, 0);
print "typedef enum\n{\n"; # Start printing the enum
$$output .= "typedef enum\n{\n";
my @values = @{${$enum}{"values"}}; my @values = @{${$enum}{"values"}};
@ -146,66 +148,67 @@ sub printEnum()
my $value = shift(@values); my $value = shift(@values);
my $name = ${$value}{"name"}; my $name = ${$value}{"name"};
printComments ${$value}{"comment"}, 1; printComments($output, ${$value}{"comment"}, 1);
print "\t${$enum}{\"name\"}_${name},\n"; $$output .= "\t${$enum}{\"name\"}_${name},\n";
print "\n" if @values; $$output .= "\n" if @values;
} }
print "} ${$enum}{\"name\"};\n\n"; # Finish printing the enum
$$output .= "} ${$enum}{\"name\"};\n\n";
} }
sub printStruct() sub printStruct()
{ {
my ($struct, $structMap, $enumMap) = @_; my ($output, $struct, $structMap, $enumMap) = @_;
my $name; my $name;
my $prefix = ""; my $prefix = "";
printComments ${$struct}{"comment"}, 0; printComments($output, ${$struct}{"comment"}, 0);
# Start printing the structure # Start printing the structure
print "typedef struct\n{\n"; $$output .= "typedef struct\n{\n";
printStructContent($struct, \$name, \$prefix, $structMap, $enumMap); printStructContent($output, $struct, \$name, \$prefix, $structMap, $enumMap);
# Finish printing the structure # Finish printing the structure
print "} ${prefix}${name};\n\n"; $$output .= "} ${prefix}${name};\n\n";
} }
sub printHdrGuard sub printHdrGuard
{ {
my ($name) = @_; my ($output, $name) = @_;
$name =~ s/\./_/g; $name =~ s/\./_/g;
$name = uc($name); $name = uc($name);
print "__INCLUDED_DB_TEMPLATE_SCHEMA_STRUCTDEF_${name}_H__"; $$output .= "__INCLUDED_DB_TEMPLATE_SCHEMA_STRUCTDEF_${name}_H__";
} }
sub startFile() sub startFile()
{ {
my ($name) = @_; my ($output, $name) = @_;
print "/* This file is generated automatically, do not edit, change the source ($name) instead. */\n\n"; $$output .= "/* This file is generated automatically, do not edit, change the source ($name) instead. */\n\n";
print "#ifndef "; $$output .= "#ifndef ";
printHdrGuard($name); printHdrGuard($output, $name);
print "\n"; $$output .= "\n";
print "#define "; $$output .= "#define ";
printHdrGuard($name); printHdrGuard($output, $name);
print "\n\n"; $$output .= "\n\n";
} }
sub endFile() sub endFile()
{ {
my ($name) = @_; my ($output, $name) = @_;
print "#endif // "; $$output .= "#endif // ";
printHdrGuard($name); printHdrGuard($output, $name);
print "\n"; $$output .= "\n";
} }
1; 1;

View File

@ -134,18 +134,23 @@ while (<>)
else { die "Unmatched line: $_\n"; } else { die "Unmatched line: $_\n"; }
} }
CG::startFile($name); my $output = "";
CG::startFile(\$output, $name);
# Print all enums # Print all enums
foreach my $enum (@enumList) foreach my $enum (@enumList)
{ {
CG::printEnum($enum); CG::printEnum(\$output, $enum);
} }
# Print all structs # Print all structs
foreach my $struct (@structList) foreach my $struct (@structList)
{ {
CG::printStruct($struct, \%structMap, \%enumMap); CG::printStruct(\$output, $struct, \%structMap, \%enumMap);
} }
CG::endFile($name); CG::endFile(\$output, $name);
# Actually print out the output
print $output;

View File

@ -8,20 +8,20 @@ use strict;
sub printComments sub printComments
{ {
my ($comments, $indent) = @_; my ($output, $comments, $indent) = @_;
return unless @{$comments}; return unless @{$comments};
foreach my $comment (@{$comments}) foreach my $comment (@{$comments})
{ {
print "\t" if $indent; $$output .= "\t" if $indent;
print "--${comment}\n"; $$output .= "--${comment}\n";
} }
} }
sub printStructFields sub printStructFields
{ {
my ($struct, $enumMap) = @_; my ($output, $struct, $enumMap) = @_;
my @fields = @{${$struct}{"fields"}}; my @fields = @{${$struct}{"fields"}};
my $structName = ${$struct}{"name"}; my $structName = ${$struct}{"name"};
@ -29,7 +29,7 @@ sub printStructFields
{ {
my $field = shift(@fields); my $field = shift(@fields);
printComments ${$field}{"comment"}, 1; printComments($output, ${$field}{"comment"}, 1);
if (${$field}{"type"} and ${$field}{"type"} =~ /set/) if (${$field}{"type"} and ${$field}{"type"} =~ /set/)
{ {
@ -41,22 +41,22 @@ sub printStructFields
{ {
my $value = shift(@values); my $value = shift(@values);
print "\t`$structName`.`${$field}{\"name\"}_${$value}{\"name\"}`"; $$output .= "\t`$structName`.`${$field}{\"name\"}_${$value}{\"name\"}`";
print ",\n" if @values; $$output .= ",\n" if @values;
} }
} }
else else
{ {
print "\t`$structName`.`${$field}{\"name\"}`"; $$output .= "\t`$structName`.`${$field}{\"name\"}`";
} }
print ",\n\n" if @fields; $$output .= ",\n\n" if @fields;
} }
} }
sub printStructContent sub printStructContent
{ {
my ($struct, $structMap, $enumMap, $first) = @_; my ($output, $struct, $structMap, $enumMap, $first) = @_;
foreach (keys %{${$struct}{"qualifiers"}}) foreach (keys %{${$struct}{"qualifiers"}})
{ {
@ -65,18 +65,18 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"}; my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName}; my $inheritStruct = ${$structMap}{$inheritName};
printStructContent($inheritStruct, $structMap, $enumMap, 0); printStructContent($output, $inheritStruct, $structMap, $enumMap, 0);
} }
} }
printStructFields($struct, $enumMap); printStructFields($output, $struct, $enumMap);
print ",\n" unless $first; $$output .= ",\n" unless $first;
print "\n"; $$output .= "\n";
} }
sub printBaseStruct sub printBaseStruct
{ {
my ($struct, $structMap) = @_; my ($outstr, $struct, $structMap) = @_;
my $is_base = 1; my $is_base = 1;
foreach (keys %{${$struct}{"qualifiers"}}) foreach (keys %{${$struct}{"qualifiers"}})
@ -86,22 +86,22 @@ sub printBaseStruct
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"}; my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName}; my $inheritStruct = ${$structMap}{$inheritName};
printBaseStruct($inheritStruct, $structMap); printBaseStruct($outstr, $inheritStruct, $structMap);
$is_base = 0; $is_base = 0;
} }
elsif (/abstract/) elsif (/abstract/)
{ {
print "`${$struct}{\"name\"}`"; $$outstr .= "`${$struct}{\"name\"}`";
$is_base = 0; $is_base = 0;
} }
} }
print "`${$struct}{\"name\"}`" if $is_base; $$outstr .= "`${$struct}{\"name\"}`" if $is_base;
} }
sub printStructJoins sub printStructJoins
{ {
my ($struct, $structMap) = @_; my ($outstr, $struct, $structMap) = @_;
foreach (keys %{${$struct}{"qualifiers"}}) foreach (keys %{${$struct}{"qualifiers"}})
{ {
@ -110,8 +110,8 @@ sub printStructJoins
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"}; my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName}; my $inheritStruct = ${$structMap}{$inheritName};
printStructJoins($inheritStruct, $structMap); printStructJoins($outstr, $inheritStruct, $structMap);
print " INNER JOIN `${$struct}{\"name\"}` ON `${inheritName}`.`unique_inheritance_id` = `${$struct}{\"name\"}`.`unique_inheritance_id`"; $$outstr .= " INNER JOIN `${$struct}{\"name\"}` ON `${inheritName}`.`unique_inheritance_id` = `${$struct}{\"name\"}`.`unique_inheritance_id`";
} }
} }
} }
@ -122,27 +122,27 @@ sub printEnum()
sub printStruct() sub printStruct()
{ {
my ($struct, $structMap, $enumMap) = @_; my ($output, $struct, $structMap, $enumMap) = @_;
printComments(${$struct}{"comment"}, 0); printComments($output, ${$struct}{"comment"}, 0);
# Start printing the select statement # Start printing the select statement
print "SELECT\n"; $$output .= "SELECT\n";
printStructContent($struct, $structMap, $enumMap, 1); printStructContent($output, $struct, $structMap, $enumMap, 1);
print "FROM "; $$output .= "FROM ";
printBaseStruct($struct, $structMap); printBaseStruct($output, $struct, $structMap);
printStructJoins($struct, $structMap); printStructJoins($output, $struct, $structMap);
print ";\n\n"; $$output .= ";\n\n";
} }
sub startFile() sub startFile()
{ {
my ($name) = @_; my ($output, $name) = @_;
print "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n"; $$output .= "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
} }
sub endFile() sub endFile()

View File

@ -8,34 +8,34 @@ use strict;
sub printStructFieldType sub printStructFieldType
{ {
my ($field) = @_; my ($output, $field) = @_;
$_ = ${$field}{"type"}; $_ = ${$field}{"type"};
if (/count/) { print "INTEGER NOT NULL"; } if (/count/) { $$output .= "INTEGER NOT NULL"; }
elsif (/string/) { print "TEXT NOT NULL"; } elsif (/string/) { $$output .= "TEXT NOT NULL"; }
elsif (/real/) { print "NUMERIC NOT NULL"; } elsif (/real/) { $$output .= "NUMERIC NOT NULL"; }
elsif (/bool/) { print "INTEGER NOT NULL"; } elsif (/bool/) { $$output .= "INTEGER NOT NULL"; }
elsif (/enum/) { print "INTEGER NOT NULL"; } elsif (/enum/) { $$output .= "INTEGER NOT NULL"; }
else { die "UKNOWN TYPE: $_"; } else { die "UKNOWN TYPE: $_"; }
} }
sub printComments sub printComments
{ {
my ($comments, $indent) = @_; my ($output, $comments, $indent) = @_;
return unless @{$comments}; return unless @{$comments};
foreach my $comment (@{$comments}) foreach my $comment (@{$comments})
{ {
print "\t" if $indent; $$output .= "\t" if $indent;
print "--${comment}\n"; $$output .= "--${comment}\n";
} }
} }
sub printStructFields sub printStructFields
{ {
my ($struct, $enumMap) = @_; my ($output, $struct, $enumMap) = @_;
my @fields = @{${$struct}{"fields"}}; my @fields = @{${$struct}{"fields"}};
my @constraints = (); my @constraints = ();
@ -43,7 +43,7 @@ sub printStructFields
{ {
my $field = shift(@fields); my $field = shift(@fields);
printComments ${$field}{"comment"}, 1; printComments($output, ${$field}{"comment"}, 1);
if (${$field}{"type"} and ${$field}{"type"} =~ /set/) if (${$field}{"type"} and ${$field}{"type"} =~ /set/)
{ {
@ -56,9 +56,9 @@ sub printStructFields
{ {
my $value = shift(@values); my $value = shift(@values);
print "\t${$field}{\"name\"}_${$value}{\"name\"} INTEGER NOT NULL"; $$output .= "\t${$field}{\"name\"}_${$value}{\"name\"} INTEGER NOT NULL";
print "," if @values or @fields or @constraints or (${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/); $$output .= "," if @values or @fields or @constraints or (${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/);
print "\n"; $$output .= "\n";
$unique_string .= "${$field}{\"name\"}_${$value}{\"name\"}"; $unique_string .= "${$field}{\"name\"}_${$value}{\"name\"}";
$unique_string .= ", " if @values; $unique_string .= ", " if @values;
} }
@ -68,28 +68,29 @@ sub printStructFields
} }
else else
{ {
print "\t${$field}{\"name\"} "; $$output .= "\t${$field}{\"name\"} ";
printStructFieldType($field); printStructFieldType($output, $field);
print " UNIQUE" if ${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/; $$output .= " UNIQUE" if ${$field}{"qualifier"} and ${$field}{"qualifier"} =~ /unique/;
print ",\n" if @fields or @constraints; $$output .= ",\n" if @fields or @constraints;
} }
print "\n"; $$output .= "\n";
} }
while (@constraints) while (@constraints)
{ {
my $constraint = shift(@constraints); my $constraint = shift(@constraints);
print "\t${constraint}"; $$output .= "\t${constraint}";
print ",\n" if @constraints; $$output .= ",\n" if @constraints;
print "\n"; $$output .= "\n";
} }
} }
sub printStructContent sub printStructContent
{ {
my ($struct, $name, $structMap, $enumMap, $printFields) = @_; my ($output, $struct, $name, $structMap, $enumMap, $printFields) = @_;
foreach (keys %{${$struct}{"qualifiers"}}) foreach (keys %{${$struct}{"qualifiers"}})
{ {
@ -98,20 +99,20 @@ sub printStructContent
my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"}; my $inheritName = ${${$struct}{"qualifiers"}}{"inherit"};
my $inheritStruct = ${$structMap}{$inheritName}; my $inheritStruct = ${$structMap}{$inheritName};
printStructContent($inheritStruct, $name, $structMap, $enumMap, 0); printStructContent($output, $inheritStruct, $name, $structMap, $enumMap, 0);
} }
elsif (/abstract/) elsif (/abstract/)
{ {
print "\t-- Automatically generated ID to link the inheritance hierarchy.\n" $$output .= "\t-- Automatically generated ID to link the inheritance hierarchy.\n";
."\tunique_inheritance_id INTEGER PRIMARY KEY "; . "\tunique_inheritance_id INTEGER PRIMARY KEY ";
print "AUTOINCREMENT " if $printFields; $$output .= "AUTOINCREMENT " if $printFields;
print "NOT NULL,\n\n"; $$output .= "NOT NULL,\n\n";
} }
} }
$$name = ${$struct}{"name"}; $$name = ${$struct}{"name"};
printStructFields($struct, $enumMap) if $printFields; printStructFields($output, $struct, $enumMap) if $printFields;
} }
sub printEnum() sub printEnum()
@ -120,25 +121,25 @@ sub printEnum()
sub printStruct() sub printStruct()
{ {
my ($struct, $structMap, $enumMap) = @_; my ($output, $struct, $structMap, $enumMap) = @_;
my $name = ${$struct}{"name"}; my $name = ${$struct}{"name"};
printComments(${$struct}{"comment"}, 0); printComments($output, ${$struct}{"comment"}, 0);
# Start printing the structure # Start printing the structure
print "CREATE TABLE `${name}` (\n"; $$output .= "CREATE TABLE `${name}` (\n";
printStructContent($struct, \$name, $structMap, $enumMap, 1); printStructContent($output, $struct, \$name, $structMap, $enumMap, 1);
# Finish printing the structure # Finish printing the structure
print ");\n\n"; $$output .= ");\n\n";
} }
sub startFile() sub startFile()
{ {
my ($name) = @_; my ($output, $name) = @_;
print "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n"; $$output .= "-- This file is generated automatically, do not edit, change the source ($name) instead.\n\n";
} }
sub endFile() sub endFile()