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;
+329 -333
View File
@@ -26,15 +26,14 @@
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 ,
1,11,'r','e','t','u','r','n',0, 1,11,'r','e','t','u','r','n', 0 ,
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,19 +85,19 @@ int main(int argc, char **argv) {
} }
while (!eof) { while (!eof) {
ns = symtab + 51;
extdef(); extdef();
// blkend(); blkend();
} }
xflush(); xflush();
exit(nerror); exit(nerror);
} }
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;
sp = sp + 1; while (*ns = *sp) {
if (*sp) ns = ns + 1;
goto loop; sp = sp + 1;
*np = 0; }
ns = ns + 1;
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')
@@ -201,7 +202,7 @@ com1:
c = xread(); c = xread();
goto loop; goto loop;
} }
if (ct==124) { /* number */ if (ct==124) { /* number */
cval = 0; cval = 0;
if (c=='0') if (c=='0')
b = 8; b = 8;
@@ -214,26 +215,26 @@ com1:
peekc = c; peekc = c;
return(21); return(21);
} }
if (c=='\'') { /* ' */ if (c=='\'') { /* ' */
getcc(); getcc();
return(21); return(21);
} }
if (ct==123) { /* letter */ if (ct==123) { /* letter */
sp = symbuf; sp = symbuf;
while(ctab[c]==123 | ctab[c]==124) { while(ctab[c]==123 | ctab[c]==124) {
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');
c = xread(); c = xread();
goto loop; goto loop;
@@ -309,32 +310,40 @@ void expr(int lev) {
int o; int o;
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);
xwrite('\n'); xwrite('\n');
goto loop; goto loop;
} }
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 */ if(*csym==5) /* auto */
gens('x',csym+2);
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;
} }
if (o==34) { /* ! */ if (o==34) { /* ! */
expr(1); expr(1);
gen('u',4); /* unot */ gen('u',4); /* unot */
@@ -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;
@@ -352,19 +361,19 @@ case21:
gen('u',2); /* umin */ gen('u',2); /* umin */
goto loop; goto loop;
} }
if (o==47) { /* & */ if (o==47) { /* & */
expr(1); expr(1);
gen('u',1); /* uadr */ gen('u',1); /* uadr */
goto loop; goto loop;
} }
if (o==42) { /* * */ if (o==42) { /* * */
expr(1); expr(1);
gen('u',3); /* uind */ gen('u',3); /* uind */
goto loop; goto loop;
} }
if (o==6) { /* ( */ if (o==6) { /* ( */
peeksym = o; peeksym = o;
pexpr(); pexpr();
@@ -390,27 +399,27 @@ loop:
gen('b',3); /* band */ gen('b',3); /* band */
goto loop; goto loop;
} }
if (lev>=7 & o>=60 & o<=61) { /* == != */ if (lev>=7 & o>=60 & o<=61) { /* == != */
expr(6); expr(6);
gen('b',o-56); /* beq bne */ gen('b',o-56); /* beq bne */
goto loop; goto loop;
} }
if (lev>=6 & o>=62 & o<=65) { /* <= < >= > */ if (lev>=6 & o>=62 & o<=65) { /* <= < >= > */
expr(5); expr(5);
gen('b',o-56); /* ble blt bge bgt */ gen('b',o-56); /* ble blt bge bgt */
goto loop; goto loop;
} }
if (lev>=4 & o>=40 & o<=41) { /* + - */ if (lev>=4 & o>=40 & o<=41) { /* + - */
expr(3); expr(3);
gen('b',o-28); /* badd bmin */ gen('b',o-28); /* badd bmin */
goto loop; goto loop;
} }
if (lev>=3 & o>=42 & o<=43) { /* * / */ if (lev>=3 & o>=42 & o<=43) { /* * / */
expr(2); expr(2);
gen('b',o-27); /* bmul bdiv */ gen('b',o-27); /* bmul bdiv */
goto loop; goto loop;
} }
if (lev>=3 & o==44) { /* % */ if (lev>=3 & o==44) { /* % */
expr(2); expr(2);
gen('b',14); /* bmod */ gen('b',14); /* bmod */
goto loop; goto loop;
@@ -438,262 +447,14 @@ loop:
return; return;
} }
} }
gen('n',3); gen('n',3); /* call */
} }
goto loop; goto loop;
} }
peeksym = o; peeksym = o;
} }
declare(kw) {
// extern csym[], symbol, paraml[], parame[];
// extern error, cval, peeksym, exit;
// int t[], n, o;
int o;
while((o=symbol())==20) { /* name */
*csym = kw;
#if 0
while((o=symbol())==4) { /* [ */
if((o=symbol())==21) { /* const */
if(csym[1]>=020)
error('de'); //error("Bad vector");
csym[3] = cval;
o = symbol();
}
if (o!=5) /* ] */
goto syntax;
csym[1] += 020;
}
#endif
if(kw==5) { /* auto */
csym[1] = nauto;
nauto++;
*csym = -1;
}
if (o!=9) /* , */
goto done;
}
done:
if(o==1 | o==7) /* ; or ')' */
return;
syntax:
error('[]'); //error("Declaration syntax");
}
extdef() {
// extern eof, cval;
// extern symbol, block, printf, pname, csym[];
// extern error;
auto o, c, *cs;
// char s;
o = symbol();
if(o==0 | o==1) /* EOF */
return;
if(o!=20)
goto syntax;
csym[0] = 6;
cs = csym + 2;
xwrite('.');
name(cs);
xwrite(': ');
xwrite('.+');
xwrite('1\n');
// s = ".data; %p:1f\n";
o=symbol();
if (o==6) { /* ( */
nauto = 2;
declare(5);
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;
return;
}
if (o==1) { /* ; */
printf(".bss; %p: .=.+2\n", cs);
return;
}
if (o==4) { /* [ */
c = 0;
if((o=symbol())==21) { /* const */
c = cval<<1;
o = symbol();
}
if(o!=5) /* ] */
goto syntax;
if((o=symbol())==1) { /* ; */
printf(".bss; 1:.=.+%o\n", c);
return;
}
printf("1:");
while(o==21) { /* const */
printf("%o\n", cval);
c =- 2;
if((o=symbol())==1) /* ; */
goto done;
if(o!=9) /* , */
goto syntax;
else
o = symbol();
}
goto syntax;
done:
if(c>0)
printf(".=.+%o\n", c);
return;
}
if (o==0) /* EOF */
return;
syntax:
error('xx');
statement(0);
}
statement(d) {
// extern symbol, error, blkhed, eof, peeksym;
// extern blkend, csym[], rcexpr, block[], tree[], regtab[];
// extern jumpc, jump, label, contlab, brklab, cval;
// extern swp[], isn, pswitch, peekc, slabel;
// extern efftab[], declare, deflab, swtab[], swsiz, branch;
// int o, o1, o2, np[];
int o, o1, o2, n, *np;
stmt:
o = symbol();
if (o==0) /* EOF */
{
error('fe'); //"Unexpected EOF");
return;
}
if (o==1 || o==3) /* ; } */
return;
if (o==2) { /* { */
if(d) {
declist();
gen('s',nauto); /* setop */
}
while (!eof) {
o = symbol();
if (o==3) /* } */
return;
peeksym = o;
statement(0);
}
error('$)'); //error("Missing '}'");
return;
}
if (o==19) { /* keyword */
if (cval==10) { /* goto */
expr(15);
gen('n',6); /* goto */
goto semi;
}
if (cval==11) { /* return */
if((peeksym=symbol())==6) /* ( */
pexpr();
gen('n',7); /* retrn */
goto semi;
}
if (cval==12) { /* if */
pexpr();
o1 = isn;
isn++;
jumpc(o1);
statement(0);
o = symbol();
if (o==19 & cval==14) { /* else */
o2 = isn;
isn++;
jump(o2);
label(o1);
statement(0);
label(o2);
return;
}
peeksym = o;
label(o1);
return;
}
if (cval==13) { /* while */
o1 = isn;
isn++;
label(o1);
pexpr();
o2 = isn;
isn++;
jumpc(o2);
statement(0);
jump(o1);
label(o2);
return;
}
error('sx');
goto syntax;
}
if (o==20) { /* name */
if (peekc==':') {
peekc = 0;
if (csym[0]>0) {
error('rd');
goto stmt;
}
csym[0] = 2;
csym[1] = 020; /* int[] */
// if (csym[2]==0)
// csym[2] = isn++;
// slabel();
goto stmt;
}
}
peeksym = o;
expr(15);
gen('s',nauto); /* setop */
semi:
o = symbol();
if (o==1) /* ; */
return;
syntax:
error('sz');
goto stmt;
}
pexpr() pexpr()
{ {
auto o, t; auto o, t;
@@ -706,21 +467,257 @@ pexpr()
error('()'); error('()');
} }
declist() declare(kw) {
{ // extern csym[], symbol, paraml[], parame[];
// extern peeksym, peekc, csym[], cval; // extern error, cval, peeksym, exit;
auto o; // int t[], n, o;
int o;
while((o=symbol())==19 & cval<10) while((o=symbol())==20) { /* name */
declare(cval); if (kw==6) { /* extrn */
peeksym = o; *csym = 6;
o = symbol();
} else { /* auto/param */
*csym = 5; /* auto */
csym[1] = nauto;
o = symbol();
if (kw==5 & o==21) { /* auto & number */
gen('y',nauto); /* aryop */
nauto = nauto + cval;
o = symbol();
}
nauto++;
}
if (o!=9) /* , */
goto done;
}
done:
if(o==1 & kw!=8 | o==7 & kw==8) /* auto/extrn ; param ')' */
return;
syntax:
error('[]'); /* declaration syntax */
} }
gens(o,n) { extdef() {
xwrite(o); // extern eof, cval;
xwrite(' .'); // extern symbol, block, printf, pname, csym[];
name(n); // extern error;
xwrite('\n'); auto o, c;
o = symbol();
if(o==0 | o==1) /* eof ; */
return;
if(o!=20) /* name */
goto syntax;
csym[0] = 6; /* extrn */
xwrite('.');
name(csym + 2);
xwrite(': ');
o=symbol();
if (o==2 | o==6) { /* $( ( */
xwrite('.+');
xwrite('1\n');
nauto = 2;
if (o==6) { /* ( */
declare(8); /* param */
if ((o=symbol())!=2) /* $( */
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;
}
if (o==1) { /* ; */
xwrite('0\n');
return;
}
if (o==4) { /* [ */
c = 0;
if ((o=symbol())==21) { /* number */
c = cval;
o = symbol();
}
if (o!=5) /* ] */
goto syntax;
xwrite('.+');
xwrite('1\n');
if ((o=symbol())==1) /* ; */
goto done;
while (o==21) { /* number */
number(cval);
xwrite('\n');
c--;
if ((o=symbol())==1) /* ; */
goto done;
if (o!=9) /* , */
goto syntax;
else
o = symbol();
}
goto syntax;
done:
if (c>0) {
xwrite('.=');
xwrite('.+');
number(c);
xwrite('\n');
}
return;
}
if (o==0) /* eof */
return;
syntax:
error('xx');
stmt();
}
stmtlist() {
int o;
while (!eof) {
if ((o = symbol())==3) /* $) */
return;
peeksym = o;
stmt();
}
error('$)'); /* missing $) */
}
stmt() {
// extern symbol, error, blkhed, eof, peeksym;
// extern blkend, csym[], rcexpr, block[], tree[], regtab[];
// extern jumpc, jump, label, contlab, brklab, cval;
// extern swp[], isn, pswitch, peekc, slabel;
// extern efftab[], declare, deflab, swtab[], swsiz, branch;
// int o, o1, o2, np[];
int o, o1, o2;
next:
o = symbol();
if (o==0) /* eof */
{
error('fe'); /* Unexpected eof */
return;
}
if (o==1 || o==3) /* ; $) */
return;
if (o==2) { /* $( */
stmtlist();
return;
}
if (o==19) { /* keyword */
if (cval==10) { /* goto */
expr(15);
gen('n',6); /* goto */
goto semi;
}
if (cval==11) { /* return */
if((peeksym=symbol())==6) /* ( */
pexpr();
gen('n',7); /* retrn */
goto semi;
}
if (cval==12) { /* if */
pexpr();
isn++;
o1 = isn;
jumpc(o1);
stmt();
o = symbol();
if (o==19 & cval==14) { /* else */
isn++;
o2 = isn;
jump(o2);
label(o1);
stmt();
label(o2);
return;
}
peeksym = o;
label(o1);
return;
}
if (cval==13) { /* while */
isn++;
o1 = isn;
label(o1);
pexpr();
isn++;
o2 = isn;
jumpc(o2);
stmt();
jump(o1);
label(o2);
return;
}
error('sx');
goto syntax;
}
if (o==20 & peekc==':') { /* name : */
peekc = 0;
if (!*csym) {
*csym = 2; /* param */
++isn;
csym[1] = isn;
} else if (*csym != 2) {
error('rd');
goto next;
}
label(csym[1]);
goto next;
}
peeksym = o;
expr(15);
gen('s',nauto); /* setop */
semi:
o = symbol();
if (o==1) /* ; */
return;
syntax:
error('sz');
goto next;
}
blkend() {
while (nisn < isn) {
++nisn;
xwrite('l');
number(nisn);
xwrite(': ');
xwrite('ll');
number(nisn);
xwrite('\n');
}
} }
gen(o,n) { gen(o,n) {
@@ -731,26 +728,23 @@ 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) {
@@ -780,7 +774,7 @@ error(code)
{ {
extern line; extern line;
int f; int f;
nerror++; nerror++;
xflush(); xflush();
f = fout; f = fout;
@@ -793,6 +787,8 @@ error(code)
} }
printn(line); printn(line);
xwrite('\n'); xwrite('\n');
if (nerror==20)
exit(20);
fout = f; fout = f;
} }