CC = gcc
CFLAGS = -O3 -Wall
-OBJ = main.o lex.o error.o parse.o
+OBJ = main.o lex.o error.o parse.o cpp.o
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
--- /dev/null
+#include <limits.h>
+#include "gmqcc.h"
+
+/*
+ * Returns the next token back to the caller
+ * which is what we parse for the preprocessor here.
+ */
+int cpp(struct lex_file *file) {
+ /* TODO ... */
+}
"Parsing Error:",
"Lexing Error:",
"Internal Error:",
- "Compilation Error:"
+ "Compilation Error:",
+ "Preprocessor Error:"
};
int error(int status, const char *msg, ...) {
#define LEX_CHRLIT 129
#define LEX_STRLIT 130
#define LEX_IDENT 131
-#define LEX_DO 132
-#define LEX_ELSE 133
-#define LEX_IF 134
-#define LEX_WHILE 135
-#define LEX_INCLUDE 136
-#define LEX_DEFINE 137
int lex_token(struct lex_file *);
void lex_reset(struct lex_file *);
int lex_debug(struct lex_file *);
int lex_close(struct lex_file *);
-struct lex_file *lex_open (const char *);
+struct lex_file *lex_open (FILE *);
/* errors */
#define ERROR_LEX (SHRT_MAX+0)
#define ERROR_PARSE (SHRT_MAX+1)
#define ERROR_INTERNAL (SHRT_MAX+2)
#define ERROR_COMPILER (SHRT_MAX+3)
+#define ERROR_PREPRO (SHRT_MAX+4)
int error(int, const char *, ...);
/* parse.c */
int parse(struct lex_file *);
+/* cpp.c */
+int cpp (struct lex_file *);
#endif
"string",
"float",
"vector",
- "entity"
+ "entity",
};
-struct lex_file *lex_open(const char *name) {
+struct lex_file *lex_open(FILE *fp) {
struct lex_file *lex = mem_a(sizeof(struct lex_file));
if (lex) {
- lex->file = fopen(name, "r");
+ lex->file = fp;
fseek(lex->file, 0, SEEK_END);
lex->length = ftell(lex->file);
lex->size = lex->length; /* copy, this is never changed */
lex_addch(ch, file);
while ((ch = lex_getch(file)) != '*') {
if (ch == EOF)
- return error(ERROR_LEX, "malformatted comment"," ");
+ return error(ERROR_LEX, "malformatted comment", " ");
else
lex_addch(ch, file);
}
/* valid identifier */
if (ch > 0 && (ch == '_' || isalpha(ch))) {
lex_clear(file);
- while (ch > 0 && (isalpha(ch) || isdigit(ch) || ch == '_')) {
+ while (ch > 0 && (isalpha(ch) || ch == '_')) {
lex_addch(ch, file);
ch = lex_getsource(file);
}
#include <stdlib.h>
#include <string.h>
#include <limits.h>
-#include <sys/stat.h>
#include "gmqcc.h"
int usage(const char *name) {
}
int main(int argc, char **argv) {
- struct stat chk;
const char *ofile = NULL;
const char *ifile = NULL;
int i;
printf("ifile: %s\n", ifile);
printf("ofile: %s\n", ofile);
- /* we check here for file existance, not in the lexer */
- if (stat(ifile, &chk) != 0)
- return error(ERROR_COMPILER, "source file `%s` not found\n", ifile);
+ /* Open file */
+ FILE *fp = fopen(ifile, "r");
+ if (!fp) {
+ fclose(fp);
+ return error(ERROR_COMPILER, "Source file: %s not found\n", ifile);
+ }
- struct lex_file *lex = lex_open(ifile);
+ struct lex_file *lex = lex_open(fp);
lex_debug(lex);
parse (lex);
lex_close(lex);
int parse(struct lex_file *file) {
int token = 0;
- while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
+ while ((token = lex_token(file)) != ERROR_LEX && \
+ token != ERROR_COMPILER && \
+ token != ERROR_INTERNAL && \
+ token != ERROR_PARSE && \
+ token != ERROR_PREPRO && file->length >= 0) {
switch (token) {
case TOKEN_IF:
token = lex_token(file);
if (token != '(')
error(ERROR_PARSE, "Expected `(` after if\n", "");
break;
+
+ /* TODO: Preprocessor */
+ case '#':
+ token = cpp(file);
}
}
lex_reset(file);