/************************************* * C02 Variable Management Routines * *************************************/ #include #include #include #include #include #include "common.h" #include "files.h" #include "asm.h" #include "parse.h" #include "label.h" #include "vars.h" /* Lookup variable name in variable table * * Returns index into varnam array * * FALSE if variable was not found */ int fndvar(char *name) { int i; DEBUG("Looking up variable '%s'\n", word); for (i=0; i -1) return TRUE; else return FALSE; } /* Check for variable * * Generates error if variable is undefined * * Args: alwreg - allow register name * * name - variable name */ void chksym(int alwreg, char *name) { if (strlen(name) == 1 && strchr("AXY", name[0])) { if (alwreg) return; else ERROR("Illegal reference to register %s\n", name, EXIT_FAILURE); } if (!symdef(name)) ERROR("Undeclared variable '%s' encountered\n", name, EXIT_FAILURE); } /* Parse Variable Name * * Parameters: alwary - Allow Array Reference * * Sets: vrname - operand for LDA/STA/LDY/STY */ void reqvar(int alwary) { prsvar(FALSE); if (!alwary) if (valtyp != VARIABLE) expctd("Variable"); } /* Check for Array specifier and get size * * Sets: value - array size (as string) * * "" if not an array */ void pvarsz() { DEBUG("Checking for array definition\n", 0); value[0] = 0; if (match('[')) { skpchr(); if (alcvar) { DEBUG("Parsing array size\n", 0); sprintf(value, "%d", prsnum(0xFF) + 1); } expect(']'); } if (!alcvar) strcpy(value, "*"); } /* Parse Data Constant */ void prsdtc() { dtype = DTBYTE; prscon(); } /* Parse Data Array */ void prsdta() { dtype = DTARRY; expect('{'); dlen = 0; while (TRUE) { prscon(); dattmp[dlen++] = cnstnt; if (!look(',')) break; } expect('}'); } /* Parse Data String */ void prsdts() { dtype = DTSTR; getstr(); strcpy(value, word); DEBUG("Parsed Data String '%s'\n", value); } /* Store variable data * * Uses: value - Data to store * * Sets: datvar[] - Variable Data * * datlen[] - Data Length */ void setdat() { int i; if (dtype == DTBYTE) { DEBUG("Setting variable data to '%d'\n", cnstnt); dlen = 1; datvar[dsize++] = cnstnt; } else if (dtype == DTARRY) { DEBUG("Setting variable data to array of length %d\n", dlen); for (i=0; i 0) asmlin("STA", prmtra); //Store First Parameter if (prmcnt > 1) asmlin("STY", prmtry); //Store Second Parameter if (prmcnt > 2) asmlin("STX", prmtrx); //Store Third Parameter endlbl[0] = 0; //Create Dummy End Label pshlbl(LTFUNC, endlbl); //and Push onto Stack bgnblk(TRUE); //Start Program Block } /* (Check For and) Parse Variable Declaration*/ void pdecl(int m, int t) { DEBUG("Processing variable declarations(s) of type %d\n", t); while(TRUE) { getwrd(); if (match('(')) { if (m != MTNONE) { ERROR("Illegal Modifier %d in Function Definion", m, EXIT_FAILURE); } addfnc(); //Add Function Call return; } addvar(m, t); if (!look(',')) break; } expect(';'); } /* Check for and Parse Type Keyword */ int ptype(int m) { int result = TRUE; if (wordis("VOID")) pdecl(m, VTVOID); //Parse 'void' declaration else if (wordis("CHAR")) pdecl(m, VTCHAR); //Parse 'char' declaration else result = FALSE; return result; } /* Check for and Parse Modifier */ int pmodfr() { DEBUG("Parsing modifier '%s'\n", word); int result = TRUE; if (wordis("ZEROPAGE")) { getwrd(); ptype(MTZP); } else result = FALSE; return result; } /* Write Variable Data */ void vardat(int i) { int j; DEBUG("Building Data for Variable '%s'\n", varnam[i]); value[0] = 0; for (j=0; j 0) { DEBUG("Allocating array '%s'\n", varnam[i]); asmlin(STROP, varsiz[i]); } else { DEBUG("Allocating variable '%s'\n", varnam[i]); asmlin(BYTEOP, "0"); } } vrwrtn = TRUE; } /* Print Variable Table to Log File */ void logvar() { int i; fprintf(logfil, "\n%-31s %s %s %s\n", "Variable", "Type", "Size", "Data"); for (i=0; i