B compiler update

More progress
This commit is contained in:
rswier
2016-03-13 03:41:14 -04:00
parent a3e3a263ef
commit a7b4608711
5 changed files with 333 additions and 337 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ getc: 0
dac cibufp dac cibufp
jmp getc+1 jmp getc+1
1: 1:
lac 0o4 lac o4
jmp getc i jmp getc i
putc: 0 putc: 0
+1 -1
View File
@@ -7,7 +7,7 @@ loop:
loop1: loop1:
c = read(); c = read();
if(c==4) return; if(c==4) return;
if(c==';' & state==0) state = 2; if(c==':' & state==0) state = 2;
if((c<'0' ^ c>'9'&c<'a' ^ c>'z') & state==0) state = 1; if((c<'0' ^ c>'9'&c<'a' ^ c>'z') & state==0) state = 1;
line[i] = c; line[i] = c;
i = i+1; i = i+1;
+1 -1
View File
@@ -78,7 +78,7 @@ getc: 0
dac cibufp dac cibufp
jmp getc+1 jmp getc+1
1: 1:
lac 0o4 lac o4
jmp getc i jmp getc i
putc: 0 putc: 0
+1 -1
View File
@@ -7,7 +7,7 @@ loop:
loop1: loop1:
c = read(); c = read();
if(c==4) return; if(c==4) return;
if(c==';' & state==0) state = 2; if(c==':' & state==0) state = 2;
if((c<'0' ^ c>'9'&c<'a' ^ c>'z') & state==0) state = 1; if((c<'0' ^ c>'9'&c<'a' ^ c>'z') & state==0) state = 1;
line[i] = c; line[i] = c;
i = i+1; i = i+1;
+165 -169
View File
@@ -26,7 +26,7 @@
int fin = 0; int fin = 0;
int fout = 1; int fout = 1;
int symtab[500] = { /* , type = 1, value = 1, name = 4 */ int symtab[500] = { /* class value name */
1, 5,'a','u','t','o', 0 , 1, 5,'a','u','t','o', 0 ,
1, 6,'e','x','t','r','n', 0 , 1, 6,'e','x','t','r','n', 0 ,
1,10,'g','o','t','o', 0 , 1,10,'g','o','t','o', 0 ,
@@ -34,7 +34,6 @@ int symtab[500] = { /* , type = 1, value = 1, name = 4 */
1,12,'i','f', 0 , 1,12,'i','f', 0 ,
1,13,'w','h','i','l','e', 0 , 1,13,'w','h','i','l','e', 0 ,
1,14,'e','l','s','e', 0 , 1,14,'e','l','s','e', 0 ,
0
}; };
int ctab[] = { int ctab[] = {
@@ -64,8 +63,10 @@ int peekc = 0;
int eof = 0; int eof = 0;
int line = 1; int line = 1;
int *csym = 0; int *csym = 0;
int *ns;
int cval = 0; int cval = 0;
int isn = 1; int isn = 0;
int nisn = 0;
int nerror = 0; int nerror = 0;
int nauto = 0; int nauto = 0;
@@ -84,8 +85,9 @@ int main(int argc, char **argv) {
} }
while (!eof) { while (!eof) {
ns = symtab + 51;
extdef(); extdef();
// blkend(); blkend();
} }
xflush(); xflush();
@@ -94,9 +96,8 @@ int main(int argc, char **argv) {
int *lookup() { int *lookup() {
int *np, *sp, *rp; int *np, *sp, *rp;
rp = symtab; rp = symtab;
while (*rp) { while (rp < ns) {
np = rp + 2; np = rp + 2;
sp = symbuf; sp = symbuf;
while (*np==*sp) { while (*np==*sp) {
@@ -110,18 +111,18 @@ int *lookup() {
rp = np + 1; rp = np + 1;
} }
sp = symbuf; sp = symbuf;
np = rp + 2; if (ns >= symtab + 490) {
if (np >= symtab + 490) {
error("sf"); error("sf");
exit(1); exit(1);
} }
loop: *ns = 0;
*np = *sp; ns[1] = 0;
np = np + 1; ns = ns + 2;
while (*ns = *sp) {
ns = ns + 1;
sp = sp + 1; sp = sp + 1;
if (*sp) }
goto loop; ns = ns + 1;
*np = 0;
return rp; return rp;
} }
@@ -150,13 +151,13 @@ symbol() {
loop: loop:
ct = ctab[c]; ct = ctab[c];
if (ct==0) { /* EOT */ if (ct==0) { /* eof */
eof++; eof++;
return(0); return(0);
} }
if (ct==126) { /* white space */ if (ct==126) { /* white space */
if (c=='\n') /* newline */ if (c=='\n')
line = line + 1; line = line + 1;
c = xread(); c = xread();
goto loop; goto loop;
@@ -188,7 +189,7 @@ com:
com1: com1:
if (c==4) { if (c==4) {
eof++; eof++;
error('*/'); /* end of text */ error('*/'); /* eof */
return(0); return(0);
} }
if (c=='\n') if (c=='\n')
@@ -224,14 +225,14 @@ com1:
if (sp<symbuf+9) *sp++ = c; if (sp<symbuf+9) *sp++ = c;
c = xread(); c = xread();
} }
*sp = '\0'; *sp = 0;
peekc = c; peekc = c;
csym = lookup(); csym = lookup();
if (csym[0]==1) { /* keyword */ if (csym[0]==1) {
cval = csym[1]; cval = csym[1];
return(19); return(19); /* keyword */
} }
return(20); return(20); /* name */
} }
if (ct==127) { /* unknown */ if (ct==127) { /* unknown */
error('sy'); error('sy');
@@ -310,7 +311,7 @@ void expr(int lev) {
o = symbol(); o = symbol();
if (o==21) { /* constant */ if (o==21) { /* number */
case21: case21:
gen('n',5); /* litrl */ gen('n',5); /* litrl */
number(cval); number(cval);
@@ -320,18 +321,26 @@ case21:
if (o==20) { /* name */ if (o==20) { /* name */
if (*csym==0) { /* not seen */ if (*csym==0) { /* not seen */
// if((peeksym=symbol())==6) { /* ( */ if((peeksym=symbol())==6) { /* ( */
*csym = 6; /* extern */ *csym = 6; /* extrn */
// } } else {
// else { *csym = 2; /* internal */
// if(csym[2]==0) /* unseen so far */ csym[1] = ++isn;
// csym[2] = isn++;
// }
} }
if(*csym==6) /* extern */ }
gens('x',csym+2); if(*csym==5) /* auto */
else
gen('a',csym[1]); gen('a',csym[1]);
else {
xwrite('x ');
if (*csym==6) { /* extrn */
xwrite('.');
name(csym+2);
} else { /* internal */
xwrite('l');
number(csym[1]);
}
xwrite('\n');
}
goto loop; goto loop;
} }
@@ -343,7 +352,7 @@ case21:
if (o==41) { /* - */ if (o==41) { /* - */
peeksym = symbol(); peeksym = symbol();
if (peeksym==21) { /* constant */ if (peeksym==21) { /* number */
peeksym = -1; peeksym = -1;
cval = -cval; cval = -cval;
goto case21; goto case21;
@@ -438,7 +447,7 @@ loop:
return; return;
} }
} }
gen('n',3); gen('n',3); /* call */
} }
goto loop; goto loop;
} }
@@ -446,6 +455,18 @@ loop:
peeksym = o; peeksym = o;
} }
pexpr()
{
auto o, t;
if ((o=symbol())==6) { /* ( */
expr(15);
if ((o=symbol())==7) /* ) */
return;
}
error('()');
}
declare(kw) { declare(kw) {
// extern csym[], symbol, paraml[], parame[]; // extern csym[], symbol, paraml[], parame[];
// extern error, cval, peeksym, exit; // extern error, cval, peeksym, exit;
@@ -453,103 +474,94 @@ declare(kw) {
int o; int o;
while((o=symbol())==20) { /* name */ while((o=symbol())==20) { /* name */
*csym = kw; if (kw==6) { /* extrn */
#if 0 *csym = 6;
while((o=symbol())==4) { /* [ */ o = symbol();
if((o=symbol())==21) { /* const */ } else { /* auto/param */
if(csym[1]>=020) *csym = 5; /* auto */
error('de'); //error("Bad vector"); csym[1] = nauto;
csym[3] = cval; o = symbol();
if (kw==5 & o==21) { /* auto & number */
gen('y',nauto); /* aryop */
nauto = nauto + cval;
o = symbol(); o = symbol();
} }
if (o!=5) /* ] */
goto syntax;
csym[1] += 020;
}
#endif
if(kw==5) { /* auto */
csym[1] = nauto;
nauto++; nauto++;
*csym = -1;
} }
if (o!=9) /* , */ if (o!=9) /* , */
goto done; goto done;
} }
done: done:
if(o==1 | o==7) /* ; or ')' */ if(o==1 & kw!=8 | o==7 & kw==8) /* auto/extrn ; param ')' */
return; return;
syntax: syntax:
error('[]'); //error("Declaration syntax"); error('[]'); /* declaration syntax */
} }
extdef() { extdef() {
// extern eof, cval; // extern eof, cval;
// extern symbol, block, printf, pname, csym[]; // extern symbol, block, printf, pname, csym[];
// extern error; // extern error;
auto o, c, *cs; auto o, c;
// char s;
o = symbol(); o = symbol();
if(o==0 | o==1) /* EOF */ if(o==0 | o==1) /* eof ; */
return; return;
if(o!=20) if(o!=20) /* name */
goto syntax; goto syntax;
csym[0] = 6; csym[0] = 6; /* extrn */
cs = csym + 2;
xwrite('.'); xwrite('.');
name(cs); name(csym + 2);
xwrite(': '); xwrite(': ');
o=symbol();
if (o==2 | o==6) { /* $( ( */
xwrite('.+'); xwrite('.+');
xwrite('1\n'); xwrite('1\n');
// s = ".data; %p:1f\n"; nauto = 2;
o=symbol();
if (o==6) { /* ( */ if (o==6) { /* ( */
nauto = 2; declare(8); /* param */
declare(5); if ((o=symbol())!=2) /* $( */
statement(1);
gen('n',7); /* retrn */
return;
}
if (o==2) { /* $( */
peeksym = o;
nauto = 2;
statement(1);
gen('n',7); /* retrn */
return;
}
if (o==21) { /* const */
printf(".data; %p: %o\n", cs, cval);
if((o=symbol())!=1) /* ; */
goto syntax; goto syntax;
}
while((o=symbol())==19 && cval<10) /* auto extrn */
declare(cval);
peeksym = o;
gen('s',nauto); /* setop */
stmtlist();
gen('n',7); /* retrn */
return;
}
if (o==21) { /* number */
number(cval);
xwrite('\n');
return; return;
} }
if (o==1) { /* ; */ if (o==1) { /* ; */
printf(".bss; %p: .=.+2\n", cs); xwrite('0\n');
return; return;
} }
if (o==4) { /* [ */ if (o==4) { /* [ */
c = 0; c = 0;
if((o=symbol())==21) { /* const */ if ((o=symbol())==21) { /* number */
c = cval<<1; c = cval;
o = symbol(); o = symbol();
} }
if (o!=5) /* ] */ if (o!=5) /* ] */
goto syntax; goto syntax;
if((o=symbol())==1) { /* ; */ xwrite('.+');
printf(".bss; 1:.=.+%o\n", c); xwrite('1\n');
return; if ((o=symbol())==1) /* ; */
} goto done;
printf("1:"); while (o==21) { /* number */
while(o==21) { /* const */ number(cval);
printf("%o\n", cval); xwrite('\n');
c =- 2; c--;
if ((o=symbol())==1) /* ; */ if ((o=symbol())==1) /* ; */
goto done; goto done;
if (o!=9) /* , */ if (o!=9) /* , */
@@ -559,20 +571,35 @@ extdef() {
} }
goto syntax; goto syntax;
done: done:
if(c>0) if (c>0) {
printf(".=.+%o\n", c); xwrite('.=');
xwrite('.+');
number(c);
xwrite('\n');
}
return; return;
} }
if (o==0) /* EOF */ if (o==0) /* eof */
return; return;
syntax: syntax:
error('xx'); error('xx');
statement(0); stmt();
} }
statement(d) { stmtlist() {
int o;
while (!eof) {
if ((o = symbol())==3) /* $) */
return;
peeksym = o;
stmt();
}
error('$)'); /* missing $) */
}
stmt() {
// extern symbol, error, blkhed, eof, peeksym; // extern symbol, error, blkhed, eof, peeksym;
// extern blkend, csym[], rcexpr, block[], tree[], regtab[]; // extern blkend, csym[], rcexpr, block[], tree[], regtab[];
// extern jumpc, jump, label, contlab, brklab, cval; // extern jumpc, jump, label, contlab, brklab, cval;
@@ -580,33 +607,22 @@ statement(d) {
// extern efftab[], declare, deflab, swtab[], swsiz, branch; // extern efftab[], declare, deflab, swtab[], swsiz, branch;
// int o, o1, o2, np[]; // int o, o1, o2, np[];
int o, o1, o2, n, *np; int o, o1, o2;
stmt: next:
o = symbol(); o = symbol();
if (o==0) /* EOF */ if (o==0) /* eof */
{ {
error('fe'); //"Unexpected EOF"); error('fe'); /* Unexpected eof */
return; return;
} }
if (o==1 || o==3) /* ; } */ if (o==1 || o==3) /* ; $) */
return; return;
if (o==2) { /* { */ if (o==2) { /* $( */
if(d) { stmtlist();
declist();
gen('s',nauto); /* setop */
}
while (!eof) {
o = symbol();
if (o==3) /* } */
return;
peeksym = o;
statement(0);
}
error('$)'); //error("Missing '}'");
return; return;
} }
@@ -627,17 +643,17 @@ stmt:
if (cval==12) { /* if */ if (cval==12) { /* if */
pexpr(); pexpr();
o1 = isn;
isn++; isn++;
o1 = isn;
jumpc(o1); jumpc(o1);
statement(0); stmt();
o = symbol(); o = symbol();
if (o==19 & cval==14) { /* else */ if (o==19 & cval==14) { /* else */
o2 = isn;
isn++; isn++;
o2 = isn;
jump(o2); jump(o2);
label(o1); label(o1);
statement(0); stmt();
label(o2); label(o2);
return; return;
} }
@@ -647,14 +663,14 @@ stmt:
} }
if (cval==13) { /* while */ if (cval==13) { /* while */
o1 = isn;
isn++; isn++;
o1 = isn;
label(o1); label(o1);
pexpr(); pexpr();
o2 = isn;
isn++; isn++;
o2 = isn;
jumpc(o2); jumpc(o2);
statement(0); stmt();
jump(o1); jump(o1);
label(o2); label(o2);
return; return;
@@ -664,20 +680,18 @@ stmt:
goto syntax; goto syntax;
} }
if (o==20) { /* name */ if (o==20 & peekc==':') { /* name : */
if (peekc==':') {
peekc = 0; peekc = 0;
if (csym[0]>0) { if (!*csym) {
*csym = 2; /* param */
++isn;
csym[1] = isn;
} else if (*csym != 2) {
error('rd'); error('rd');
goto stmt; goto next;
}
csym[0] = 2;
csym[1] = 020; /* int[] */
// if (csym[2]==0)
// csym[2] = isn++;
// slabel();
goto stmt;
} }
label(csym[1]);
goto next;
} }
peeksym = o; peeksym = o;
@@ -691,37 +705,20 @@ semi:
syntax: syntax:
error('sz'); error('sz');
goto stmt; goto next;
} }
pexpr() blkend() {
{ while (nisn < isn) {
auto o, t; ++nisn;
xwrite('l');
if ((o=symbol())==6) { /* ( */ number(nisn);
expr(15); xwrite(': ');
if ((o=symbol())==7) /* ) */ xwrite('ll');
return; number(nisn);
}
error('()');
}
declist()
{
// extern peeksym, peekc, csym[], cval;
auto o;
while((o=symbol())==19 & cval<10)
declare(cval);
peeksym = o;
}
gens(o,n) {
xwrite(o);
xwrite(' .');
name(n);
xwrite('\n'); xwrite('\n');
} }
}
gen(o,n) { gen(o,n) {
xwrite(o); xwrite(o);
@@ -732,25 +729,22 @@ gen(o,n) {
jumpc(n) { jumpc(n) {
xwrite('f '); /* ifop */ xwrite('f '); /* ifop */
xwrite(' .'); xwrite('l');
number(n); number(n);
xwrite('\n'); xwrite('\n');
} }
jump(lbl) { jump(n) {
xwrite('t '); /* traop */ xwrite('t '); /* traop */
xwrite(' .'); xwrite('ll');
number(lbl); number(n);
xwrite('\n'); xwrite('\n');
} }
label(lbl) { label(n) {
xwrite('.'); xwrite('ll');
number(lbl); number(n);
xwrite(': '); xwrite(':\n');
xwrite('t ');
xwrite('.+');
xwrite('1\n');
} }
printn(n) { printn(n) {
@@ -793,6 +787,8 @@ error(code)
} }
printn(line); printn(line);
xwrite('\n'); xwrite('\n');
if (nerror==20)
exit(20);
fout = f; fout = f;
} }