Skip to content

Commit 597711f

Browse files
Embedding COBOL variable names into Java variable names (#39)
With -fembed-var-name option, the compiler replaces variable names f_{number} and b_{number} in Java source code with f_{COBOL variable name} and b_{COBOL variable name} respectively.
1 parent a38b097 commit 597711f

File tree

3 files changed

+126
-24
lines changed

3 files changed

+126
-24
lines changed

cobc/cobc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static const struct option long_options[] = {
314314
{"assign_external", no_argument, NULL, 'A'},
315315
{"reference_check", no_argument, NULL, 'K'},
316316
{"constant", optional_argument, NULL, '3'},
317+
{"embed-var-name", no_argument, &cb_flag_embed_var_name, 1},
317318
#undef CB_FLAG
318319
#define CB_FLAG(var,name,doc) \
319320
{"f"name, no_argument, &var, 1}, \

cobc/codegen.c

Lines changed: 122 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,60 @@ static void joutput_alphabet_name_initialization(struct cb_alphabet_name *p);
177177
const int L_initextern_addr = 2000000000;
178178
int param_wrap_string_flag = 0;
179179

180+
static char* get_java_identifier_field(struct cb_field* f);
181+
static char* get_java_identifier_base(struct cb_field* f);
182+
static void get_java_identifier_helper(struct cb_field* f, char* buf);
183+
static void strcpy_identifier_cobol_to_java(char* buf, char* identifier);
184+
//#define ENABLE_EMBED_ORIGINAL_VARIABLE_NAME 1
185+
186+
static char*
187+
get_java_identifier_field(struct cb_field* f) {
188+
char *buf = malloc(COB_SMALL_BUFF);
189+
if(cb_flag_embed_var_name) {
190+
strcpy(buf, CB_PREFIX_FIELD);
191+
get_java_identifier_helper(f, buf + strlen(CB_PREFIX_FIELD));
192+
} else {
193+
sprintf(buf, "%s%d", CB_PREFIX_FIELD, f->id);
194+
}
195+
return buf;
196+
}
197+
198+
static char*
199+
get_java_identifier_base(struct cb_field* f) {
200+
char *buf = malloc(COB_SMALL_BUFF);
201+
if(cb_flag_embed_var_name) {
202+
strcpy(buf, CB_PREFIX_BASE);
203+
get_java_identifier_helper(f, buf + strlen(CB_PREFIX_BASE));
204+
} else {
205+
sprintf(buf, "%s%d", CB_PREFIX_BASE, f->id);
206+
}
207+
return buf;
208+
}
209+
210+
static void
211+
get_java_identifier_helper(struct cb_field* f, char* buf) {
212+
static const char* of = "_of_";
213+
strcpy_identifier_cobol_to_java(buf, f->name);
214+
buf += strlen(f->name);
215+
if(f->parent != NULL) {
216+
strcpy(buf, of);
217+
buf += strlen(of);
218+
get_java_identifier_helper(f->parent, buf);
219+
}
220+
}
221+
222+
static void strcpy_identifier_cobol_to_java(char* buf, char* identifier) {
223+
for(; *identifier != '\0'; ++identifier, ++buf) {
224+
if(*identifier == '-') {
225+
*buf = '_';
226+
} else {
227+
*buf = *identifier;
228+
}
229+
}
230+
*buf = '\0';
231+
}
232+
233+
180234
static void
181235
lookup_call (const char *p)
182236
{
@@ -467,7 +521,8 @@ joutput_base (struct cb_field *f)
467521
struct cb_field *v;
468522
struct base_list *bl;
469523
char *nmp;
470-
char name[COB_MINI_BUFF];
524+
char name[COB_SMALL_BUFF];
525+
char *base_name;
471526

472527
f01 = cb_field_founder (f);
473528

@@ -480,6 +535,7 @@ joutput_base (struct cb_field *f)
480535
f01 = f01->redefines;
481536
}
482537

538+
//EDIT
483539
/* Base name */
484540
if (f01->flag_external) {
485541
strcpy (name, f01->name);
@@ -489,7 +545,9 @@ joutput_base (struct cb_field *f)
489545
}
490546
}
491547
} else {
492-
sprintf (name, "%d", f01->id);
548+
//sprintf (name, "%d", f01->id);
549+
base_name = get_java_identifier_base(f01);
550+
strcpy(name, base_name);
493551
}
494552

