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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
|
#include "bool.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#define max_refinements 10000
#define max_src_lines 10000
char *src_stat [max_src_lines];
bool *src_mode [max_src_lines];
bool is_ref_line [max_src_lines];
char *ref_name [max_refinements];
int ref_start [max_refinements];
int ref_end [max_refinements];
int ref_start_line [max_refinements];
int ref_end_line [max_refinements];
int ref_scope_start [max_refinements];
int ref_scope_end [max_refinements];
int last_obj_c;
int line_cor [max_src_lines][2];
int obj_line_no;
int open_bracket_line;
int open_string_line;
int open_char_line;
int open_sl_comment_line;
int open_comment_line;
int num_refs;
int num_src_lines;
int ref_search_base;
FILE *obj;
FILE *src;
void f_open (FILE *&f, char name [], char mode [])
{f = fopen (name, mode);
check_error;
. check_error
{if (mode [0] == 'r' && f == NULL)
{printf ("1, FILE, file '%s' not found\n", name);
fflush (stdout);
exit (1);
};
}.
}
char *substring (char s [], int from)
{static bool is_init = false;
static char *result;
perhaps_delete_old_result;
calc_new_result;
return result;
. perhaps_delete_old_result
{if (is_init)
delete (result);
}.
. calc_new_result
{result = new char [strlen (s) - from + 2];
is_init = true;
strcpy (result, &s [from]);
}.
}
char *substring (char s [], int from, int to)
{static bool is_init = false;
static char *result;
perhapss_delete_old_result;
calcc_new_result;
return result;
. perhapss_delete_old_result
{if (is_init)
delete (result);
}.
. calcc_new_result
{int i;
result = new char [to - from + 2];
is_init = true;
for (i = from; i < to; i++)
result [i-from] = s [i];
result [i-from] = 0;
}.
}
void changeall (char s [],
int max_length_of_s,
char tmplate [],
char replacement [])
{char *pos;
while ((pos = strstr (s, tmplate)) != NULL)
perform_substitute;
. perform_substitute
{delete_tmplate;
insert_replacement;
}.
. delete_tmplate
{char *pp = pos;
while (*(pp + strlen (tmplate)) != 0)
{*(pp) = *(pp + strlen (tmplate));
pp++;
};
*pp = 0;
}.
. insert_replacement
{if (strlen (s) + strlen (replacement) < max_length_of_s)
perform_insert;
}.
. perform_insert
{char buffer [max_length_of_s];
char *pp = s;
int i = 0;
while (pp != pos)
buffer [i++] = *(pp++);
buffer [i] = 0;
strcat (buffer, replacement);
i = strlen (buffer);
while (*pp != 0)
buffer [i++] = *(pp++);
buffer [i] = 0;
strcpy (s, buffer);
}.
}
char *complete (char name [], char tail [])
{static char r [256];
strcpy (r, name);
if (wrong_tail)
strcat (r, tail);
return r;
. wrong_tail
(strcmp (name_tail, tail) != 0).
. name_tail
(substring (name, strlen (name) - strlen (tail))).
}
void dump_refs ()
{for (int r = 0; r < num_refs; r++)
dump_ref;
. dump_ref
{printf ("%40s %5d %5d : %5d %5d <%5d %5d>\n",
ref_name [r],
ref_start_line [r],
ref_end_line [r],
ref_start [r],
ref_end [r],
ref_scope_start [r],
ref_scope_end [r]);
}.
}
void dump_src ()
{for (int i = 0; i < num_src_lines; i++)
{printf ("%d : %s", is_ref_line [i], src_stat [i]);
printf (" ");
for (int j = 0; j < strlen (src_stat [i]); j++)
if (src_mode [i][j])
printf ("-");
else printf (" ");
printf ("\n");
};
}
void dump_line_cor ()
{for (int i = 0; i < obj_line_no; i++)
{printf ("%5d %5d\n", line_cor [i][0], line_cor [i][1]);
};
}
void pass_1_error (int line_no, char msg [], int l)
{char err_msg [256];
char ii [128];
sprintf (ii, "%d", l);
strcpy (err_msg, msg);
changeall (err_msg, 255, "$", ii);
fclose (src);
printf ("error : %d : %s\n", line_no, err_msg);
exit (1);
}
void pass_1_error (int line_no, char msg [])
{fclose (src);
printf ("error : %d : %s\n", line_no, msg);
exit (1);
}
bool pass_1 (char src_name [])
{bool inside_comment;
bool inside_prc;
int symbol_no;
int line_no;
int last_none_sep_sym;
int last_sym;
int last_last_sym;
int last_ref_sym_line;
int last_ref_sym_no;
int sym;
int nested_brackets;
bool inside_refinement;
bool inside_string;
bool inside_char;
bool inside_sl_comment;
char line_buffer [1024];
bool line_mode [1024];
int line_length;
int proc_base;
int ref_base;
open_src_file;
init_symbol_table;
scan_file;
fclose (src);
. open_src_file
{f_open (src, complete (src_name, ".hc"), "r");
}.
. init_symbol_table
{num_refs = 0;
inside_prc = false;
symbol_no = 0;
line_no = 0;
inside_comment = false;
last_none_sep_sym = 0;
sym = 0;
nested_brackets = 0;
inside_refinement = false;
line_buffer [0] = 0;
line_length = 0;
inside_string = false;
inside_char = false;
inside_sl_comment = false;
}.
. scan_file
{get_sym;
while (another_sym)
{handle_sym;
};
new_line;
check_everything_closed;
}.
. check_everything_closed
{if (inside_string)
pass_1_error (line_no,
"EOF ($) inside string, depp",
open_string_line);
if (inside_char)
pass_1_error (line_no,
"EOF ($) inside char constant, hahaha",
open_char_line);
if (inside_sl_comment)
pass_1_error (line_no,
"EOF ($) inside comment, forget it",
open_sl_comment_line);
if (inside_comment)
pass_1_error (line_no,
"EOF ($) inside comment, oh boy",
open_comment_line);
if (inside_prc)
pass_1_error (line_no,
"'}' ($) missing, start to search it, haha",
open_bracket_line);
}.
. another_sym
(sym != EOF).
. get_sym
{if (sym != ' ' && sym != '\n')
handle_last_non_sep_symbol;
last_last_sym = last_sym;
last_sym = sym;
sym = fgetc (src);
line_mode [line_length] = inside_string ||
inside_char ||
inside_sl_comment ||
inside_comment;
if (another_sym)
line_buffer [line_length++] = sym;
line_buffer [line_length] = 0;
symbol_no++;
if (sym == '\n')
new_line;
handle_brackets;
}.
. handle_last_non_sep_symbol
{last_none_sep_sym = sym;
if (sym != '.')
{last_ref_sym_line = line_no;
last_ref_sym_no = line_length;
};
}.
. new_line
{src_stat [line_no] = new char [line_length + 1];
src_mode [line_no] = new bool [line_length + 1];
strcpy (src_stat [line_no], line_buffer);
memcpy (src_mode [line_no], line_mode, line_length * sizeof (bool));
line_buffer [0] = 0;
line_length = 0;
is_ref_line [line_no] = inside_refinement;
line_no++;
num_src_lines = line_no;
/* if (num_src_lines > max_src_lines)
printf ("too many src_lines\n");
*/
}.
. handle_brackets
{if (! inside_sl_comment &&
! inside_comment &&
! inside_string &&
! inside_char)
count_brackets;
}.
. count_brackets
{if (sym == '{') nested_brackets++;
if (sym == '}') nested_brackets--;
}.
. handle_sym
{if (inside_comment) skip_to_end_of_comment
else if (is_comment) start_new_comment
else if (inside_sl_comment) skip_to_end_of_sl_comment
else if (is_sl_comment) start_new_sl_comment
else if (inside_string) skip_to_end_of_string
else if (is_string) start_new_string
else if (inside_char) skip_to_end_of_char
else if (is_char) start_new_char
else if (! inside_prc) skip_to_prc_start
else if (is_refinement_start) handle_refinement_start
else if (is_refinement_end) handle_refinement_end
else if (is_prc_end) handle_prc_end
else get_sym;
}.
. is_refinement_start
(! inside_refinement && sym == '.' && last_sym == '\n').
. is_refinement_end
(sym == '\n' && last_none_sep_sym == '.' && inside_refinement).
. is_comment
(sym == '*' && last_sym == '/').
. is_sl_comment
(! inside_string && ! inside_char && sym == '/' && last_sym == '/' ).
. is_string
(! inside_string && sym == '\x22').
. is_char
(! inside_char && sym == '\'').
. is_prc_end
(inside_prc && nested_brackets == 0).
. skip_to_end_of_sl_comment
{if (sym == '\n')
inside_sl_comment = false;
get_sym;
}.
. start_new_sl_comment
{inside_sl_comment = true;
open_sl_comment_line = line_no;
line_mode [line_length-1] = true;
line_mode [line_length-2] = true;
get_sym;
}.
. skip_to_end_of_comment
{if (sym == '/' && last_sym == '*')
{inside_comment = false;
line_mode [line_length] = true;
};
get_sym;
}.
. start_new_comment
{inside_comment = true;
open_comment_line = line_no;
line_mode [line_length-1] = true;
get_sym;
}.
. skip_to_end_of_string
{if (sym == '\x22' && (last_sym != '\\' || last_last_sym == '\\'))
{inside_string = false;
line_mode [line_length] = true;
};
get_sym;
}.
. start_new_string
{inside_string = true;
open_string_line = line_no;
get_sym;
}.
. skip_to_end_of_char
{if (sym == '\'' && (last_sym != '\\' || last_last_sym == '\\'))
inside_char = false;
get_sym;
}.
. start_new_char
{inside_char = true;
open_char_line = line_no;
line_mode [line_length-1] = true;
get_sym;
}.
. skip_to_prc_start
{if (sym == '{' && last_none_sep_sym == ')')
start_new_prc;
get_sym;
}.
. start_new_prc
{ref_base = num_refs;
proc_base = line_no;
inside_prc = true;
open_bracket_line = line_no;
}.
. handle_prc_end
{inside_prc = false;
set_ref_scope_end;
if (inside_refinement)
pass_1_error (line_no, "unexpected end of function");
}.
. set_ref_scope_end
{for (int i = ref_base; i < num_refs; i++)
ref_scope_end [i] = line_no;
}.
. handle_refinement_start
{char name [128];
if (inside_refinement)
pass_1_error (line_no, "nested refinement");
get_name;
open_refinement;
}.
. get_name
{int l = 0;
inside_refinement = true;
name [0] = 0;
get_sym;
skip_blanks;
while (another_sym && sym != '\n' && sym != ' ' && sym != '\t')
{if (sym != ' ')
{name [l++] = sym; name [l] = 0;
};
get_sym;
};
}.
. open_refinement
{ref_name [num_refs] = new char [strlen (name) + 1];
ref_start [num_refs] = line_length;
ref_start_line [num_refs] = line_no;
ref_scope_start [num_refs] = proc_base;
strcpy (ref_name [num_refs], name);
}.
. skip_blanks
{while (sym == ' ' && another_sym)
{get_sym;
};
}.
. handle_refinement_end
{ref_end [num_refs] = last_ref_sym_no;
ref_end_line [num_refs] = last_ref_sym_line;
inside_refinement = false;
num_refs++;
/* if (num_refs > max_refinements)
printf ("too many refs\n");
*/
get_sym;
}.
}
void write_obj (int line_no, char c)
{fprintf (obj, "%c", c);
if (last_obj_c == '\n')
handle_new_line;
last_obj_c = c;
. handle_new_line
{line_cor [obj_line_no][0] = obj_line_no;
line_cor [obj_line_no][1] = line_no;
obj_line_no ++;
}.
}
void write_obj (int line_no, char st [])
{int l = strlen (st);
for (int i = 0; i < l; i++)
write_obj (line_no, st [i]);
}
int ref_no (int line_no, char sym [])
{int ref_n;
search_sym;
if (ref_n >= 0)
set_search_base;
return ref_n;
. search_sym
{for (ref_n = ref_search_base; ref_n < num_refs; ref_n++)
check_refinment;
if (ref_n >= num_refs)
ref_n = -1;
}.
. check_refinment
{if (strcmp (ref_name [ref_n], sym) == 0 &&
ref_scope_start [ref_n] <= line_no &&
ref_scope_end [ref_n] >= line_no)
break;
}.
. set_search_base
{while (ref_search_base > 0 &&
ref_scope_start [ref_search_base] ==
ref_scope_start [ref_search_base-1])
{ref_search_base--;
};
}.
}
void sub_ref (int ref_n)
{write_name;
for (int l = ref_start_line [ref_n]; l <= ref_end_line [ref_n]; l++)
gen_ref_line;
. write_name
{fprintf (obj, " /* %s */ ", ref_name [ref_n]);
}.
. gen_ref_line
{int min;
int max;
calc_min;
calc_max;
for (int c = min; c < max; c++)
handle_char;
}.
. handle_char
{if (within_identi && ! src_mode [l][c])
handle_identi
else to_obj;
}.
. within_identi
(isalnum (ss) || ss == '_').
. to_obj
{write_obj (l, ss);
}.
. handle_identi
{char ident [1024];
int ref;
get_identi;
ref = ref_no (l, ident);
if (ref != -1)
sub_ref (ref);
else write_obj (l, ident);
}.
. get_identi
{int h = 0;
while (c < max && within_identi)
{ident [h++] = ss;
c++;
};
c--;
ident [h] = 0;
}.
. ss
src_stat [l][c].
. calc_min
{if (first_line)
min = ref_start [ref_n];
else min = 0;
}.
. calc_max
{if (last_line)
max = ref_end [ref_n];
else max = strlen (src_stat [l]);
}.
. first_line
(l == ref_start_line [ref_n]).
. last_line
(l == ref_end_line [ref_n]).
}
bool pass_2 (char obj_name [])
{int line_no;
int char_no;
bool within_string;
bool within_comment;
bool within_char;
open_obj_file;
init_symbol_tab;
scan_lines;
fclose (obj);
. open_obj_file
{f_open (obj, complete (obj_name, ".cc"), "w");
}.
. init_symbol_tab
{line_no = 0;
ref_search_base = 0;
obj_line_no = 1;
line_cor [0][0] = 0;
line_cor [0][1] = 0;
within_string = false;
within_comment = false;
within_char = false;
}.
. scan_lines
{while (line_no < num_src_lines)
handle_line;
}.
. handle_line
{if (is_ref_line [line_no])
line_no++;
else handle_main_line;
}.
. handle_main_line
{int ll = strlen (src_stat [line_no]);
char_no = 0;
while (within_line)
{handle_symbol;
};
line_no++;
}.
. within_line
(char_no < ll).
. handle_symbol
{if (within_ident && ! src_mode [line_no][char_no])
handle_ident
else char_to_obj;
}.
. within_ident
(isalnum (s) || s == '_').
. char_to_obj
{write_obj (line_no, s);
char_no++;
}.
. handle_ident
{char ident [1024];
int ref;
get_ident;
ref = ref_no (line_no, ident);
if (ref != -1)
sub_ref (ref);
else write_obj (line_no, ident);
}.
. get_ident
{int l = 0;
while (within_line && within_ident)
{ident [l++] = s;
char_no++;
};
ident [l] = 0;
}.
. s
src_stat [line_no][char_no].
}
void pass_1_and_a_half ()
{for (int r = num_refs-1; r >= 0; r--)
check_ref;
. check_ref
{ref_search_base = 0;
if (dr != r)
handle_double;
}.
. handle_double
{printf ("Refinement : '%s' (in lines %d, %d) multiply declared\n",
ref_name [r],
ref_start_line [r],
ref_start_line [dr]);
exit (1);
}.
. dr
ref_no (ref_start_line [r], ref_name [r]).
}
int gcc_call(char gcc_exec [],
char src_name [],
char options [])
{char cmd [8000];
sprintf (cmd, "%s %s.cc %s 1> %s.err 2>&1",
gcc_exec,
src_name,
options,
src_name);
return system (cmd);
}
int ref_line (int i)
{for (int j = 0; j < obj_line_no; j++)
if (line_cor [j][0] >= i)
return line_cor [j][1];
return line_cor [obj_line_no-1][1];
}
void pass_3 (int gcc_return_code, char src_name [])
{FILE *err_file;
open_err_file;
if (err_file != NULL)
scan_errors;
fclose (err_file);
. open_err_file
{err_file = fopen (complete (src_name, ".err"), "r");
}.
. scan_errors
{char buffer [1024];
int num_errors = 0;
while (another_error_msg)
{handle_error_msg;
};
if (num_errors > 0)
remove_o;
}.
. remove_o
{char msg [80];
sprintf (msg, "rm -f %s.o", src_name);
system (msg);
exit (1);
}.
. another_error_msg
(fgets (buffer, 1020, err_file) != NULL).
. handle_error_msg
{int p;
int p0;
int c = 0;
num_errors++;
get_p;
if (p < strlen (buffer))
handle_second_colon
else printf ("%s", buffer);
}.
. handle_second_colon
{char num [128];
p0 = p;
if (strcmp (substring (buffer, p-3, p), ".cc") == 0)
handle_cc_error_msg
else printf ("%s", buffer);
}.
. handle_cc_error_msg
{get_num;
if (strlen (num) > 0)
handle_full_error
else printf ("%s.hc:%s",
substring (buffer, 0, p0-3),
substring (buffer, p0+1));
}.
. handle_full_error
{printf ("%s.hc:%d%s\n",
substring (buffer, 0, p0-3),
ref_line (atoi (num)),
substring (buffer, p));
}.
. get_p
{while (c < strlen (buffer) && buffer [c] != ':')
{c++;
};
p = c;
c++;
}.
. get_num
{int l = 0;
while (c < strlen (buffer) && isdigit (buffer [c]))
{num [l++] = buffer [c];
c++;
};
num [l] = 0;
p = c;
c++;
}.
}
main (int num_params, char *shell_params [])
{char gcc_exec [256];
char src_name [256];
char options [256];
int gcc_return_code;
handle_params;
pass_1 (src_name);
pass_1_and_a_half ();
pass_2 (src_name);
gcc_return_code = gcc_call (gcc_exec, src_name, options);
pass_3 (gcc_return_code, src_name);
return 0;
. handle_params
{if (num_params < 2)
usage
else analyse_params;
}.
. usage
{printf ("usage : hcc -complier <gcc_executbale> [options] <src>\n");
exit (1);
}.
. analyse_params
{bool any_gcc_executable = false;
strcpy (gcc_exec, "");
strcpy (src_name, "");
strcpy (options, "");
pharse_params;
if (! any_gcc_executable)
fuck;
}.
. fuck
{printf ("Idiot, tell me what compiler to use !!!\n");
exit (1);
}.
. pharse_params
{for (int p = 1; p < num_params-1; p++)
{if (strcmp (shell_params [p], "-compiler") == 0)
grab_compiler
else {strcat (options, shell_params [p]);
strcat (options, " ");
};
};
strcpy (src_name, shell_params [num_params-1]);
}.
. grab_compiler
{any_gcc_executable = true;
strcpy (gcc_exec, shell_params [p+1]);
p++;
}.
}
|