forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preproc.pl
executable file
·186 lines (156 loc) · 4.33 KB
/
preproc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/perl -w
$basedir = 'src/processing/core';
@contents = ();
# next slurp methods from PGraphics
open(F, "$basedir/PGraphics.java") || die $!;
foreach $line (<F>) {
push @contents, $line;
}
close(F);
# PGraphics subclasses PImage.. now get those methods
open(F, "$basedir/PImage.java") || die $!;
foreach $line (<F>) {
push @contents, $line;
}
close(F);
#open(DEBUG, ">debug.java") || die $!;
#print DEBUG @contents;
#close(DEBUG);
#exit;
open(APPLET, "$basedir/PApplet.java") || die $!;
@applet = <APPLET>;
close(APPLET);
$insert = 'public functions for processing.core';
# an improved version of this would only rewrite if changes made
open(OUT, ">$basedir/PApplet.new") || die $!;
foreach $line (@applet) {
print OUT $line;
last if ($line =~ /$insert/);
}
$comments = 0;
while ($line = shift(@contents)) {
$decl = "";
if ($line =~ /\/\*/) {
$comments++;
#print "+[$comments] $line";
}
if ($line =~ /\*\//) {
$comments--;
#print "-[$comments] $line";
}
next if ($comments > 0);
$got_something = 0; # so it's ugly, back off
$got_static = 0;
$got_interface = 0;
if ($line =~ /^\s*public ([\w\[\]]+) [a-zA-z_]+\(.*$/) {
$got_something = 1;
$got_interface = 1;
} elsif ($line =~ /^\s*abstract public ([\w\[\]]+) [a-zA-z_]+\(.*$/) {
$got_something = 1;
} elsif ($line =~ /^\s*public final ([\w\[\]]+) [a-zA-z_]+\(.*$/) {
$got_something = 1;
} elsif ($line =~ /^\s*static public ([\w\[\]]+) [a-zA-z_]+\(.*$/) {
$got_something = 1;
$got_static = 1;
}
# if function is marked "// ignore" then, uh, ignore it.
if (($got_something == 1) && ($line =~ /\/\/ ignore/)) {
$got_something = 0;
}
#if ($line =~ /^\s*public (\w+) [a-zA-z_]+\(.*$/) {
if ($got_something == 1) {
if ($1 ne 'void') {
$returns = 'return ';
} else {
$returns = '';
}
# if ($line =~ /^(\s+)abstract\s+([^;]+);/) {
# $line = $1 . $2 . " {\n";
# #print "found $1\n";
# # hrm
# }
# remove the 'abstract' modifier
$line =~ s/\sabstract\s/ /;
# replace semicolons with a start def
$line =~ s/\;\s*$/ {\n/;
print OUT "\n\n$line";
# if ($got_interface == 1) {
# $iline = $line;
# $iline =~ s/ \{/\;/;
## print INTF "\n$iline";
# }
$decl .= $line;
while (!($line =~ /\)/)) {
$line = shift (@contents);
$decl .= $line;
$line =~ s/\;\s*$/ {\n/;
print OUT $line;
# if ($got_interface == 1) {
# $iline = $line;
# $iline =~ s/ \{/\;/;
## print INTF $iline;
# }
}
#$g_line = '';
#$r_line = '';
$decl =~ /\s(\S+)\(/;
$decl_name = $1;
if ($got_static == 1) {
#print OUT " ${returns}PGraphics.${decl_name}(";
$g_line = " ${returns}PGraphics.${decl_name}(";
} else {
#if ($returns eq '') {
#print OUT " if (recorder != null) recorder.${decl_name}(";
$r_line = " if (recorder != null) recorder.${decl_name}(";
#}
#print OUT " ${returns}g.${decl_name}(";
$g_line = " ${returns}g.${decl_name}(";
}
$decl =~ s/\s+/ /g; # smush onto a single line
$decl =~ s/^.*\(//;
$decl =~ s/\).*$//;
$prev = 0;
@parts = split(', ', $decl);
foreach $part (@parts) {
#($the_type, $the_arg) = split(' ', $part);
@blargh = split(' ', $part);
$the_arg = $blargh[1];
$the_arg =~ s/[\[\]]//g;
if ($prev != 0) {
#print OUT ", ";
$g_line .= ", ";
$r_line .= ", ";
}
#print OUT "${the_arg}";
$g_line .= "${the_arg}";
$r_line .= "${the_arg}";
$prev = 1;
}
#print OUT ");\n";
$g_line .= ");\n";
$r_line .= ");\n";
if (($got_static != 1) && ($returns eq '')) {
print OUT $r_line;
}
print OUT $g_line;
print OUT " }\n";
}
}
print OUT "}\n";
#print INTF "}\n";
close(OUT);
#close(INTF);
$oldguy = join(' ', @applet);
open(NEWGUY, "$basedir/PApplet.new") || die $!;
@newbie = <NEWGUY>;
$newguy = join(' ', @newbie);
close(NEWGUY);
if ($oldguy ne $newguy) {
# replace him
print "updating PApplet with PGraphics api changes\n";
`mv $basedir/PApplet.new $basedir/PApplet.java`;
} else {
# just kill the new guy
#print "no changes to applet\n";
`rm -f $basedir/PApplet.new`;
}