495553
if (!f01->flag_base) {
@@ -502,6 +560,8 @@ joutput_base (struct cb_field *f)
502560
base_cache = bl;
503561
} else {
504562
if (current_prog->flag_global_use) {
563+
joutput_local ("unsigned char\t\t*%s%s = NULL;",
564+
CB_PREFIX_BASE, name);
505565
joutput_local ("unsigned char\t\t*%s%s = NULL;",
506566
CB_PREFIX_BASE, name);
507567
joutput_local ("\t/* %s */\n", f01->name);
@@ -516,7 +576,13 @@ joutput_base (struct cb_field *f)
516576
}
517577
f01->flag_base = 1;
518578
}
519-
joutput ("%s%s", CB_PREFIX_BASE, name);
579+
580+
if(f01->flag_external) {
581+
joutput ("%s%s", CB_PREFIX_BASE, name);
582+
} else {
583+
joutput(name);
584+
free(base_name);
585+
}
520586

521587
if (cb_field_variable_address (f)) {
522588
for (p = f->parent; p; f = f->parent, p = f->parent) {
@@ -1307,22 +1373,26 @@ joutput_param (cb_tree x, int id)
13071373
joutput_target = savetarget;
13081374
}
13091375
if (f->flag_local) {
1376+
char* field_name = get_java_identifier_field(f);
13101377
if (f->flag_any_length && f->flag_anylen_done) {
1311-
joutput ("%s%d", CB_PREFIX_FIELD, f->id);
1378+
joutput(field_name);
13121379
} else {
13131380
joutput ("new GetAbstractCobolField() { ");
1314-
joutput ("public AbstractCobolField run() { %s%d.setDataStorage(", CB_PREFIX_FIELD, f->id);
1381+
joutput ("public AbstractCobolField run() { %s.setDataStorage(", field_name);
13151382
joutput_data (x);
1316-
joutput ("); return %s%d; }}.run()", CB_PREFIX_FIELD, f->id);
1383+
joutput ("); return %s; }}.run()", field_name);
13171384
if (f->flag_any_length) {
13181385
f->flag_anylen_done = 1;
13191386
}
13201387
}
1388+
free(field_name);
13211389
} else {
13221390
if (screenptr && f->storage == CB_STORAGE_SCREEN) {
13231391
joutput ("s_%d", f->id);
13241392
} else {
1325-
joutput ("%s%d", CB_PREFIX_FIELD, f->id);
1393+
char* field_name = get_java_identifier_field(f);
1394+
joutput(field_name);
1395+
free(field_name);
13261396
}
13271397
}
13281398
} else {
@@ -4111,8 +4181,13 @@ joutput_internal_function (struct cb_program *prog, cb_tree parameter_list)
41114181
if (!prog->flag_chained) {
41124182
int k;
41134183
for (k =0, l = parameter_list; l; l = CB_CHAIN (l), ++k) {
4114-
joutput_line("this.%s%d = %d < argStorages.length ? argStorages[%d] : null;",
4115-
CB_PREFIX_BASE, cb_field (CB_VALUE (l))->id, k, k);
4184+
//EDIT
4185+
char* base_name = get_java_identifier_base(cb_field (CB_VALUE (l)));
4186+
//joutput_line("this.%s%d = %d < argStorages.length ? argStorages[%d] : null;",
4187+
//CB_PREFIX_BASE, cb_field (CB_VALUE (l))->id, k, k);
4188+
joutput_line("this.%s = %d < argStorages.length ? argStorages[%d] : null;",
4189+
base_name, k, k);
4190+
free(base_name);
41164191
parmnum++;
41174192
}
41184193
}
@@ -4787,7 +4862,10 @@ joutput_internal_function (struct cb_program *prog, cb_tree parameter_list)
47874862
for (k = field_cache; k; k = k->next) {
47884863
if (k->f->flag_item_external) {
47894864
joutput_prefix ();
4790-
joutput ("\t%s%d.setDataStorage(", CB_PREFIX_FIELD, k->f->id);
4865+
//EDIT
4866+
char* field_name = get_java_identifier_field(k->f);
4867+
joutput ("\t%s.setDataStorage(", k->f);
4868+
free(field_name);
47914869
joutput_data (k->x);
47924870
joutput (");\n");
47934871
}
@@ -4936,21 +5014,28 @@ void joutput_init_method(struct cb_program *prog) {
49365014
joutput_line ("cob_unifunc = null;\n");
49375015
base_cache = list_cache_sort (base_cache, &base_cache_cmp);
49385016
prevprog = NULL;
5017+
//EDIT
49395018
for (blp = base_cache; blp; blp = blp->next) {
5019+
char* base_name = get_java_identifier_base(blp->f);
49405020
if (blp->curr_prog != prevprog) {
49415021
prevprog = blp->curr_prog;
49425022
joutput_prefix();
49435023
joutput ("/* PROGRAM-ID : %s */\n", prevprog);
49445024
joutput_prefix();
4945-
joutput ("%s%d = new CobolDataStorage(%d);",
4946-
CB_PREFIX_BASE, blp->f->id,
5025+
//joutput ("%s%d = new CobolDataStorage(%d);",
5026+
//CB_PREFIX_BASE, blp->f->id,
5027+
joutput ("%s = new CobolDataStorage(%d);",
5028+
base_name,
49475029
blp->f->memory_size);
49485030
} else {
49495031
joutput_prefix();
4950-
joutput ("%s%d = new CobolDataStorage(%d);",
4951-
CB_PREFIX_BASE, blp->f->id,
5032+
//joutput ("%s%d = new CobolDataStorage(%d);",
5033+
//CB_PREFIX_BASE, blp->f->id,
5034+
joutput ("%s = new CobolDataStorage(%d);",
5035+
base_name,
49525036
blp->f->memory_size);
49535037
}
5038+
free(base_name);
49545039
joutput ("\t/* %s */\n", blp->f->name);
49555040
}
49565041
joutput("\n");
@@ -4974,8 +5059,9 @@ void joutput_init_method(struct cb_program *prog) {
49745059
}
49755060

49765061
joutput_prefix();
4977-
joutput ("%s%d\t= ", CB_PREFIX_FIELD,
4978-
k->f->id);
5062+
char* field_name = get_java_identifier_field(k->f);
5063+
joutput ("%s\t= ", field_name);
5064+
free(field_name);
49795065
if (!k->f->flag_local && !k->f->flag_item_external) {
49805066
joutput_field (k->x);
49815067
} else {
@@ -5298,20 +5384,27 @@ void joutput_declare_member_variables(struct cb_program *prog, cb_tree parameter
52985384
base_cache = list_cache_sort (base_cache, &base_cache_cmp);
52995385
prevprog = NULL;
53005386
for (blp = base_cache; blp; blp = blp->next) {
5387+
//EDIT
5388+
char* base_name = get_java_identifier_base(blp->f);
53015389
if (blp->curr_prog != prevprog) {
53025390
prevprog = blp->curr_prog;
53035391
joutput_prefix();
53045392
joutput ("/* PROGRAM-ID : %s */\n", prevprog);
53055393
joutput_prefix();
5306-
joutput ("private CobolDataStorage %s%d;",
5307-
CB_PREFIX_BASE, blp->f->id,
5394+
//joutput ("private CobolDataStorage %s%d;",
5395+
//CB_PREFIX_BASE, blp->f->id,
5396+
joutput ("private CobolDataStorage %s;",
5397+
base_name,
53085398
blp->f->memory_size);
53095399
} else {
53105400
joutput_prefix();
5311-
joutput ("private CobolDataStorage %s%d;",
5312-
CB_PREFIX_BASE, blp->f->id,
5401+
//joutput ("private CobolDataStorage %s%d;",
5402+
//CB_PREFIX_BASE, blp->f->id,
5403+
joutput ("private CobolDataStorage %s;",
5404+
base_name,
53135405
blp->f->memory_size);
53145406
}
5407+
free(base_name);
53155408
joutput ("\t/* %s */\n", blp->f->name);
53165409
}
53175410
joutput("\n");
@@ -5320,8 +5413,12 @@ void joutput_declare_member_variables(struct cb_program *prog, cb_tree parameter
53205413

53215414
joutput_line("/* Call parameters */");
53225415
for (l = parameter_list; l; l = CB_CHAIN (l)) {
5323-
joutput_line("private CobolDataStorage %s%d;",
5324-
CB_PREFIX_BASE, cb_field (CB_VALUE (l))->id);
5416+
char* base_name = get_java_identifier_base(cb_field (CB_VALUE (l))->id);
5417+
//joutput_line("private CobolDataStorage %s%d;",
5418+
//CB_PREFIX_BASE, cb_field (CB_VALUE (l))->id);
5419+
joutput_line("private CobolDataStorage %s;",
5420+
base_name);
5421+
free(base_name);
53255422
}
53265423

53275424
/* External items */
@@ -5365,8 +5462,9 @@ void joutput_declare_member_variables(struct cb_program *prog, cb_tree parameter
53655462
prevprog);
53665463
}
53675464
joutput_prefix();
5368-
joutput ("private AbstractCobolField %s%d;", CB_PREFIX_FIELD,
5369-
k->f->id);
5465+
char* field_name = get_java_identifier_field(k->f);
5466+
joutput ("private AbstractCobolField %s;", field_name);
5467+
free(field_name);
53705468
joutput ("\t/* %s */\n", k->f->name);
53715469
}
53725470
joutput("\n");

cobc/flag.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ CB_FLAG (cb_flag_mfcomment, "mfcomment",
7070

7171
CB_FLAG (cb_flag_null_param, "null-param",
7272
N_("Pass extra NULL terminating pointers on CALL statements"))
73+
74+
CB_FLAG (cb_flag_embed_var_name, "embed-var-name",
75+
N_("Embed COBOL variable names into Java variable names. This option is experimental."))

0 commit comments

Comments
 (0)