Did some syntactical updates
[educational.data.git] / src / code_gen / test.pl
blob3abfbe364182f26f3080e3c88d636a39f9b0554c
1 #!/usr/bin/perl
3 use strict:
5 my @classes = ();
6 my $cClass = 0;
7 my $lastComments = ();
9 sub loadFile
11 ($file) = @_;
12 open (CLASSFILE, $file) || die "Could not open file $ARGV[0]";
14 @raw_data = <CLASSFILE>;
16 close (CLASSFILE);
18 foreach $line ( @raw_data ) {
19 chomp $line;
20 $line =~ /^([^ ]*) (.*)/;
21 $command = $1;
22 $line = $2;
23 parseCommand($command, $line);
25 pushCurrentClass();
28 sub parseCommand
30 my ($cmd, $line) = @_;
32 parseCreateClass ($line) if $cmd eq "CC";
34 push ( @{$cClass->{ PUBLIC_CTOR }}, parseCreateConstructor ($line) ) if $cmd eq "CTOR+";
35 push ( @{$cClass->{ PRIVATE_CTOR }}, parseCreateConstructor ($line) ) if $cmd eq "CTOR-";
36 push ( @{$cClass->{ PROTECTED_CTOR }}, parseCreateConstructor ($line) ) if $cmd eq "CTOR#";
38 push ( @{$cClass->{ PUBLIC_FUNCTIONS }}, parseCreateFunction ($line) ) if $cmd eq "F+";
39 push ( @{$cClass->{ PRIVATE_FUNCTIONS }}, parseCreateFunction ($line) ) if $cmd eq "F-";
40 push ( @{$cClass->{ PROTECTED_FUNCTIONS }}, parseCreateFunction ($line) ) if $cmd eq "F#";
42 push ( @{$cClass->{ PUBLIC_ATTRIBUTES }}, parseCreateAttribute ($line) ) if $cmd eq "A+";
43 push ( @{$cClass->{ PRIVATE_ATTRIBUTES }}, parseCreateAttribute ($line) ) if $cmd eq "A-";
44 push ( @{$cClass->{ PROTECTED_ATTRIBUTES }}, parseCreateAttribute ($line) ) if $cmd eq "A#";
48 sub parseCreateConstructor
50 ($line) = @_;
51 $line =~/\((.*)\)$/;
52 my $attrib_str = $1;
54 $attrib_str =~ s/ //g;
55 @attribs = split(/,/,$attrib_str);
56 $attr = ();
57 foreach my $attrib (@attribs) {
58 $attrib =~ /^(.*):(.*)$/;
59 $attr_name = $1;
60 $attr_type = $2;
61 $attribHash = {
62 NAME => $attr_name,
63 TYPE => $attr_type
65 push @{$attr}, $attribHash;
68 my $function = {
69 ATTRIBS => $attr,
70 COMMENTS => $lastComments
72 $lastComments = ();
73 return $function;
76 sub parseCreateFunction
78 ($line) = @_;
79 $line =~ s/ //g;
80 $line =~ /^([\w]*)[\W]*\((.*)\)[\W]*:[\W]*(.*)$/;
81 my $name = $1;
82 my $attrib_str = $2;
83 my $retval = $3;
85 $attrib_str =~ s/ //g;
86 @attribs = split(/,/,$attrib_str);
87 $attr = ();
88 foreach my $attrib (@attribs) {
89 $attrib =~ /^(.*):(.*)$/;
90 $attr_name = $1;
91 $attr_type = $2;
92 $attribHash = {
93 NAME => $attr_name,
94 TYPE => $attr_type
96 push @{$attr}, $attribHash;
99 my $function = {
100 FUNCTION_NAME => $name,
101 RETVAL => $retval,
102 ATTRIBS => $attr,
103 VALIDATORS => parseValidators($line),
104 COMMENTS => $lastComments
106 $lastComments = ();
107 return $function;
112 sub parseComment
114 ($line) = @_;
115 push @{$lastComments},$line;
118 sub parseValidators
120 ($line) = @_;
121 my @content = ();
122 if($line =~ /\{(.*)\}/ ) {
123 my $val = $1;
124 print $line . " => " . $val . "\n";
125 @content = split(/,/,$val);
127 return \@content;
130 sub parseCreateAttribute
132 ($line) = @_;
133 $line =~ s/ //g;
135 $line =~ /^([\w]*):([\w]*)/;
136 $attr_name = $1;
137 $attr_type = $2;
138 $attribHash = {
139 NAME => $attr_name,
140 TYPE => $attr_type,
141 VALIDATORS => parseValidators($line),
142 COMMENTS => $lastComments
144 $lastComments = ();
145 return $attribHash;
148 sub parseCreateClass
150 ($line) = @_;
151 pushCurrentClass();
152 $cClass = {
153 CLASSNAME => '',
155 PUBLIC_CTOR => [ ],
156 PRIVATE_CTOR => [ ],
157 PROTECTED_CTOR => [ ],
159 PUBLIC_FUNCTIONS => [ ],
160 PRIVATE_FUNCTIONS => [ ],
161 PROTECTED_FUNCTIONS => [ ],
163 PUBLIC_ATTRIBUTES => [ ],
164 PRIVATE_ATTRIBUTES => [ ],
165 PROTECTED_ATTRIBUTES => [ ]
168 $cClass->{ 'CLASSNAME' } = $line;
171 sub pushCurrentClass
173 if ($cClass != 0)
175 push @classes,$cClass;
177 else {
181 sub dumpValidators
183 ($val) = @_;
184 if ( @{$val} != 0 )
186 my $s = "<<@{$val}>>";
187 $s =~ s/ /:/g;
188 return " " .$s
190 return "";
193 sub dumpFunctions
195 @funcs = @_;
197 foreach my $func (@funcs)
199 $attrib_str = "";
200 my $setComma = 0;
201 foreach my $attrib (@{$func->{ ATTRIBS }})
203 $attrib_str .="," if ($setComma == 1);
205 $attrib_str .= " " . $attrib->{NAME} . ":" .
206 $attrib->{TYPE};
207 $setComma = 1;
209 print " " . $func->{ FUNCTION_NAME } . " (";
210 print $attrib_str;
211 print " ) : " . $func->{ RETVAL } . dumpValidators($func->{VALIDATORS}) . "\n";
215 sub dumpAttribs
217 @attribs = @_;
219 foreach my $attr (@attribs)
221 print " " . $attr->{ NAME } . " : " . $attr->{ TYPE } . dumpValidators($attr->{VALIDATORS}) ."\n";
225 sub printList
227 foreach my $class (@classes)
229 my $className = $class->{ CLASSNAME };
230 print "Class description for $className:\n";
231 if(@{$cClass->{ PUBLIC_FUNCTIONS }} != 0)
233 print " Public functions:\n";
234 dumpFunctions @{$cClass->{ PUBLIC_FUNCTIONS }};
236 if(@{$cClass->{ PROTECTED_FUNCTIONS }} != 0)
238 print " Protected functions:\n";
239 dumpFunctions @{$cClass->{ PROTECTED_FUNCTIONS }};
242 if(@{$cClass->{ PRIVATE_FUNCTIONS }} != 0)
244 print " Private functions:\n";
245 dumpFunctions @{$cClass->{ PRIVATE_FUNCTIONS }};
248 if ( @{$cClass->{ PUBLIC_ATTRIBUTES }} != 0 )
250 print " Public attributes:\n";
251 dumpAttribs @{$cClass->{ PUBLIC_ATTRIBUTES }};
253 if ( @{$cClass->{ PRIVATE_ATTRIBUTES }} != 0 )
255 print " Private attributes:\n";
256 dumpAttribs @{$cClass->{ PRIVATE_ATTRIBUTES }};
262 loadFile $ARGV[0];
263 printList