1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-28 19:29:39 +00:00
C02/src/include.c

246 lines
5.7 KiB
C
Raw Normal View History

2018-02-13 23:16:23 +00:00
/*************************************
* C02 Include File Parsing Routines *
*************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "common.h"
#include "files.h"
#include "asm.h"
#include "parse.h"
#include "label.h"
#include "vars.h"
#include "stmnt.h"
#include "dclrtn.h"
#include "include.h"
/* Read next include file name from Source File *
* Sets: incnam - the include file name */
2018-03-04 03:32:39 +00:00
void pincnm(void) {
2018-02-13 23:16:23 +00:00
char dlmtr;
int inclen = 0;
skpspc();
dlmtr = getnxt();
if (dlmtr == '<') {
strcpy(incnam, incdir);
inclen = strlen(incnam);
dlmtr = '>';
}
else if (dlmtr != '"')
ERROR("Unexpected character '%c' after include\n", dlmtr, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
while (!match(dlmtr))
{
incnam[inclen++] = nxtchr;
skpchr();
}
skpchr(); //skip end dlmtr
incnam[inclen] = 0;
}
/* Process assembly language include file */
2018-03-04 03:32:39 +00:00
void incasm(void) {
2018-02-13 23:16:23 +00:00
opninc();
setcmt("======== Assembler File ");
addcmt(incnam);
addcmt(" =======");
cmtlin();
while (fgets(line, sizeof line, incfil) != NULL) {
DEBUG("Writing line: %s", line)
2018-02-13 23:16:23 +00:00
fputs(line, outfil);
}
setcmt("==========================================");
cmtlin();
clsinc();
}
/* Process define directive */
2018-03-04 03:32:39 +00:00
void pdefin(void) {
2018-03-07 17:10:23 +00:00
ERROR("Define directive not implemented\n", 0, EXIT_FAILURE)
2018-02-27 04:21:01 +00:00
}
2018-02-13 23:16:23 +00:00
/* Parse ASCII Subdirective */
2018-03-04 03:32:39 +00:00
void pascii(void) {
2018-02-13 23:16:23 +00:00
getwrd(); //Get Pragma Subdirective
2018-02-15 04:40:57 +00:00
if (wordis("INVERT"))
2018-02-13 23:16:23 +00:00
invasc = TRUE;
if (wordis("HIGH"))
2018-02-15 04:40:57 +00:00
mskasc = TRUE;
else
ERROR("Unrecognized option '%s'\n", word, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
/* Parse Origin Subdirective */
2018-03-04 03:32:39 +00:00
void porign(void) {
2018-02-13 23:16:23 +00:00
prsnum(0xFFFF); //Get Origin Address
asmlin(ORGOP, value); //Emit Origin Instruction
DEBUG("Set origin to %s\n", value)
2018-02-13 23:16:23 +00:00
}
2018-07-24 04:30:00 +00:00
/* Parse Padding Subdirective */
void ppddng(void) {
padcnt = prsnum(0xFF); //Get Number of Padding Bytes
DEBUG("Set padding to %d\n", padcnt)
}
2018-02-13 23:16:23 +00:00
/* Parse Zeropage Subdirective */
2018-03-04 03:32:39 +00:00
void prszpg(void) {
2018-03-07 16:38:22 +00:00
zpaddr = prsnum(0xFF); //Set Zero Page Address to Literal
DEBUG("Set zero page address to %d\n", zpaddr)
2018-02-13 23:16:23 +00:00
}
/* Process Vartable Subdirective */
2018-03-04 03:32:39 +00:00
void pvrtbl(void) {
if (vrwrtn)
ERROR("Variable table already written", 0, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
vartbl(); //Write Variable Table
}
/* Parse Pragma Directive */
2018-03-04 03:32:39 +00:00
void pprgma(void) {
2018-02-13 23:16:23 +00:00
getwrd(); //Get Pragma Subdirective
DEBUG("Parsing pragma directive '%s'\n", word)
2018-02-13 23:16:23 +00:00
if (wordis("ASCII"))
pascii(); //Parse Ascii
else if (wordis("ORIGIN"))
porign(); //Parse Origin
2018-07-24 04:30:00 +00:00
else if (wordis("PADDING"))
ppddng(); //Parse Origin
2018-02-13 23:16:23 +00:00
else if (wordis("VARTABLE"))
pvrtbl(); //Parse Vartable
else if (wordis("ZEROPAGE"))
prszpg(); //Parse Origin
else
ERROR("Illegal pragma subdirective '%s'\n", word, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
/* Process Include File Directive */
2018-03-04 03:32:39 +00:00
void pincdr(void) {
2018-02-13 23:16:23 +00:00
skpchr(); //skip '#'
getwrd(); //read directive into word
DEBUG("Processing include file directive '%s'\n", word)
2018-02-13 23:16:23 +00:00
if (wordis("DEFINE"))
pdefin();
2018-02-27 04:21:01 +00:00
else if (wordis("PRAGMA"))
2018-02-13 23:16:23 +00:00
pprgma();
else
ERROR("Unrecognized directive '%s'\n", word, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
/* Parse Header Word */
2018-03-04 03:32:39 +00:00
void phdwrd(void) {
2018-02-13 23:16:23 +00:00
getwrd();
if (!ptype(MTNONE))
ERROR("Unexpected word '%s' in header\n", word, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
/* Save Source File Information */
2018-03-04 03:32:39 +00:00
void savsrc(void) {
2018-02-13 23:16:23 +00:00
savchr = nxtchr;
savcol = curcol;
savlin = curlin;
}
/* Set Include File Information */
2018-03-04 03:32:39 +00:00
void setinc(void) {
2018-02-13 23:16:23 +00:00
curcol = 0;
curlin = 0;
inpfil = incfil;
strcpy(inpnam, incnam);
alcvar = FALSE;
}
2018-02-17 19:50:43 +00:00
/* Set Include File Name */
void setinm(char* filext) {
strcpy(incnam, incdir);
strcat(incnam, hdrnam);
strcat(incnam, filext);
}
/* Set Input to Source File */
2018-03-04 03:32:39 +00:00
void setsrc(void) {
2018-02-17 19:50:43 +00:00
inpfil = srcfil;
strcpy(inpnam, srcnam);
}
2018-02-13 23:16:23 +00:00
/* Restore Source File Pointer*/
2018-03-04 03:32:39 +00:00
void rstsrc(void) {
2018-02-13 23:16:23 +00:00
nxtchr = savchr;
nxtupc = toupper(nxtchr);
curcol = savcol;
curlin = savlin;
2018-02-17 19:50:43 +00:00
setsrc();
2018-02-13 23:16:23 +00:00
alcvar = TRUE;
}
/* Process header include file */
2018-03-04 03:32:39 +00:00
void inchdr(void) {
2018-02-13 23:16:23 +00:00
savsrc(); //Save Source File Information
opninc(); //Open Include File
setinc(); //Set Include File Information
skpchr();
while (TRUE)
{
skpspc();
if (match(EOF)) break;
DEBUG("Checking next character '%c'\n", nxtchr)
2018-02-13 23:16:23 +00:00
if (match('#'))
pincdr();
else if (match('/'))
skpcmt();
else if (isalph())
phdwrd();
else {
ERROR("Unexpected character '%c'\n", nxtchr, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
}
clsinc();
rstsrc();
nxtchr = savchr;
}
2018-02-17 19:50:43 +00:00
/* Process Header File specified on Command Line */
2018-03-04 03:32:39 +00:00
void phdrfl(void) {
2018-02-17 19:50:43 +00:00
if (hdrnam[0] == 0) return;
DEBUG("Processing Header '%s'\n", hdrnam)
2018-02-17 19:50:43 +00:00
setinm(".h02");
inchdr();
setinm(".a02");
incasm();
}
2018-02-13 23:16:23 +00:00
/* Process include file */
2018-03-04 03:32:39 +00:00
void pincfl(void) {
2018-02-13 23:16:23 +00:00
pincnm(); //Parse Include File Name
DEBUG("Processing include file '%s'\n", incnam)
2018-02-13 23:16:23 +00:00
char *dot = strrchr(incnam, '.'); //find extension
if (dot == NULL) {
ERROR("Invalid include file name '%sn", incnam, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
if (strcmp(dot, ".a02") == 0)
incasm();
if (strcmp(dot, ".asm") == 0)
incasm();
else if (strcmp(dot, ".h02") == 0) {
inchdr(); //Process Header File
strcpy(dot, ".a02");
incasm(); //Process Assembly File with Same Name
}
else {
ERROR("Unrecognized include file extension '%s'\n'", dot, EXIT_FAILURE)
2018-02-13 23:16:23 +00:00
}
}
2018-03-07 17:32:08 +00:00
/* Print Constant Table to Log File */
void logcon(void) {
2018-02-13 23:16:23 +00:00
int i;
2018-03-08 14:39:32 +00:00
fprintf(logfil, "\n%-10s %5s\n", "Constant", "Value");
for (i=0; i<concnt; i++) {
fprintf(logfil, "%-10s %5d\n", connam[i], conval[i]);
2018-02-13 23:16:23 +00:00
}
}