diff src/pass1.c @ 21:3c0e5f311c95

Added reading of input file for pass1
author lost
date Fri, 02 Jan 2009 00:43:06 +0000
parents 05d4115b4860
children b704f7ffc8ba
line wrap: on
line diff
--- a/src/pass1.c	Fri Jan 02 00:42:11 2009 +0000
+++ b/src/pass1.c	Fri Jan 02 00:43:06 2009 +0000
@@ -24,27 +24,137 @@
 
 1. read all lines from the main source file, following all "include"
    directives as appropriate
-2. parse each line into a symbol, operation code, and operand as appropriate
-3. each operand is evaluated for syntax and futher for value if there are
+2. each operand is evaluated for syntax and futher for value if there are
    multiple addressing sizes available; any undefined or not fully resolved
    value will default to the largest addressing size available (16 bit)
-4. addresses are assigned to every symbol defined in the assembly
-5. macros are defined and expanded at this pass
+3. addresses are assigned to every symbol defined in the assembly
+4. macros are defined and expanded at this pass
+
+* note: the lines are re-evaluated on the second pass
+
+All source lines are read into memory with a record of the file name and
+line number within the files.
+
+Lines are one of the following formats:
 
+<symbol> <opcode> <operand> <comment>
+<symbol> <opcode> <comment>
+ <opcode> <operand> <comment>
+ <opcode> <comment>
 
+A "*" or ";" appearing anywhere on the line that is not otherwise interpreted
+as part of an operation code or operand introduces a comment.
+
+Certain lwasm specific operations are prefixed with a "*" to aid in source
+code portability (like *pragma).
 */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "lwasm.h"
+#include "util.h"
+
+// we can't use standard line inputting functions here because we have to
+// handle non-standard line terminations (CR, LF, CRLF, or LFCR)
+int lwasm_read_file(asmstate_t *as, const char *filename)
+{
+	FILE *f;
+	int c, c2;
+	lwasm_line_t *nl;
+	int lineno = 1;
+	char *fnref;
+	
+	// ought to be long enough...we truncate longer lines
+	char linebuff[2049];
+	int lbloc = 0;
+	int eol = 0;
+
+	// add filename to list
+	as -> filelist = lwasm_realloc(as -> filelist, sizeof(char *) * (as -> filelistlen + 1));
+	fnref = as -> filelist[as -> filelistlen] = lwasm_strdup(filename);
+	as -> filelistlen += 1;
+	
+	f = fopen(filename, "r");
+	if (!f)
+		return -1;
+
+	for (;;)
+	{
+		c = fgetc(f);
+		if (c == EOF)
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+		}
+		else if (c == '\r')
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+			// check for '\n':
+			c2 = fgetc(f);
+			if (c2 == EOF)
+				c = EOF;
+			else if (c2 != '\n')
+				ungetc(c2, f);
+		}
+		else if (c == '\n')
+		{
+			linebuff[lbloc] = '\0';
+			eol = 1;
+			// check for '\r':
+			c2 = fgetc(f);
+			if (c2 == EOF)
+				c = EOF;
+			else if (c2 != '\r')
+				ungetc(c2, f);
+		}
+		else
+		{
+			// silently ignore characters past 2K on a line... FIXME
+			if (lbloc < 2048)
+				linebuff[lbloc++] = c;
+		}
+		if (eol)
+		{
+			fprintf(stderr, "READ: %s\n", linebuff);
+			eol = 0;
+			lbloc = 0;
+			nl = lwasm_alloc(sizeof(lwasm_line_t));
+			nl -> text = lwasm_strdup(linebuff);
+			nl -> lineno = lineno++;
+			nl -> filename = fnref;
+			nl -> next = NULL;
+			nl -> prev = as -> linestail;
+			if (as -> linestail)
+				as -> linestail -> next = nl;
+			else
+				as -> linestail = nl;
+			if (!(as -> lineshead))
+				as -> lineshead = nl;
+		}
+		if (c == EOF)
+			break;
+	}
+	
+	fclose(f);
+	return 0;
+}
 
 void lwasm_pass1(asmstate_t *as)
 {
+	as -> passnum = 1;
+	
+	if (lwasm_read_file(as, as -> infile) < 0)
+	{
+		fprintf(stderr, "Error reading input file '%s'", as -> infile);
+		perror("");
+		exit(1);
+	}
+	
 }