legacy-cc

The earliest versions of the very first c compiler known to exist in the wild written by the late legend himself dmr. https://github.com/mortdeus/legacy-cc
git clone http://git.hanabi.in/repos/legacy-cc.git
Log | Files | Refs | README

cvopt.c (4112B)


      1 main() {
      2 /*
      3 	A1 -> A
      4 	A2    B
      5 	A     O
      6 	B1    C
      7 	B2    D
      8 	BE    L
      9 	BF    P
     10 	C1    E
     11 	C2    F
     12 	F     G
     13 	H     H
     14 	R     I
     15 	R1    J
     16 	S     K
     17 	I     M
     18 	M     N
     19 
     20 		*	+1
     21 		S	+2
     22 		C	+4
     23 		1	+8
     24 
     25 	z  -> 4
     26 	c     10
     27 	a     14
     28 	e     20
     29 	n     63
     30 	*	+0100
     31 */
     32 
     33 	extern putchar,getc,peekc,printf,flag,flush;
     34 
     35 	auto c,snlflg,nlflg,t,smode,m,ssmode;
     36 
     37 	smode = nlflg = snlflg = ssmode = 0;
     38 loop:
     39 	c = getc();
     40 	if (c!='\n' & c!='\t') nlflg = 0;
     41 	if (ssmode!=0 & c!='%') {
     42 		ssmode = 0;
     43 		printf(".data\n1:<");
     44 	}
     45 	switch(c) {
     46 
     47 	case '\0':
     48 		printf(".text; 0\n");
     49 		flush();
     50 		return;
     51 
     52 	case ':':
     53 		if (!smode)
     54 			printf("=.+2; 0"); else
     55 			putchar(':');
     56 		goto loop;
     57 
     58 	case 'A':
     59 		if ((c=getc())=='1' | c=='2') {
     60 			putchar(c+'A'-'1');
     61 			goto loop;
     62 		}
     63 		putchar('O');
     64 		peekc = c;
     65 		goto loop;
     66 
     67 	case 'B':
     68 		switch (getc()) {
     69 
     70 		case '1':
     71 			putchar('C');
     72 			goto loop;
     73 
     74 		case '2':
     75 			putchar('D');
     76 			goto loop;
     77 
     78 		case 'E':
     79 			putchar('L');
     80 			goto loop;
     81 
     82 		case 'F':
     83 			putchar('P');
     84 			goto loop;
     85 		}
     86 		putchar('?');
     87 		goto loop;
     88 
     89 	case 'C':
     90 		putchar(getc()+'E'-'1');
     91 		goto loop;
     92 
     93 	case 'F':
     94 		putchar('G');
     95 		goto subtre;
     96 
     97 	case 'R':
     98 		if ((c=getc()) == '1')
     99 		putchar('J'); else {
    100 			putchar('I');
    101 			peekc = c;
    102 		}
    103 		goto loop;
    104 
    105 	case 'H':
    106 		putchar('H');
    107 		goto subtre;
    108 
    109 	case 'I':
    110 		putchar('M');
    111 		goto loop;
    112 
    113 	case 'M':
    114 		putchar('N');
    115 		snlflg++;
    116 		goto loop;
    117 
    118 	case 'S':
    119 		putchar('K');
    120 subtre:
    121 		snlflg = 1;
    122 		t = 'A';
    123 l1:
    124 		switch (c=getc()) {
    125 
    126 		case '*':
    127 			t++;
    128 			goto l1;
    129 
    130 		case 'S':
    131 			t =+ 2;
    132 			goto l1;
    133 
    134 		case 'C':
    135 			t =+ 4;
    136 			goto l1;
    137 
    138 		case '1':
    139 			t =+ 8;
    140 			goto l1;
    141 		}
    142 		peekc = c;
    143 		putchar(t);
    144 		goto loop;
    145 
    146 	case '#':
    147 		if(getc()=='1')
    148 			putchar('#'); else
    149 			putchar('"');
    150 		goto loop;
    151 
    152 	case '%':
    153 		if (smode)
    154 			printf(".text;");
    155 loop1:
    156 		switch (c=getc()) {
    157 
    158 		case 'a':
    159 			m = 16;
    160 			t = flag();
    161 			goto pf;
    162 
    163 		case ',':
    164 			putchar(';');
    165 			goto loop1;
    166 
    167 		case 'i':
    168 			m = 12;
    169 			t = flag();
    170 			goto pf;
    171 		case 'z':
    172 			m = 4;
    173 			t = 0;
    174 			goto pf;
    175 
    176 		case 'c':
    177 			t = 0;
    178 			m = 8;
    179 			goto pf;
    180 
    181 		case 'e':
    182 			t = flag();
    183 			m = 20;
    184 			goto pf;
    185 
    186 		case 'n':
    187 			t = flag();
    188 			m = 63;
    189 pf:
    190 			if ((c=getc())=='*')
    191 				m =+ 0100; else
    192 				peekc = c;
    193 			printf(".byte %o,%o", m, t);
    194 			goto loop1;
    195 
    196 		case '\n':
    197 			printf(";1f\n");
    198 			ssmode = 1;
    199 			nlflg = 1;
    200 			smode = 1;
    201 			goto loop;
    202 		}
    203 		putchar(c);
    204 		goto loop1;
    205 
    206 	case '\t':
    207 		if (nlflg) {
    208 			nlflg = 0;
    209 			goto loop;
    210 		}
    211 		putchar('\t');
    212 		goto loop;
    213 
    214 	case '\n':
    215 		if (!smode)  {
    216 			putchar('\n');
    217 			goto loop;
    218 		}
    219 		if (nlflg) {
    220 			nlflg = 0;
    221 			printf("\\0>\n.text\n");
    222 			smode = 0;
    223 			goto loop;
    224 		}
    225 		if (!snlflg)
    226 			printf("\\n");
    227 		snlflg = 0;
    228 		printf(">\n<");
    229 		nlflg = 1;
    230 		goto loop;
    231 	}
    232 	putchar(c);
    233 	goto loop;
    234 }
    235 
    236 getc() {
    237 	extern getchar, peekc, nofloat;
    238 	auto t, ifcnt;
    239 
    240 	ifcnt = 0;
    241 gc:
    242 	if (peekc) {
    243 		t = peekc;
    244 		peekc = 0;
    245 	} else
    246 		t = getchar();
    247 	if (t==0)
    248 		return(0);
    249 	if (t=='{') {
    250 		ifcnt++;
    251 		t = getchar();
    252 	}
    253 	if (t=='}') {
    254 		t = getc();
    255 		if (--ifcnt==0)
    256 			if (t=='\n')
    257 				t = getc();
    258 	}
    259 	if (ifcnt & nofloat)
    260 		goto gc;
    261 	return(t);
    262 }
    263 
    264 flag() {
    265 	extern getc, peekc;
    266 	auto c, f;
    267 
    268 	f = 0;
    269 l1:
    270 	switch(c=getc()) {
    271 
    272 	case 'w':
    273 		f = 1;
    274 		goto l1;
    275 
    276 	case 'i':
    277 		f = 2;
    278 		goto l1;
    279 
    280 	case 'b':
    281 		f = 3;
    282 		goto l1;
    283 
    284 	case 'f':
    285 		f = 4;
    286 		goto l1;
    287 
    288 	case 'd':
    289 		f = 5;
    290 		goto l1;
    291 
    292 	case 'p':
    293 		f =+ 16;
    294 		goto l1;
    295 	}
    296 	peekc = c;
    297 	return(f);
    298 }
    299 
    300 peekc 0;
    301 
    302 putchar(c) {
    303 	extern flush, oubuf, ouptr;
    304 	char ouptr[], oubuf[];
    305 	auto c1;
    306 
    307 	goto init;
    308 init:
    309 	ouptr = oubuf;
    310 	init = init1;
    311 init1:
    312 	if(c1 = c>>8) {
    313 		*ouptr++ = c1;
    314 		if(ouptr >= oubuf+512)
    315 			flush();
    316 	}
    317 	if(c =& 0377) {
    318 			*ouptr++ = c;
    319 		if(ouptr >= oubuf+512)
    320 			flush();
    321 	}
    322 }
    323 
    324 flush() {
    325 	extern ouptr, oubuf, fout, write;
    326 	char ouptr[], oubuf[];
    327 
    328 	write(fout, oubuf, ouptr-oubuf);
    329 	ouptr = oubuf;
    330 }
    331 
    332 getcha() {
    333 	extern read, incnt, fin, inbuf, inptr;
    334 	char inbuf[], inptr[];
    335 
    336 	goto init;
    337 init:
    338 	inptr = inbuf;
    339 	init = init1;
    340 init1:
    341 	if(inptr >= inbuf+incnt) {
    342 		inptr = inbuf;
    343 		incnt = read(fin, inbuf, 512);
    344 		if(!incnt)
    345 			return('\0');
    346 	}
    347 	return(*inptr++);
    348 }
    349 
    350 inbuf[256];
    351 oubuf[256];
    352 inptr 0;
    353 incnt 0;
    354 ouptr 0;
    355 fin 0;
    356 fout 1;
    357 nofloat 0;