I need to convert some C subroutine into Lazarus Pascal. So i developed this application that I think is the only one open source that do this. All that I found before this was just some string replacer, insted mine is a syntax interpreter.
Main goal was to help me converting C algorithms contained in R source code into Lazarus.
It supports FOR, WHILE, IF, pointers, and all main statements. Expressions are converted respecting priority between operators.
It doesnt't support CASE statement.
Here some example:
Original C code (taken from R source codes)
void kmeans_Lloyd(double *x, int *pn, int *pp, double *cen, int *pk, int *cl,
int *pmaxiter, int *nc, double *wss)
{
int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
int iter, i, j, c, it, inew = 0;
double best, dd, tmp;
Rboolean updated;
for(i = 0; i < n; i++) cl[i] = -1;
for(iter = 0; iter < maxiter; iter++) {
updated = FALSE;
for(i = 0; i < n; i++) {
/* find nearest centre for each point */
best = R_PosInf;
for(j = 0; j < k; j++) {
dd = 0.0;
for(c = 0; c < p; c++) {
tmp = x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
}
if(dd < best) {
best = dd;
inew = j+1;
}
}
if(cl[i] != inew) {
updated = TRUE;
cl[i] = inew;
}
}
if(!updated) break;
/* update each centre */
for(j = 0; j < k*p; j++) cen[j] = 0.0;
for(j = 0; j < k; j++) nc[j] = 0;
for(i = 0; i < n; i++) {
it = cl[i] - 1; nc[it]++;
for(c = 0; c < p; c++) cen[it+c*k] += x[i+c*n];
}
for(j = 0; j < k*p; j++) cen[j] /= nc[j % k];
}
*pmaxiter = iter + 1;
for(j = 0; j < k; j++) wss[j] = 0.0;
for(i = 0; i < n; i++) {
it = cl[i] - 1;
for(c = 0; c < p; c++) {
tmp = x[i+n*c] - cen[it+k*c];
wss[it] += tmp * tmp;
}
}
}
void kmeans_MacQueen(double *x, int *pn, int *pp, double *cen, int *pk,
int *cl, int *pmaxiter, int *nc, double *wss)
{
int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
int iter, i, j, c, it, inew = 0, iold;
double best, dd, tmp;
Rboolean updated;
/* first assign each point to the nearest cluster centre */
for(i = 0; i < n; i++) {
best = R_PosInf;
for(j = 0; j < k; j++) {
dd = 0.0;
for(c = 0; c < p; c++) {
tmp = x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
}
if(dd < best) {
best = dd;
inew = j+1;
}
}
if(cl[i] != inew) cl[i] = inew;
}
/* and recompute centres as centroids */
for(j = 0; j < k*p; j++) cen[j] = 0.0;
for(j = 0; j < k; j++) nc[j] = 0;
for(i = 0; i < n; i++) {
it = cl[i] - 1; nc[it]++;
for(c = 0; c < p; c++) cen[it+c*k] += x[i+c*n];
}
for(j = 0; j < k*p; j++) cen[j] /= nc[j % k];
for(iter = 0; iter < maxiter; iter++) {
updated = FALSE;
for(i = 0; i < n; i++) {
best = R_PosInf;
for(j = 0; j < k; j++) {
dd = 0.0;
for(c = 0; c < p; c++) {
tmp = x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
}
if(dd < best) {
best = dd;
inew = j;
}
}
if((iold = cl[i] - 1) != inew) {
updated = TRUE;
cl[i] = inew + 1;
nc[iold]--; nc[inew]++;
/* update old and new cluster centres */
for(c = 0; c < p; c++) {
cen[iold+k*c] += (cen[iold+k*c] - x[i+n*c])/nc[iold];
cen[inew+k*c] += (x[i+n*c] - cen[inew+k*c])/nc[inew];
}
}
}
if(!updated) break;
}
*pmaxiter = iter + 1;
for(j = 0; j < k; j++) wss[j] = 0.0;
for(i = 0; i < n; i++) {
it = cl[i] - 1;
for(c = 0; c < p; c++) {
tmp = x[i+n*c] - cen[it+k*c];
wss[it] += tmp * tmp;
}
}
}
//Converted code
procedure kmeans_Lloyd (
x:pDouble;
pn:pInteger;
pp:pInteger;
cen:pDouble;
pk:pInteger;
cl:pInteger;
pmaxiter:pInteger;
nc:pInteger;
wss:pDouble
);
var
n:integer;
k:integer;
p:integer;
maxiter:integer;
iter:integer;
i:integer;
j:integer;
c:integer;
it:integer;
inew:integer;
best:double;
dd:double;
tmp:double;
updated:Rboolean;
A0b0ca04e75:boolean;
A48ec43d6f10:boolean;
A0b0ca04e714:boolean;
A17719338e18:boolean;
A4f2166fb422:boolean;
Ae9c5e806450:boolean;
A17719338e55:boolean;
A0b0ca04e760:boolean;
A4f2166fb466:boolean;
Ae9c5e806472:boolean;
A17719338e80:boolean;
A0b0ca04e785:boolean;
A4f2166fb489:boolean;
begin
n := pn^;
k := pk^;
p := pp^;
maxiter := pmaxiter^;
inew := 0;
i := 0;
A0b0ca04e75 := false;
while ( true ) do
begin
if (A0b0ca04e75=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e75 := true;
cl[i] := -1;
end;
iter := 0;
A48ec43d6f10 := false;
while ( true ) do
begin
if (A48ec43d6f10=true ) then
begin
inc(iter);
end;
if not (iter<maxiter) then break;
A48ec43d6f10 := true;
updated := FALSE;
i := 0;
A0b0ca04e714 := false;
while ( true ) do
begin
if (A0b0ca04e714=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e714 := true;
best := R_PosInf;
j := 0;
A17719338e18 := false;
while ( true ) do
begin
if (A17719338e18=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e18 := true;
dd := 0.0;
c := 0;
A4f2166fb422 := false;
while ( true ) do
begin
if (A4f2166fb422=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb422 := true;
tmp := x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
end;
if ((dd<best)) then
begin
best := dd;
inew := j+1;
end;
end;
if (((cl[i])<>(inew))) then
begin
updated := TRUE;
cl[i] := inew;
end;
end;
if ((not(updated))) then
begin
break;
end;
j := 0;
Ae9c5e806450 := false;
while ( true ) do
begin
if (Ae9c5e806450=true ) then
begin
inc(j);
end;
if not (j<k*p) then break;
Ae9c5e806450 := true;
cen[j] := 0.0;
end;
j := 0;
A17719338e55 := false;
while ( true ) do
begin
if (A17719338e55=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e55 := true;
nc[j] := 0;
end;
i := 0;
A0b0ca04e760 := false;
while ( true ) do
begin
if (A0b0ca04e760=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e760 := true;
it := cl[i] - 1;
inc(nc[it]);
c := 0;
A4f2166fb466 := false;
while ( true ) do
begin
if (A4f2166fb466=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb466 := true;
cen[it+c*k] += x[i+c*n];
end;
end;
j := 0;
Ae9c5e806472 := false;
while ( true ) do
begin
if (Ae9c5e806472=true ) then
begin
inc(j);
end;
if not (j<k*p) then break;
Ae9c5e806472 := true;
cen[j] /= nc[j mod k];
end;
end;
pmaxiter^ := iter + 1;
j := 0;
A17719338e80 := false;
while ( true ) do
begin
if (A17719338e80=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e80 := true;
wss[j] := 0.0;
end;
i := 0;
A0b0ca04e785 := false;
while ( true ) do
begin
if (A0b0ca04e785=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e785 := true;
it := cl[i] - 1;
c := 0;
A4f2166fb489 := false;
while ( true ) do
begin
if (A4f2166fb489=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb489 := true;
tmp := x[i+n*c] - cen[it+k*c];
wss[it] += tmp * tmp;
end;
end;
end;
procedure kmeans_MacQueen (
x:pDouble;
pn:pInteger;
pp:pInteger;
cen:pDouble;
pk:pInteger;
cl:pInteger;
pmaxiter:pInteger;
nc:pInteger;
wss:pDouble
);
var
n:integer;
k:integer;
p:integer;
maxiter:integer;
iter:integer;
i:integer;
j:integer;
c:integer;
it:integer;
inew:integer;
iold:integer;
best:double;
dd:double;
tmp:double;
updated:Rboolean;
A0b0ca04e7108:boolean;
A17719338e112:boolean;
A4f2166fb4116:boolean;
Ae9c5e8064137:boolean;
A17719338e142:boolean;
A0b0ca04e7147:boolean;
A4f2166fb4153:boolean;
Ae9c5e8064159:boolean;
A48ec43d6f164:boolean;
A0b0ca04e7168:boolean;
A17719338e172:boolean;
A4f2166fb4176:boolean;
A4f2166fb4201:boolean;
A17719338e218:boolean;
A0b0ca04e7223:boolean;
A4f2166fb4227:boolean;
begin
n := pn^;
k := pk^;
p := pp^;
maxiter := pmaxiter^;
inew := 0;
i := 0;
A0b0ca04e7108 := false;
while ( true ) do
begin
if (A0b0ca04e7108=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e7108 := true;
best := R_PosInf;
j := 0;
A17719338e112 := false;
while ( true ) do
begin
if (A17719338e112=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e112 := true;
dd := 0.0;
c := 0;
A4f2166fb4116 := false;
while ( true ) do
begin
if (A4f2166fb4116=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb4116 := true;
tmp := x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
end;
if ((dd<best)) then
begin
best := dd;
inew := j+1;
end;
end;
if (((cl[i])<>(inew))) then
begin
cl[i] := inew;
end;
end;
j := 0;
Ae9c5e8064137 := false;
while ( true ) do
begin
if (Ae9c5e8064137=true ) then
begin
inc(j);
end;
if not (j<k*p) then break;
Ae9c5e8064137 := true;
cen[j] := 0.0;
end;
j := 0;
A17719338e142 := false;
while ( true ) do
begin
if (A17719338e142=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e142 := true;
nc[j] := 0;
end;
i := 0;
A0b0ca04e7147 := false;
while ( true ) do
begin
if (A0b0ca04e7147=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e7147 := true;
it := cl[i] - 1;
inc(nc[it]);
c := 0;
A4f2166fb4153 := false;
while ( true ) do
begin
if (A4f2166fb4153=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb4153 := true;
cen[it+c*k] += x[i+c*n];
end;
end;
j := 0;
Ae9c5e8064159 := false;
while ( true ) do
begin
if (Ae9c5e8064159=true ) then
begin
inc(j);
end;
if not (j<k*p) then break;
Ae9c5e8064159 := true;
cen[j] /= nc[j mod k];
end;
iter := 0;
A48ec43d6f164 := false;
while ( true ) do
begin
if (A48ec43d6f164=true ) then
begin
inc(iter);
end;
if not (iter<maxiter) then break;
A48ec43d6f164 := true;
updated := FALSE;
i := 0;
A0b0ca04e7168 := false;
while ( true ) do
begin
if (A0b0ca04e7168=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e7168 := true;
best := R_PosInf;
j := 0;
A17719338e172 := false;
while ( true ) do
begin
if (A17719338e172=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e172 := true;
dd := 0.0;
c := 0;
A4f2166fb4176 := false;
while ( true ) do
begin
if (A4f2166fb4176=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb4176 := true;
tmp := x[i+n*c] - cen[j+k*c];
dd += tmp * tmp;
end;
if ((dd<best)) then
begin
best := dd;
inew := j;
end;
end;
if ((((iold=cl[i]-1))<>(inew))) then
begin
updated := TRUE;
cl[i] := inew + 1;
dec(nc[iold]);
inc(nc[inew]);
c := 0;
A4f2166fb4201 := false;
while ( true ) do
begin
if (A4f2166fb4201=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb4201 := true;
cen[iold+k*c] += (cen[iold+k*c] - x[i+n*c])/nc[iold];
cen[inew+k*c] += (x[i+n*c] - cen[inew+k*c])/nc[inew];
end;
end;
end;
if ((not(updated))) then
begin
break;
end;
end;
pmaxiter^ := iter + 1;
j := 0;
A17719338e218 := false;
while ( true ) do
begin
if (A17719338e218=true ) then
begin
inc(j);
end;
if not (j<k) then break;
A17719338e218 := true;
wss[j] := 0.0;
end;
i := 0;
A0b0ca04e7223 := false;
while ( true ) do
begin
if (A0b0ca04e7223=true ) then
begin
inc(i);
end;
if not (i<n) then break;
A0b0ca04e7223 := true;
it := cl[i] - 1;
c := 0;
A4f2166fb4227 := false;
while ( true ) do
begin
if (A4f2166fb4227=true ) then
begin
inc(c);
end;
if not (c<p) then break;
A4f2166fb4227 := true;
tmp := x[i+n*c] - cen[it+k*c];
wss[it] += tmp * tmp;
end;
end;
end;
here there is the link to the project (on sourceforge):
http://ctopas.sourceforge.net/I think that this project opens a world of math routines developed in C to lazarus users.
I will not develope this project anymore because it just do what I need, but if some one is interested in I will be happy to answer to any question.
I would be delighted if you say me what do you think about this project
