Work in progress preprocessor

This commit is contained in:
Dale Weiler 2012-04-09 09:36:16 -04:00
parent c436c3a6c6
commit 1deeb8c9cf
8 changed files with 38 additions and 21 deletions

View file

@ -1,6 +1,6 @@
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)

10
cpp.c Normal file
View file

@ -0,0 +1,10 @@
#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 ... */
}

View file

@ -55,7 +55,8 @@ static const char *const error_list[] = {
"Parsing Error:",
"Lexing Error:",
"Internal Error:",
"Compilation Error:"
"Compilation Error:",
"Preprocessor Error:"
};
int error(int status, const char *msg, ...) {

BIN
gmqcc

Binary file not shown.

11
gmqcc.h
View file

@ -173,27 +173,24 @@ struct lex_file {
#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

10
lex.c
View file

@ -39,13 +39,13 @@ static const char *const lex_keywords[] = {
"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 */
@ -244,7 +244,7 @@ static int lex_skipcmt(struct lex_file *file) {
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);
}
@ -276,7 +276,7 @@ int lex_token(struct lex_file *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);
}

13
main.c
View file

@ -23,7 +23,6 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include "gmqcc.h"
int usage(const char *name) {
@ -32,7 +31,6 @@ int usage(const char *name) {
}
int main(int argc, char **argv) {
struct stat chk;
const char *ofile = NULL;
const char *ifile = NULL;
int i;
@ -58,11 +56,14 @@ int main(int argc, char **argv) {
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);

10
parse.c
View file

@ -25,7 +25,11 @@
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);
@ -35,6 +39,10 @@ int parse(struct lex_file *file) {
if (token != '(')
error(ERROR_PARSE, "Expected `(` after if\n", "");
break;
/* TODO: Preprocessor */
case '#':
token = cpp(file);
}
}
lex_reset(file);