void enterbuiltin(alfa x0, int x1, int x2, int x3){ //if ( lnfeedback == 1 ) printf("\nENTERING enter - 1\n"); t = t + 1; strcpy(tab[t].name, x0); tab[t].link = t - 1; tab[t].obj = x1; tab[t].typ = x2; tab[t].ref = 0; tab[t].normal = 1; tab[t].lev = 0; tab[t].adr = x3; if(feedbacklevel == 1 || feedbacklevel ==3) printf("\t\t entered { %s, link = %d, obj = %d, ref = %d, normal %d, lev = %d, adr = %d\n", tab[t].name, tab[t].link, tab[t].obj, 0, 1, 0, x3 ); }// end enter void compile(){ int it; strcpy(key[ 1],"and "); strcpy(key[ 2],"array "); strcpy(key[ 3],"begin "); strcpy(key[ 4],"case "); strcpy(key[ 5],"const "); strcpy(key[ 6],"div "); strcpy(key[ 7],"do "); strcpy(key[ 8],"downto "); strcpy(key[ 9],"else "); strcpy(key[10],"end "); strcpy(key[11],"for "); strcpy(key[12],"function "); strcpy(key[13],"if "); strcpy(key[14],"mod "); strcpy(key[15],"not "); strcpy(key[16],"of "); strcpy(key[17],"or "); strcpy(key[18],"procedure "); strcpy(key[19],"program "); strcpy(key[20],"record "); strcpy(key[21],"repeat "); strcpy(key[22],"then "); strcpy(key[23],"to "); strcpy(key[24],"type "); strcpy(key[25],"until "); strcpy(key[26],"var "); strcpy(key[27],"while "); ksy[ 1] = andsy; ksy[ 2] = arraysy; ksy[ 3] = beginsy; ksy[ 4] = casesy; ksy[ 5] = constsy; ksy[ 6] = idiv; ksy[ 7] = dosy; ksy[ 8] = downtosy; ksy[ 9] = elsesy; ksy[10] = endsy; ksy[11] = forsy; ksy[12] = functionsy; ksy[13] = ifsy; ksy[14] = imod; ksy[15] = notsy; ksy[16] = ofsy; ksy[17] = orsy; ksy[18] = proceduresy; ksy[19] = programsy; ksy[20] = recordsy; ksy[21] = repeatsy; ksy[22] = thensy; ksy[23] = tosy; ksy[24] = typesy; ksy[25] = untilsy; ksy[26] = varsy; ksy[27] = whilesy; sps['+'] = plus; sps['-'] = minus; sps['*'] = times; sps['/'] = rdiv; sps['('] = lparent; sps[')'] = rparent; sps['='] = egl; sps[','] = comma; sps['['] = lbrack; sps[']'] = rbrack; sps['#'] = neg; sps['&'] = andsy; sps[';'] = semicolon; for(it = 0; it < 52; it++){ constbegsys[it] = 0; typebegsys[it] = 0; blockbegsys[it] = 0; facbegsys[it] = 0; statbegsys[it] = 0; if(it <8)stantyps[it] = 0; addSym[it] = 0; } constbegsys[1] = plus; constbegsys[2] = minus; constbegsys[3] = intcon; constbegsys[4] = realcon; constbegsys[5] = charcon; constbegsys[6] = ident; typebegsys[1] = ident; typebegsys[2] = arraysy; typebegsys[3] = recordsy; blockbegsys[1] = constsy; blockbegsys[2] = typesy; blockbegsys[3] = varsy; blockbegsys[4] = proceduresy; blockbegsys[5] = functionsy; blockbegsys[6] = beginsy; facbegsys[1] = intcon; facbegsys[2] = realcon; facbegsys[3] = charcon; facbegsys[4] = ident; facbegsys[5] = lparent; facbegsys[6] = notsy; statbegsys[1] = beginsy; statbegsys[2] = ifsy; statbegsys[3] = whilesy; statbegsys[4] = repeatsy; statbegsys[5] = forsy; statbegsys[6] = casesy; stantyps[1] = notyp; stantyps[2] = ints; stantyps[3] = reals; stantyps[4] = bools; stantyps[5] = chars; addSym[ 1] = constsy; addSym[ 2] = typesy; addSym[ 3] = varsy; addSym[ 4] = proceduresy; addSym[ 5] = functionsy; addSym[ 6] = beginsy; addSym[ 7] = ifsy; addSym[ 8] = whilesy; addSym[ 9] = repeatsy; addSym[10] = forsy; addSym[11] = casesy; strcpy(symbolname[intcon], "intcon"); strcpy(symbolname[realcon], "realcon"); strcpy(symbolname[charcon], "charcon"); strcpy(symbolname[stringt], "stringt"); strcpy(symbolname[notsy], "notsy"); strcpy(symbolname[plus], "plus"); strcpy(symbolname[minus], "minus"); strcpy(symbolname[times], "times"); strcpy(symbolname[idiv], "idiv"); strcpy(symbolname[rdiv], "rdiv"); strcpy(symbolname[imod], "imod"); strcpy(symbolname[andsy], "andsy"); strcpy(symbolname[orsy], "orsy"); strcpy(symbolname[egl], "egl"); strcpy(symbolname[neg], "neg"); strcpy(symbolname[gtr], "gtr"); strcpy(symbolname[geg], "geg"); strcpy(symbolname[lss], "lss"); strcpy(symbolname[leg], "leg"); strcpy(symbolname[lparent], "lparent"); strcpy(symbolname[rparent], "rparent"); strcpy(symbolname[lbrack], "lbrack"); strcpy(symbolname[rbrack], "rbrack"); strcpy(symbolname[comma], "comma"); strcpy(symbolname[semicolon], "semicolon"); strcpy(symbolname[period], "period"); strcpy(symbolname[colon], "colon"); strcpy(symbolname[becomes], "becomes"); strcpy(symbolname[constsy], "contsy"); strcpy(symbolname[typesy], "typesy"); strcpy(symbolname[varsy], "varsy"); strcpy(symbolname[functionsy], "functionsy"); strcpy(symbolname[proceduresy], "proceduresy"); strcpy(symbolname[arraysy], "arraysy"); strcpy(symbolname[recordsy], "recordsy"); strcpy(symbolname[programsy], "programsy"); strcpy(symbolname[ident], "ident"); strcpy(symbolname[beginsy], "beginsy"); strcpy(symbolname[ifsy], "ifsy"); strcpy(symbolname[casesy], "casesy"); strcpy(symbolname[repeatsy], "repeatsy"); strcpy(symbolname[whilesy], "whilesy"); strcpy(symbolname[forsy], "forsy"); strcpy(symbolname[endsy], "endsy"); strcpy(symbolname[elsesy], "elsesy"); strcpy(symbolname[untilsy], "untilsy"); strcpy(symbolname[ofsy], "ofsy"); strcpy(symbolname[dosy], "dosy"); strcpy(symbolname[tosy], "tosy"); strcpy(symbolname[downtosy], "downtosy"); strcpy(symbolname[thensy], "thensy"); lc = 0; ll = 0; cc = 0; ch = ' '; errpos = 0; /*errs = []*/ insymbol(); t = -1; a = 0; b = 1; sx = 0; c2 = 0; display[0] = 1; if ( sy != programsy ) error(3); else{ insymbol(); if ( sy != ident ) error(2); else{ strcpy(progname, id); insymbol(); if ( sy == semicolon ){ iflag = 0; oflag = 1; insymbol(); } else{ if ( sy != lparent ) error(9); else do{ insymbol(); if ( sy != ident ) error(2); else{ if ( !strcmp(id,"input ") ) iflag = 1; else if ( !strcmp(id,"output ") ) oflag = 1; else error(0); insymbol(); } }while( sy == comma ); if ( sy == rparent ) insymbol(); else error(4); } if ( !oflag ) error(20); } } enterbuiltin(" ", variable, notyp, 0); /* sentinel */ enterbuiltin("false ", konstant, bools, 0); enterbuiltin("true ", konstant, bools, 1); enterbuiltin("real ", typel, reals, 1); enterbuiltin("char ", typel, chars, 1); enterbuiltin("boolean ", typel, bools, 1); enterbuiltin("integer ", typel, ints , 1); enterbuiltin("abs ", funktion, reals, 0); enterbuiltin("sqr ", funktion, reals, 2); enterbuiltin("odd ", funktion, bools, 4); enterbuiltin("chr ", funktion, chars, 5); enterbuiltin("ord ", funktion, ints, 6); enterbuiltin("succ ", funktion, chars, 7); enterbuiltin("pred ", funktion, chars, 8); enterbuiltin("round ", funktion, ints, 9); enterbuiltin("trunc ", funktion, ints, 10); enterbuiltin("sin ", funktion, reals, 11); enterbuiltin("cos ", funktion, reals, 12); enterbuiltin("exp ", funktion, reals, 13); enterbuiltin("ln ", funktion, reals, 14); enterbuiltin("sqrt ", funktion, reals, 15); enterbuiltin("arctan ", funktion, reals, 16); enterbuiltin("eof ", funktion, bools, 17); enterbuiltin("eoln ", funktion, bools, 18); enterbuiltin("read ", prozedure, notyp, 1); enterbuiltin("readln ", prozedure, notyp, 2); enterbuiltin("write ", prozedure, notyp, 3); enterbuiltin("writeln ", prozedure, notyp, 4); enterbuiltin(" ", prozedure, notyp, 0); strcpy(mnemonics[lad],"lad"); strcpy(mnemonics[lva], "lva"); strcpy(mnemonics[lin],"lin"); strcpy(mnemonics[upd], "upd"); strcpy(mnemonics[bif],"bif"); strcpy(mnemonics[off], "off"); strcpy(mnemonics[jmp],"jmp"); strcpy(mnemonics[cjp], "cjp"); strcpy(mnemonics[swt],"swt"); strcpy(mnemonics[f1u], "f1u"); strcpy(mnemonics[f2u],"f2u"); strcpy(mnemonics[f1d], "f1d"); strcpy(mnemonics[f2d],"f2d"); strcpy(mnemonics[mks], "mks"); strcpy(mnemonics[cal],"cal"); strcpy(mnemonics[ix1], "ix1"); strcpy(mnemonics[ix2],"ix2"); strcpy(mnemonics[lbl], "lbl"); strcpy(mnemonics[cbl],"cbl"); strcpy(mnemonics[lit], "lit"); strcpy(mnemonics[lre],"lre"); strcpy(mnemonics[flt], "flt"); strcpy(mnemonics[rea],"rea"); strcpy(mnemonics[wst], "wst"); strcpy(mnemonics[wdf],"wdf"); strcpy(mnemonics[wrf], "wrf"); strcpy(mnemonics[hlt],"hlt"); strcpy(mnemonics[xpr], "xpr"); strcpy(mnemonics[xfn],"xfn"); strcpy(mnemonics[fid], "fid"); strcpy(mnemonics[not],"not"); strcpy(mnemonics[ngt], "ngt"); strcpy(mnemonics[wff],"wff"); strcpy(mnemonics[sto], "sto"); strcpy(mnemonics[req],"req"); strcpy(mnemonics[rne], "rne"); strcpy(mnemonics[rlt],"rlt"); strcpy(mnemonics[rle], "rle"); strcpy(mnemonics[rgt],"rgt"); strcpy(mnemonics[rge], "rge"); strcpy(mnemonics[ieq],"ieq"); strcpy(mnemonics[ine], "ine"); strcpy(mnemonics[ilt],"ilt"); strcpy(mnemonics[ile], "ile"); strcpy(mnemonics[igt],"igt"); strcpy(mnemonics[ile], "ige"); strcpy(mnemonics[bor],"bor"); strcpy(mnemonics[iad], "iad"); strcpy(mnemonics[isb],"isb"); strcpy(mnemonics[rad], "rad"); strcpy(mnemonics[rsb],"rsb"); strcpy(mnemonics[bad], "bad"); strcpy(mnemonics[iml],"iml"); strcpy(mnemonics[idv], "idv"); strcpy(mnemonics[imd],"imd"); strcpy(mnemonics[rml], "rml"); strcpy(mnemonics[rdv],"rdv"); strcpy(mnemonics[elr], "elr"); strcpy(mnemonics[elw],"elw"); btab[1].last = t; btab[1].lastpar = 1; btab[1].psize = 0; btab[1].vsize = 0; /* changed from blockbegsys+statbegsys to addSym */ block(addSym, 0, 1); if ( sy != period ) error(22); emit(31); if ( btab[2].vsize > stacksize ) error(49); if ( printt ) printtables(); }//end compile int testsym(symset s1, int sym){ int i; //if ( lnfeedback == 1 ) printf("\nENTERING testsym - 7\n"); for(i=0; i < 52; i++)if(s1[i] == sym) return 1; return 0; }// end testsym int testtyp(typset s1, int sym){ int i; //if ( lnfeedback == 1 ) printf("\nENTERING testtyp - 8\n"); for(i=0; i < 8; i++)if(s1[i] == sym) return 1; return 0; }// end testsym int findindex(symset s1){ int i; //if ( lnfeedback == 1 ) printf("\nENTERING findindex - 9\n"); for(i = 1; i < 52; i++)if(s1[i]==0) return i; return -1; }// end findindex void skip(symset fsys, int n){ int test = 0; //if ( lnfeedback == 1 ) printf("\nENTERING skip - 11\n"); error(n); while(!testsym(fsys, sy)) insymbol(); }// end skip void test(symset s1, symset s2, int n){ int i, j; symset addsym; //if ( lnfeedback == 1 ) printf("\nENTERING test - 12\n"); //printf("sy = %d\n",sy); for(i = 1; i < 52; i++)addsym[i] = s1[i]; if(!testsym(addsym, sy)){ for(i=1; i < 52; i++)if(addsym == 0) break; for(j=1; s2[j] != 0; j++){ if(!testsym(addsym, s2[j])){ addsym[i] = s2[j]; i++; } } skip(addsym, n); } }// end test void testsemicolon(symset fsys){ symset addsym; int i; //if ( lnfeedback == 1 ) printf("\nENTERING testsemicolon - 13\n"); if(sy == semicolon) insymbol(); else{ error(14); if((sy == comma) || (sy == colon)) insymbol(); } for(i = 1; i < 52; i++)addsym[i] = blockbegsys[i]; if(!testsym(addsym, ident))addsym[findindex(addsym)] = ident; test(addsym, fsys, 6); }// end testsemicolon void emit(int fct){ //if ( lnfeedback == 1 ) printf("\nENTERING emit - 5\n"); if(lc == cmax) fatal(6); code[lc].f = fct; if(feedbacklevel == 2 || feedbacklevel == 3) printf("\t\t emit(%s)\n", mnemonics[fct]); lc = lc +1; }// end emit void emit1( int fct, int b){ //if ( lnfeedback == 1 ) printf("\nENTERING emit1 - 6\n"); if(lc == cmax) fatal(6); code[lc].f = fct; code[lc].y = b; if(feedbacklevel == 2 || feedbacklevel == 3) printf("\t\t emit1(%s, %d)\n", mnemonics[fct] , b); lc = lc + 1; }// end emit1 void emit2(int fct, int a, int b){ if(lc == cmax) fatal(6); code[lc].f = fct; code[lc].x = a; code[lc].y = b; if(feedbacklevel == 2 || feedbacklevel == 3) printf("\t\t emit2(%s, %d, %d)\n", mnemonics[ fct], a, b); lc = lc + 1; }// end emit2 int finderrindex(){ int it; for(it = 0; it<=ermax; it++){ if(errs[it] == -1)return it; } }// end finderrindex void error(int n){ int i; if(errpos == 0) printf("\n ****"); if(cc > errpos){ for(i=0; i < (cc-errpos); i++)printf(" "); printf("^%2d",n); errpos = cc + 3; errs[finderrindex()] = n; } }// end error void fatal(int n){ alfa msg[8]; printf("\n"); errormsg(); strcpy(msg[ 1], "identifier"); strcpy(msg[ 2], "procedures"); strcpy(msg[ 3], "reals "); strcpy(msg[ 4], "arrays "); strcpy(msg[ 5], "levels "); strcpy(msg[ 6], "code "); strcpy(msg[ 7], "strings "); printf(" compiler table for %s is too small\n", msg[n]); exit(1);// terminate compilation }// end fatal void selector(symset fsys, item *v, int level){ item x; int a, it, j; symset addsym, endtest; if ( lnfeedback == 1 ) printf("\n\t\t\t\t\t\tENTERING selector - 27\n"); for(it=0; it<52 ; it++) endtest[it] = 0; endtest[1] = lbrack; endtest[2] = lparent; endtest[3] = period; do{// sy in [lparent, lbrack, period] if(sy == period){ insymbol();// field selector if(sy != ident) error(2); else{ if(v->typ != records) error(31); else{// search field identifier j = btab[v->ref].last; strcpy(tab[0].name, id); while(strcmp(tab[j].name, id))j = tab[j].link; if(!j) { printf("in Selector\n"); error(0);} v->typ = tab[j].typ; v->ref = tab[j].ref; a = tab[j].adr; if (a) emit1(9,a); } insymbol(); } } else{// array selector if(sy != lbrack) error(11); do{ insymbol(); for(it=0; it<52 ; it++)addsym[it] = fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, rbrack)) addsym[findindex(addsym)] = rbrack; expression(addsym, &x, level); if(v->typ != arrays) error(28); else{ a = v->ref; if(atab[a].inxtyp != x.typ) error(26); else if(atab[a].elsize == 1) emit1(20, a); else emit1(21, a); v->typ = atab[a].eltyp; v->ref = atab[a].elref; } }while(sy == comma); if (sy == rbrack) insymbol(); else{ error(12); if(sy == rparent) insymbol(); } } }while(testsym(endtest, sy)); for(it=0; it<52 ; it++) addsym[it] = 0; test(fsys, addsym, 6); }// end selector void call(symset fsys, int i, int level){ item x; int lastp, cp, k, it; symset addsym; if ( lnfeedback == 1 ) printf("\n\t\t\t\t\t\tENTERING call 28\n"); emit1(18, i); // mark the stack lastp = btab[tab[i].ref].lastpar; cp = i; if(sy == lparent){// actual paramater list do{ insymbol(); if(cp >= lastp) error(39); else{cp = cp + 1; if(tab[cp].normal){// value paramaters for(it=0 ; it<52 ; it++) addsym[it]=fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, colon)) addsym[findindex(addsym)] = colon; if(!testsym(addsym, rparent)) addsym[findindex(addsym)] = rparent; expression(addsym, &x, level); if(x.typ == tab[cp].typ){ if(x.ref != tab[cp].ref) error(36); else if (x.typ == arrays) emit1(22, atab[x.ref].size); else if (x.typ == records) emit1(22, btab[x.ref].vsize); } else if ((x.typ == ints) && (tab[cp].typ == reals)) emit1(26,0); else if(x.typ != notyp) error(36); } else{// variable parameter if(sy != ident) error(2); else{ k = loc(id, level); insymbol(); if(k){ if(tab[k].obj != variable) error(37); x.typ = tab[k].typ; x.ref = tab[k].ref; if(tab[k].normal) emit2(0, tab[k].lev, tab[k].adr); else emit2(1, tab[k].lev, tab[k].adr); for(it=0 ; it<52 ; it++)addsym[it] = 0; addsym[1] = lbrack; addsym[2] = lparent; addsym[3] = period; if(testsym(addsym, sy)){ for(it=0; it<52 ; it++)addsym[it] = fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, colon)) addsym[findindex(addsym)] = colon; if(!testsym(addsym, rparent)) addsym[findindex(addsym)] = rparent; selector(addsym, &x, level); } if((x.typ != tab[cp].typ) || (x.ref != tab[cp].ref)) error(36); } } } } for(it=0; it<52 ; it++) addsym[it] = 0; addsym[1] = comma; addsym[2] = rparent; test(addsym, fsys, 6); }while(sy == comma); if(sy == rparent) insymbol(); else error(4); } if(cp < lastp) error(39); // not enough paramaters emit1(19, btab[tab[i].ref].psize-1); if(tab[i].lev < level) emit2(3, tab[i].lev, level); }// end call void statement(symset fsys, int level){ int i, it; symset addsym, addsym2; if ( lnfeedback == 1 ) printf("\n\t\t\t\t\tENTERING statement - 25\n"); void assignment(int lv, int ad){ item x, y; int f, it; symset addsym; symset addsym2; if ( lnfeedback == 1 ) printf("\n\t\t\t\t\tENTERING assignment - 35\n"); x.typ = tab[i].typ; x.ref = tab[i].ref; if(tab[i].normal) f = 0; else f = 1; emit2(f, lv, ad); /*emit no exact number*/ for(it=0; it<52 ; it++)addsym[it] = 0; addsym[1] = lbrack; addsym[2] = lparent; addsym[3] = period; for(it=0; it<52 ; it++)addsym2[it] = fsys[it]; if(!testsym(addsym2, becomes)) addsym2[findindex(addsym2)] = becomes; if(!testsym(addsym2, egl)) addsym2[findindex(addsym2)] = egl; if(testsym(addsym, sy)) selector(addsym2, &x, level); if(sy == becomes) insymbol(); else { error(51); if(sy == egl) insymbol(); } expression(fsys, &y, level); if(x.typ == y.typ) if(testtyp(stantyps, x.typ)) emit(38); else if(x.ref != y.ref)error(46); else if(x.typ == arrays) emit1(23, atab[x.ref].size); else emit1(23, btab[x.ref].vsize); else if((x.typ == reals) && (y.typ == ints)){ emit1(26,0); emit(38); } else if((x.typ != notyp) && (y.typ != notyp)) error(46); }// end assignment void compoundstatement(){ symset addsym; symset addsym2; int it; if ( lnfeedback == 1 ) printf("\n\t\t\t\t\tENTERING compoundstatement - 36\n"); insymbol(); for(it=0; it<52 ;it++) addsym[it] =fsys[it]; if(!testsym(addsym, semicolon)) addsym[findindex(addsym)] = semicolon; if(!testsym(addsym, endsy)) addsym[findindex(addsym)] = endsy; for(it=0; it<52 ; it++) addsym2[it] =statbegsys[it]; if(!testsym(addsym2, semicolon)) addsym2[findindex(addsym2)] = semicolon; statement(addsym, level); while(testsym(addsym2, sy)){ if(sy == semicolon) insymbol(); else error(14); statement(addsym, level); } if(sy == endsy) insymbol(); else error(57); }// end compoundstatement void ifstatement(){ item x; symset addsym; typset addtyp; int it, lc1, lc2; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING ifstatement - 37\n"); insymbol(); for(it=0; it<52 ; it++) addsym[it] = fsys[it]; if(!testsym(addsym, thensy)) addsym[findindex(addsym)] = thensy; if(!testsym(addsym, dosy)) addsym[findindex(addsym)] = dosy; expression(addsym, &x, level); for(it=0; it<8 ; it++) addtyp[it] = 0; addtyp[1] = bools; addtyp[2] = notyp; if(!testtyp(addtyp, x.typ)) error(17); lc1 = lc; emit(11); // jmpc if(sy == thensy) insymbol(); else{ error(52); if(sy == dosy) insymbol(); } for(it=0; it<52 ; it++) addsym[it] = fsys[it]; if(!testsym(addsym, elsesy)) addsym[findindex(addsym)] = elsesy; statement(addsym, level); if(sy == elsesy){ insymbol(); lc2 = lc; emit(10); code[lc1].y = lc; statement(fsys, level); code[lc2].y = lc; } else code[lc1].y = lc; }// end ifstatement void casestatement(){ item x; int i, j, k, lc1, it; symset addsym; typset addtyp; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING casestatement - 37\n"); casetabrecord casetab[1001];// 1001 = csmax + 1 int exittab[1001];// 1001 = csmax +1 void caselabel(){ conrec lab; int k, it; symset addsym; for(it = 0; it<52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, colon)) addsym[findindex(addsym)] = colon; constant(addsym, &lab, level); if(lab.tp != x.typ) error(47); else if(i == csmax) fatal(6); else{ i = i+1; k = 0; casetab[i].val = lab.i; casetab[i].lc = lc; do{ k = k+1; }while(!(casetab[k].val == lab.i)); if(k < i) error(1); // multipule definitions } }//end caselabel void onecase(){ symset addsym; int it; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING onecase - 38\n"); if(testsym(constbegsys, sy)){ caselabel(); while(sy == comma){ insymbol(); caselabel(); } if(sy == colon) insymbol(); else error(5); for(it=0 ; it<52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, colon)) addsym[findindex(addsym)] = colon; statement(addsym, level); j = j+1; exittab[j] = lc; emit(10); } } // end onecase // begin casestatement insymbol(); i = 0; j = 0; for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, ofsy)) addsym[findindex(addsym)] = ofsy; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, colon)) addsym[findindex(addsym)] = colon; for(it = 0; it < 8; it++) addtyp[it] = 0; addtyp[1] = ints; addtyp[2] = bools; addtyp[3] = chars; addtyp[4] = notyp; expression(addsym, &x, level); if(!testtyp(addtyp, x.typ)) error(32); lc1 = lc; emit(12); // jmpx if(sy == ofsy) insymbol(); else error(8); onecase(); while(sy == semicolon){ insymbol(); onecase(); } code[lc1].y = lc; for(k =1; k <= j; k++){ emit1(13, casetab[k].val); emit1(13, casetab[k].lc); } emit1(10, 0); for(k = 1; k <= j; k++) code[exittab[k]].y = lc; if(sy == endsy) insymbol(); else error(57); }// end casestatement void repeatstatement(){ item x; int lc1, it; symset addsym; symset addsym2; typset addtyp; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING repeatstatement - 39\n"); lc1 = lc; insymbol(); for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, semicolon)) addsym[findindex(addsym)] = semicolon; if(!testsym(addsym, untilsy)) addsym[findindex(addsym)] = untilsy; statement(addsym, level); for(it = 0; it < 52; it++) addsym2[it] = statbegsys[it]; if(!testsym(addsym2, semicolon)) addsym2[findindex(addsym2)] = semicolon; while(testsym(addsym2, sy)){ if(sy == semicolon) insymbol(); else error(14); statement(addsym, level); } if(sy == untilsy){ insymbol(); expression(fsys, &x, level); for(it = 0; it < 8; it++) addtyp[it] = 0; addtyp[1] = bools; addtyp[2] = notyp; if(!testtyp(addtyp, x.typ)) error(17); emit1(11, lc1); } }// end repeatstatement void whilestatement(){ item x; int lc1, lc2, it; symset addsym; typset addtyp; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING whilestatement - 40\n"); insymbol(); lc1 = lc; for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym( addsym, dosy)) addsym[findindex(addsym)] = dosy; expression(addsym, &x, level); for(it = 0; it < 8; it++) addtyp[it] = 0; addtyp[1] = bools; addtyp[2] = notyp; if(!testtyp(addtyp, x.typ)) error(17); lc2 = lc; emit(11); if(sy == dosy) insymbol(); else error(54); statement(fsys, level); emit1(10, lc1); code[lc2].y = lc; }// end whilestatement void forstatement(){ int cvt; item x; int i, f, lc1, lc2, it; symset addsym; typset addtyp; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING forstatement - 41\n"); insymbol(); if(sy == ident){ i = loc(id, level); insymbol(); if(!i) cvt = ints; else if(tab[i].obj == variable){ cvt = tab[i].typ; emit2(0, tab[i].lev, tab[i].adr); for(it = 0; it<8; it++)addtyp[it] = 0; addtyp[1] = notyp; addtyp[2] = ints; addtyp[3] = bools; addtyp[4] = chars; if(!testtyp(addtyp, cvt))error(18); } else { error(37); cvt = ints; } } else { for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, becomes)) addsym[findindex(addsym)] = becomes; if(!testsym(addsym, tosy)) addsym[findindex(addsym)] = tosy; if(!testsym(addsym, downtosy)) addsym[findindex(addsym)] = downtosy; if(!testsym(addsym, dosy)) addsym[findindex(addsym)] = dosy; skip(addsym, 2); } if(sy == becomes){ insymbol(); for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, tosy)) addsym[findindex(addsym)] = tosy; if(!testsym(addsym, downtosy)) addsym[findindex(addsym)] = downtosy; if(!testsym(addsym, dosy)) addsym[findindex(addsym)] = dosy; expression(addsym, &x, level); if(x.typ != cvt) error(19); } else skip(addsym, 51); f = 14; if((sy == tosy) || (sy == downtosy)){ if(sy == downtosy) f = 16; insymbol(); for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, dosy)) addsym[findindex(addsym)] = dosy; expression(addsym, &x, level); if(x.typ != cvt) error(19); } else skip(addsym, 55); lc1 = lc; emit(f); /*emit no exact number*/ if(sy == dosy) insymbol(); else error(54); lc2 = lc; statement(fsys, level); emit1(f+1, lc2); /*emit no exact number*/ code[lc1].y = lc; }// end forstatement void standproc(int n){ int i, f, it; item x, y; symset addsym; if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING standproc - 42\n"); switch ( n ){ case 1: case 2: if ( !iflag ){ /*read*/ error(20); iflag = 1; } if ( sy == lparent ){ do{ insymbol(); if ( sy != ident ) error(2); else{ i = loc(id, level); insymbol(); if (i) if ( tab[i].obj != variable ) error(37); else{ x.typ = tab[i].typ; x.ref = tab[i].ref; if ( tab[i].normal ) f = 0; else f = 1; emit2(f, tab[i].lev, tab[i].adr); /*emit no exact number*/ if ( sy == lbrack || sy == lparent || sy == period ){ for(it = 0; it < 52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym(addsym, rparent)) addsym[findindex(addsym)] = rparent; selector(addsym, &x, level); } if ( x.typ == ints || x.typ == reals || x.typ == chars || x.typ == notyp ) emit1(27, x.typ); else error(40); } } for(it = 0; it < 52; it++) addsym[it] =0; addsym[1] = comma; addsym[2] = rparent; test(addsym, fsys, 6); }while ( sy == comma ); if ( sy == rparent ) insymbol(); else error(4); } if ( n == 2 ) emit(62); break; case 3: case 4: if ( sy == lparent ){ /*write*/ do{ insymbol(); if ( sy == stringt ){ emit1(24, sleng); emit1(28, inum); insymbol(); } else{ for(it = 0; it <52 ; it++) addsym[it] = fsys[it]; if(!testsym( addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym( addsym, colon)) addsym[findindex(addsym)] = colon; if(!testsym( addsym, rparent)) addsym[findindex(addsym)] = rparent; expression(addsym, &x, level); if (!( x.typ == ints || x.typ == reals || x.typ == chars || x.typ == notyp )) error(41); if ( sy == colon ){ insymbol(); expression(addsym, &y, level); if ( y.typ != ints ) error(43); if ( sy == colon ){ if ( x.typ != reals ) error(42); insymbol(); for(it = 0; it <52 ; it++) addsym[it] = fsys[it]; if(!testsym( addsym, comma)) addsym[findindex(addsym)] = comma; if(!testsym( addsym, rparent)) addsym[findindex(addsym)] = rparent;; expression(addsym, &y,level); if ( y.typ != ints ) error(43); emit(37); } else emit1(30, x.typ); } else emit1(29, x.typ); } }while( sy == comma ); if ( sy == rparent ) insymbol(); else error(4); } if ( n == 4 ) emit(63); break; }/*end switch*/ }/*end standproc*/ // begin statement for(it =0; it<52; it++) addsym[it] = statbegsys[it]; if(!testsym(addsym, ident)) addsym[findindex(addsym)] = ident; switch(sy){ case 37: // ident i = loc(id, level); insymbol(); if(i) switch(tab[i].obj){ case 0: //konstant case 2: //typel error(45);break; case 1: //variable assignment(tab[i].lev, tab[i].adr);break; case 3: //procedure if(tab[i].lev) call(fsys, i, level); else standproc(tab[i].adr);break; case 4: //function if(tab[i].ref == display[level]) assignment(tab[i].lev+1, 0); else error(45);break; } break; case 38: // beginsy compoundstatement();break; case 39: //ifsy ifstatement();break; case 40: //casesy casestatement();break; case 42: //whilesy whilestatement();break; case 41: //repeatsy repeatstatement();break; case 43: //forsy forstatement();break; } for(it=0; it<52; it++)addsym[it] = 0; test(fsys, addsym, 14); }// end statement void block(symset fsys, int isfun, int level){ int dx; // data allocation index int prt; // t-index of procedure int prb; // b-index of procedure int x, it; symset addsym, addsym2; // begin block if ( lnfeedback == 1 ) printf("\n\t\t\t\tENTERING block - 10\n"); dx = 5; prt = t; if(level > lmax) fatal(5); for(it=0; it<52;it++) addsym[it] = 0; addsym[1] = lparent; addsym[2] = colon; addsym[3] = semicolon; test(addsym, fsys, 7); enterblock(); display[level] = b; prb = b; tab[prt].typ = notyp; tab[prt].ref = prb; if(sy == lparent) parameterlist(level, fsys, &dx); btab[prb].lastpar = t; btab[prb].psize = dx; if(isfun) if(sy == colon){ insymbol();// return type if(sy == ident){ x = loc(id,level); insymbol(); if(x) if(tab[x].obj != typel) error(29); else if(testtyp(stantyps, tab[x].typ)) tab[prt].typ = tab[x].typ; else error(15); } else{ for(it=0; it<52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, semicolon)) addsym[findindex(addsym)] = semicolon; skip(addsym, 2); } } else error(5); if(sy == semicolon) insymbol(); else error(14); do{ if(sy == constsy) constantdeclaration(level, fsys); if(sy == typesy) typedeclaration(level, fsys); if(sy == varsy) variabledeclaration(level, fsys, &dx); btab[prb].vsize = dx; while((sy == proceduresy) || (sy == functionsy)) procdeclaration(level, fsys); for(it=0; it<52; it++){ addsym[it] = blockbegsys[it]; addsym2[it] = 0; } for(it=0; it<52; it++) if(!testsym(addsym, statbegsys[it])) addsym[findindex(addsym)] = statbegsys[it]; addsym2[1] = beginsy; test(addsym2, addsym, 56); }while(!testsym(statbegsys, sy)); tab[prt].adr = lc; insymbol(); for(it=0; it<52; it++) addsym[it] = fsys[it]; if(!testsym(addsym, semicolon)) addsym[findindex(addsym)] = semicolon; if(!testsym(addsym, endsy)) addsym[findindex(addsym)] = endsy; statement(addsym, level); for(it=0; it<52; it++) addsym[it] = statbegsys[it]; if(!testsym(addsym, semicolon)) addsym[findindex(addsym)] = semicolon; while(testsym(addsym, sy)){ if(sy == semicolon) insymbol(); else error(14); for(it=0; it<52; it++)addsym2[it] = fsys[it]; if(!testsym(addsym2, semicolon)) addsym2[findindex(addsym2)] = semicolon; if(!testsym(addsym2, endsy)) addsym2[findindex(addsym2)] = endsy; statement(addsym2, level); } if(sy == endsy) insymbol(); else error(57); for(it=0; it<52; it++)addsym[it] = 0; for(it=0; it<52; it++)addsym2[it] = fsys[it]; if(!testsym(addsym2, period)) addsym2[findindex(addsym2)] = period; test(addsym2, addsym, 6); }// end block