/* Emit a relocated word at dot and advance dot */ void putv(word w, int rel) { if(pass2){ fprintf(lstfp, "%06o%c\t%06o%c%06o%c\t", right(dot->v.val), dot->v.rel ? '\'' : ' ', left(w), rel & 2 ? '\'' : ' ', right(w), rel & 1 ? '\'' : ' '); listline(); if(dot->v.rel != lastdot.rel || dot->v.val != lastdot.val+1){ startitem(Code); putword(dot->v.val, dot->v.rel); } putword(w, rel); }else{ // printf("\t%06o%c\t%06o%c%06o%c\n", // right(dot->v.val), dot->v.rel ? '\'' : ' ', // left(w), rel & 2 ? '\'' : ' ', // right(w), rel & 1 ? '\'' : ' '); } lastdot = dot->v; dot->v.val++; if(dot->v.rel){ if(dot->v.val > relbreak) relbreak = dot->v.val; }else{ if(dot->v.val > absbreak) absbreak = dot->v.val; } }
void listprog(program *pgm, int start, int end) { statement *tempstmt = pgm->firststmt; char *listout; do { if ((tempstmt->linenum >= start) && (tempstmt->linenum <= end)) { listout = SafeMalloc(1024*64); *listout = 0; listline(listout, tempstmt, 1); printf("%s\n", listout); GC_free(listout); } tempstmt = tempstmt->nextstmt; } while (tempstmt != NULL); }
void list_if(char *var, statement *stmt) { int x = 0, y = 0, z = 0, ctr = 0; statement *tmpstmt; tmpstmt = SafeMalloc(sizeof(statement)); stmtinit(tmpstmt); for (x=0; x<stmt->metapos; x++) if (stmt->metalist[x].operation == 0xE7) break; y = x; for (x=y; x<stmt->metapos; x++) { if (stmt->metalist[x].operation == 0xE7) ctr++; if (stmt->metalist[x].operation == 0xE2) ctr -= 2; if (ctr == 0) break; } z = x; simplifylist(var, stmt, 0, y); mysprintf(var, "%s THEN ", listpop()); ctr = 1; tmpstmt->opcode = stmt->metalist[y+1].operation; for (x=y+2; x<z; x++) { tmpstmt->metalist[ctr].operation = stmt->metalist[x].operation; if (stmt->metalist[x].intarg) tmpstmt->metalist[ctr].intarg = stmt->metalist[x].intarg; if (stmt->metalist[x].floatarg.mantisa.i) tmpstmt->metalist[ctr].floatarg = stmt->metalist[x].floatarg; if (stmt->metalist[x].shortarg) tmpstmt->metalist[ctr].shortarg = stmt->metalist[x].shortarg; if (stmt->metalist[x].stringarg) { if (stmt->metalist[x].operation == MNEMONICREF && !stmt->metalist[x].intarg) { tmpstmt->metalist[ctr].stringarg[0] = stmt->metalist[x].stringarg[0]; tmpstmt->metalist[ctr].stringarg[1] = stmt->metalist[x].stringarg[1]; } else { for (y=0; y<stmt->metalist[x].intarg; y++) tmpstmt->metalist[ctr].stringarg[y] = stmt->metalist[x].stringarg[y]; } } ctr++; } tmpstmt->metapos = ctr; listline(var, tmpstmt, 0); if (z != stmt->metapos) { stmtinit(tmpstmt); ctr = 0; mysprintf(var, " ELSE "); tmpstmt->opcode = stmt->metalist[z+1].operation; for (x=z+2; x<stmt->metapos; x++) { tmpstmt->metalist[ctr].operation = stmt->metalist[x].operation; if (stmt->metalist[x].intarg) tmpstmt->metalist[ctr].intarg = stmt->metalist[x].intarg; if (stmt->metalist[x].floatarg.mantisa.i) tmpstmt->metalist[ctr].floatarg = stmt->metalist[x].floatarg; if (stmt->metalist[x].shortarg) tmpstmt->metalist[ctr].shortarg = stmt->metalist[x].shortarg; if (stmt->metalist[x].stringarg) { if (stmt->metalist[x].operation == MNEMONICREF && !stmt->metalist[x].intarg) { tmpstmt->metalist[ctr].stringarg[0] = stmt->metalist[x].stringarg[0]; tmpstmt->metalist[ctr].stringarg[1] = stmt->metalist[x].stringarg[1]; } else { for (y=0; y<stmt->metalist[x].intarg; y++) tmpstmt->metalist[ctr].stringarg[y] = stmt->metalist[x].stringarg[y]; } } ctr++; } tmpstmt->metapos = ctr; listline(var, tmpstmt, 0); } }
char* getln(void) { int c; char *s; static char filen[MAXLINE]; if(pass2) for(;;){ /* if no words were produced, * list the original line here */ if(lastline == lineno-1){ fprintf(lstfp, "\t%14s\t", ""); listline(); } c = getc(tmpfp); if(c == 1){ /* filename */ s = filen; while(c = getc(tmpfp), c) *s++ = c; *s = '\0'; filename = filen; fprintf(lstfp, "\n\t%s\n\n", filename); lineno = 0; lastline = 0; }else if(c == 2){ /* line */ s = line; while(c = getc(tmpfp), c) *s++ = c; *s = '\0'; lineno++; strcpy(curline, line); return line; }else if(c == 3) /* EOF */ return nil; } s = line; lineno++; while(c = getc(infp), c != EOF){ if(c == 037){ /* line continuation */ lineno++; getc(infp); continue; } if(c == '\n') break; if(s < &line[MAXLINE]) *s++ = c & 0177; /* force 7 bit */ } if(s >= &line[MAXLINE]) err(0, "warning: line too long"); if(c == EOF) return nil; *s = '\0'; putc(2, tmpfp); fputs(line, tmpfp); putc(0, tmpfp); return line; }
// main entry into parser void encode_rpn() { statement *stmt, *tempstmt = gprog->firststmt; struct labelset *label; unsigned int x = 0, y = 0; int channum = 0, lastfnc = 0, lastvar = 0, ifcount = 0; byte display = 0, watchchannel = 0, special = 0; char *cont; symbol *sym; begin: stmt = SafeMalloc(sizeof(statement)); stmtinit(stmt); for (x=0; x<MAX_STMT_METAS; x++) { stmt->metalist[x].operation = 0; stmt->metalist[x].floatarg.mantisa.i = 0; stmt->metalist[x].floatarg.exp = 0; stmt->metalist[x].shortarg = 0; for (y=0; y<MAX_STRING_LENGTH; y++) stmt->metalist[x].stringarg[y] = 0; } special = 0; foundequals = 0; firstvar = 1; numlabels = 0; envinfo.stmttype = 0; tokenpos = 0; token_type = 1; lastopcode = 0; for (x=0; x<32; x++) { numargs[x] = 0; parenstack[x] = 0; } do { get_token(); tokenpos += strlen(token); if(!checkoption(get_cmdindex(get_opname(stmt->opcode)), token, token_type)) lineerror(stmt, __FILE__, __LINE__); // hack to avoid using runtime system // using runtime system would change the last executed line info. // if ((!strcmp(token, "PBSTEP")) && (token_type == TOK_COMMAND)) // dbg_step(); switch (token_type) { case TOK_ERROR: lineerror(stmt, __FILE__, __LINE__); break; case TOK_COMMAND: lastopcode = get_opcode(token); if (cmdtable[get_cmdindex(token)].options & IO_CHANNEL) watchchannel = 1; else watchchannel = 0; stmt->opcode = get_opcode(token); if (stmt->opcode == CMD_LET) goto loop2; else goto loop; case TOK_NUMBER: stmt->opcode = 0; stmt->linenum = atoi(token); break; default: buffermeta(stmt, token_type, token); goto loop2; } buffermeta(stmt, token_type, token); loop: get_token(); if (checkerr == 2) checkerr = 1; else if (checkerr == 1) { if (token[0] == '=') { if (stmt->metalist[stmt->metapos-2].operation == 0xEC) stmt->metapos-=2; else stmt->metapos--; push("ERR", TOK_OPERATOR); } else if (token[0] == '(') { prog--; stmt->metapos--; strcpy(token, "ERR"); token_type = TOK_FUNCTION; } checkerr = 0; } if (lastopcode == CMD_SETERR) { if (!strcmp(token, "ON")) lastopcode = CMD_SETERRON; if (!strcmp(token, "OFF")) lastopcode = CMD_SETERROFF; } if (!strcmp(token, "RECORD")) { if (stmt->opcode == CMD_READ) lastopcode = CMD_READRECORD; if (stmt->opcode == CMD_WRITE) lastopcode = CMD_WRITERECORD; if (stmt->opcode == CMD_EXTRACT) lastopcode = CMD_EXTRACTRECORD; if (stmt->opcode == CMD_FIND) lastopcode = CMD_FINDRECORD; if (stmt->opcode == CMD_PRINT) lastopcode = CMD_PRINTRECORD; token_type = lasttype; goto loop; } if (lastopcode == CMD_REM) { stmt->metalist[1].operation = 0xF5; tokenpos -= 2; if (input[tokenpos] == '\"') x = tokenpos; else x = tokenpos+2; stmt->metalist[1].shortarg = strlen(input) - x; y = 0; for (x=x; x<strlen(input); x++) { stmt->metalist[1].stringarg[y] = input[x]; y++; } stmt->metalist[1].stringarg[y] = '\0'; stmt->metapos++; if (stmt->linenum) insertstmt(gprog, stmt); return; } tokenpos += strlen(token); if(!checkoption(get_cmdindex(get_opname(lastopcode)), token, token_type)) lineerror(stmt, __FILE__, __LINE__); loop2: switch (token_type) { case TOK_SEMICOLON: popstack(stmt, -1); if (!stmt->linenum) { execline(gprog, stmt); stmtinit(stmt); } break; case TOK_COLON: if ((stmt->metalist[stmt->metapos-1].operation == SETVAL_NUMERIC) || (stmt->metalist[stmt->metapos-1].operation == GETVAL_NUMERIC) || (stmt->metalist[stmt->metapos-1].operation == LABELREF)) { label = SafeMalloc(sizeof(struct labelset)); sym = idx2sym(gprog, stmt->metalist[stmt->metapos-1].shortarg); label->labelnum = addlabel(gprog, sym->name, stmt->linenum); for (x=0; x<MAX_STRING_LENGTH; x++) sym->name[x] = '\0'; x = 0; stmt->metapos--; stmt->metalist[stmt->metapos].shortarg = 0; stmt->opcode = 0; stmt->labelset[stmt->numlabels] = label; stmt->numlabels++; firstvar = 1; } else { do { if (stmt->linenum == tempstmt->linenum) { for (x=0; x<strlen(input); x++) if (input[x] == ':') break; y = x; for (x=0; x<=y; x++) input[x] = ' '; cont = SafeMalloc(1024*64); *cont = 0; listline(cont, tempstmt, 1); strcat(cont, input); *prog = 0; prog = cont; display = 1; goto begin; } tempstmt = tempstmt->nextstmt; } while (tempstmt != NULL); numlabels++; } goto loop; case TOK_COMMA: if (parencount > 0) { // comma being delimiter for system functions popstack(stmt, -2); push("(", TOK_OPERATOR); if (chaninfo == 1) { if (!channum) { channum = 1; stmt->metalist[stmt->metapos].operation = 0xE1; stmt->metapos++; } } else { if (special == 1) numargs[parencount-1]+=10; else numargs[parencount-1]++; envinfo.stmttype = 0; } } else { // comma being delimiter for verbs popstack(stmt, -1); buffermeta(stmt, TOK_COMMA, token); for (x=0; x<32; x++) numargs[x] = 0; envinfo.stmttype = 0; foundequals = 0; firstvar = 1; } goto loop; case TOK_ERROR: lineerror(stmt, __FILE__, __LINE__); return; case TOK_COMMAND: lastopcode = get_opcode(token); if (cmdtable[get_cmdindex(token)].options & IO_CHANNEL) watchchannel = 1; else watchchannel = 0; if (!strcmp(token, "IF")) ifcount++; if (!stmt->opcode) { stmt->opcode = get_opcode(token); envinfo.stmttype = 0; goto loop; } else { popstack(stmt, -1); envinfo.stmttype = 0; if (ifcount > 0) { stmt->metalist[stmt->metapos].operation = 0xE7; stmt->metalist[stmt->metapos].shortarg = 0; stmt->metalist[stmt->metapos].intarg = 0; stmt->metapos++; stmt->length++; // watchchannel = 0; chaninfo = 0; channum = 0; ifcount--; } if (lastopcode == CMD_ON) { special = 1; if (!strcmp(token, "GOTO")) stmt->metalist[stmt->metapos].operation = 0x00F4; else stmt->metalist[stmt->metapos].operation = 0x01F4; stmt->length += 2; stmt->metapos++; } else buffermeta(stmt, TOK_COMMAND, token); goto loop; } break; case TOK_RESERVED: popstack(stmt, -1); envinfo.stmttype = 0; lineref = 0; if (!strcmp(token, "ELSE")) { stmt->metalist[stmt->metapos].operation = 0xE7; stmt->metalist[stmt->metapos].intarg = 0; stmt->metalist[stmt->metapos].shortarg = 0; stmt->metapos++; stmt->metalist[stmt->metapos].operation = 0xE2; stmt->metalist[stmt->metapos].intarg = 0; stmt->metalist[stmt->metapos].shortarg = 0; stmt->metapos++; stmt->length+=2; envinfo.stmttype = 0; chaninfo = 0; channum = 0; } goto loop; case TOK_DONE: if ((stmt->linenum) && (!stmt->opcode)) deletestmt(gprog,stmt->linenum); else { popstack(stmt, -1); if (stmt->opcode == CMD_LET && foundequals == 0) lineerror(stmt, __FILE__, __LINE__); if (parencount > 0 || numlabels < 0) lineerror(stmt, __FILE__, __LINE__); if (stmt->linenum) { if (!stmt->errorflag) insertstmt(gprog, stmt); } else if (stmt->opcode) { if (!stmt->errorflag) { execline(gprog, stmt); } } } if (display) { GC_realloc(cont, strlen(cont)); listprog(gprog, stmt->linenum, stmt->linenum); } return; case TOK_USERFUNCTION: if (lastopcode == CMD_DEFFN) addfunction(gprog, token, stmt->linenum, 1); else addfunction(gprog, token, stmt->linenum, 0); if (token[strlen(token)-1] == '$') parenstack[parencount] = 8; else parenstack[parencount] = 7; buffermeta(stmt, TOK_USERFUNCTION, token); break; case TOK_FUNCTION: lastfnc = get_fnc(token); numlabels++; case TOK_OPERATOR: if (token[0] == '[') { special = 1; cont = get_symname(lastvar); if (cont[strlen(cont)-1] == '$') parenstack[parencount] = 2; else parenstack[parencount] = 1; numlabels++; goto openparen; } else if (token[0] == ']') { numargs[parencount-1] += 9; popstack(stmt, -2); envinfo.stmttype = parenstack[parencount]; token_type = TOK_ARRAY; special = 0; goto loop; } else if (token[0] == '-') { // placeholder if ((lasttype == TOK_OPERATOR) || (lasttype == TOK_RESERVED)) token[0] = '_'; evalstack(stmt); goto loop; } else if (token[0] == '(') { openparen: if (lasttype == TOK_COMMAND) { if (watchchannel == 1) chaninfo = 1; } else if (lasttype == TOK_ARRAY) { envinfo.stmttype = 0; numargs[parencount]++; push(token, token_type); goto loop; } else if (lasttype == TOK_SETVAL) { push(get_symname(lastvar), TOK_SETVAL); stmt->metapos--; stmt->metalist[stmt->metapos].shortarg = 0; numlabels++; cont = get_symname(lastvar); if (cont[strlen(cont)-1] == '$') parenstack[parencount] = 2; else parenstack[parencount] = 1; } else if (lasttype == TOK_VARIABLE) { push(get_symname(lastvar), TOK_VARIABLE); numlabels++; stmt->metapos--; stmt->metalist[stmt->metapos].shortarg = 0; cont = get_symname(lastvar); if (cont[strlen(cont)-1] == '$') parenstack[parencount] = 2; else parenstack[parencount] = 1; } else if (lasttype == TOK_USERFUNCTION) { stmt->metapos--; stmt->metalist[stmt->metapos].operation = 0xF5; stmt->metapos++; stmt->length++; } else if (lasttype == TOK_FUNCTION) { if ((envinfo.stmttype != fnctable[lastfnc].returntype) && (envinfo.stmttype != 0)) lineerror(stmt, __FILE__, __LINE__); else parenstack[parencount] = fnctable[lastfnc].returntype; } else parenstack[parencount] = 1; envinfo.stmttype = 0; numargs[parencount] = 1; push(token, token_type); goto loop; } else if (token[0] == ')') { if (parencount == 0) lineerror(stmt, __FILE__, __LINE__); popstack(stmt, -2); if (parenstack[parencount] == 7) { stmt->metalist[stmt->metapos].operation = 0xF8; stmt->metapos++; stmt->length++; envinfo.stmttype = 1; } else if (parenstack[parencount] == 8) { stmt->metalist[stmt->metapos].operation = 0xF8; stmt->metapos++; stmt->length++; envinfo.stmttype = 2; } else envinfo.stmttype = parenstack[parencount]; if (lastopcode == CMD_DEFFN && foundequals == 0) { stmt->metapos--; stmt->metalist[stmt->metapos].operation = 0xF8; stmt->metapos++; } if (chaninfo == 1) { chaninfo = 0; if (!channum) { stmt->metalist[stmt->metapos].operation = 0xE1; stmt->metapos++; } stmt->metalist[stmt->metapos].operation = 0xF4F1; stmt->metapos++; numargs[parencount] = 0; } goto loop; } else { evalstack(stmt); goto loop; } break; case TOK_VARIABLE: if (get_sysvar(token)) { buffermeta(stmt, TOK_SYSVAR, token); numlabels++; goto loop; } else { addsymbol(gprog, token); lastvar = get_symref(token); } case TOK_NUMBER: default: if(!stmt->opcode) { envinfo.stmttype = 0; lastopcode = stmt->opcode = CMD_LET; } if ((firstvar == 1) && (token_type == TOK_VARIABLE) && (lastopcode == CMD_LET || lastopcode == CMD_FOR || lastopcode == CMD_FOR || lastopcode == CMD_NEXT || lastopcode == CMD_DIM || lastopcode == CMD_INPUT)) { buffermeta(stmt, TOK_SETVAL, token); firstvar = 0; token_type = TOK_SETVAL; } else { numlabels++; buffermeta(stmt, token_type, token); } goto loop; } } while (1); }