remove now unused files

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-08-14 10:57:09 +02:00
parent 82d9651529
commit c08966a2a4
4 changed files with 27 additions and 760 deletions

354
lex.c
View file

@ -1,354 +0,0 @@
/*
* Copyright (C) 2012
* Dale Weiler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "gmqcc.h"
/*
* Keywords are multichar, punctuation lexing is a bit more complicated
* than keyword lexing.
*/
static const char *const lex_keywords[] = {
"do", "else", "if", "while",
"break", "continue", "return", "goto",
"for", "typedef"
};
void lex_init(const char *file, lex_file **set) {
lex_file *lex = mem_a(sizeof(lex_file));
if (!lex)
return;
lex->file = fopen(file, "r");
if (!lex->file) {
mem_d(lex);
return;
}
fseek(lex->file, 0, SEEK_END);
lex->length = ftell(lex->file);
lex->size = lex->length; /* copy, this is never changed */
fseek(lex->file, 0, SEEK_SET);
lex->last = 0;
lex->line = 1;
memset(lex->peek, 0, sizeof(lex->peek));
*set = lex;
}
void lex_close(lex_file *file) {
if (!file) return;
fclose(file->file); /* may already be closed */
mem_d (file);
}
static void lex_addch(int ch, lex_file *file) {
if (file->current < sizeof(file->lastok)-1)
file->lastok[file->current++] = (char)ch;
if (file->current == sizeof(file->lastok)-1)
file->lastok[file->current] = (char)'\0';
}
static GMQCC_INLINE void lex_clear(lex_file *file) {
file->current = 0;
}
/*
* read in inget/unget character from a lexer stream.
* This doesn't play with file streams, the lexer has
* it's own internal state for this.
*/
static int lex_inget(lex_file *file) {
char get;
file->length --;
if (file->last > 0) {
if ((get = file->peek[--file->last]) == '\n')
file->line ++;
return get;
}
if ((get = fgetc(file->file)) == '\n')
file->line++;
return get;
}
static void lex_unget(int ch, lex_file *file) {
if (file->last < sizeof(file->peek)) {
if (ch == '\n')
file->line --;
file->peek[file->last++] = ch;
}
file->length ++;
}
/*
* This is trigraph and digraph support, a feature not qc compiler
* supports. Moving up in this world!
*/
static int lex_trigraph(lex_file *file) {
int ch;
if ((ch = lex_inget(file)) != '?') {
lex_unget(ch, file);
return '?';
}
ch = lex_inget(file);
switch (ch) {
case '(' : return '[' ;
case ')' : return ']' ;
case '/' : return '\\';
case '\'': return '^' ;
case '<' : return '{' ;
case '>' : return '}' ;
case '!' : return '|' ;
case '-' : return '~' ;
case '=' : return '#' ;
default:
lex_unget('?', file);
lex_unget(ch , file);
}
return '?';
}
static int lex_digraph(lex_file *file, int first) {
int ch = lex_inget(file);
switch (first) {
case '<':
if (ch == '%') return '{';
if (ch == ':') return '[';
break;
case '%':
if (ch == '>') return '}';
if (ch == ':') return '#';
break;
case ':':
if (ch == '>') return ']';
break;
}
lex_unget(ch, file);
return first;
}
static int lex_getch(lex_file *file) {
int ch = lex_inget(file);
if (ch == '?')
return lex_trigraph(file);
if (ch == '<' || ch == ':' || ch == '%')
return lex_digraph(file, ch);
return ch;
}
static int lex_get(lex_file *file) {
int ch;
if (!isspace(ch = lex_getch(file)))
return ch;
/* skip over all spaces */
while (isspace(ch) && ch != '\n')
ch = lex_getch(file);
if (ch == '\n')
return ch;
lex_unget(ch, file);
return ' ';
}
static int lex_skipchr(lex_file *file) {
int ch;
int it;
lex_clear(file);
lex_addch('\'', file);
for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
lex_addch(ch, file);
if (ch == '\n')
return ERROR_LEX;
if (ch == '\\')
lex_addch(lex_getch(file), file);
}
lex_addch('\'', file);
lex_addch('\0', file);
if (it > 2)
return ERROR_LEX;
return LEX_CHRLIT;
}
static int lex_skipstr(lex_file *file) {
int ch;
lex_clear(file);
lex_addch('"', file);
while ((ch = lex_getch(file)) != '"') {
if (ch == '\n' || ch == EOF)
return ERROR_LEX;
lex_addch(ch, file);
if (ch == '\\')
lex_addch(lex_inget(file), file);
}
lex_addch('"', file);
lex_addch('\0', file);
return LEX_STRLIT;
}
static int lex_skipcmt(lex_file *file) {
int ch;
lex_clear(file);
ch = lex_getch(file);
if (ch == '/') {
lex_addch('/', file);
lex_addch('/', file);
while ((ch = lex_getch(file)) != '\n') {
if (ch == '\\') {
lex_addch(ch, file);
lex_addch(lex_getch(file), file);
} else {
lex_addch(ch, file);
}
}
lex_addch('\0', file);
return LEX_COMMENT;
}
if (ch != '*') {
lex_unget(ch, file);
return '/';
}
lex_addch('/', file);
/* hate this */
do {
lex_addch(ch, file);
while ((ch = lex_getch(file)) != '*') {
if (ch == EOF)
return error(file, ERROR_LEX, "malformatted comment");
else
lex_addch(ch, file);
}
lex_addch(ch, file);
} while ((ch = lex_getch(file)) != '/');
lex_addch('/', file);
lex_addch('\0', file);
return LEX_COMMENT;
}
static int lex_getsource(lex_file *file) {
int ch = lex_get(file);
/* skip char/string/comment */
switch (ch) {
case '\'': return lex_skipchr(file);
case '"': return lex_skipstr(file);
case '/': return lex_skipcmt(file);
default:
return ch;
}
}
int lex_token(lex_file *file) {
int ch = lex_getsource(file);
int it;
/* valid identifier */
if (ch > 0 && (ch == '_' || isalpha(ch))) {
lex_clear(file);
while (ch > 0 && (ch == '_' || isalpha(ch))) {
lex_addch(ch, file);
ch = lex_getsource(file);
}
lex_unget(ch, file);
lex_addch('\0', file);
/* look inside the table for a keyword .. */
for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
if (!strncmp(file->lastok, lex_keywords[it], strlen(lex_keywords[it])))
return it;
/* try a type? */
#define TEST_TYPE(X) \
do { \
if (!strncmp(X, "float", sizeof("float"))) \
return TOKEN_FLOAT; \
if (!strncmp(X, "vector", sizeof("vector"))) \
return TOKEN_VECTOR; \
if (!strncmp(X, "string", sizeof("string"))) \
return TOKEN_STRING; \
if (!strncmp(X, "entity", sizeof("entity"))) \
return TOKEN_ENTITY; \
if (!strncmp(X, "void" , sizeof("void"))) \
return TOKEN_VOID; \
} while(0)
TEST_TYPE(file->lastok);
/* try the hashtable for typedefs? */
if (typedef_find(file->lastok))
TEST_TYPE(typedef_find(file->lastok)->name);
#undef TEST_TYPE
return LEX_IDENT;
}
return (ch != ' ') ? ch : lex_token(file);
}
void lex_reset(lex_file *file) {
file->current = 0;
file->last = 0;
file->length = file->size;
fseek(file->file, 0, SEEK_SET);
memset(file->peek, 0, sizeof(file->peek ));
memset(file->lastok, 0, sizeof(file->lastok));
}
void lex_parse(lex_file *file) {
if (!file) return;
parse_gen(file); /* run parser */
}
/*
* Include a file into the lexer / parsing process: This really
* should check if names are the same to prevent endless include
* recrusion.
*/
lex_file *lex_include(lex_file *lex, const char *file) {
lex_file *set = NULL;
util_strrq(file);
if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
error(lex, ERROR_LEX, "Source file cannot include itself\n");
exit (-1);
}
lex_init(file, &set);
return set;
}

321
parse.c
View file

@ -1,321 +0,0 @@
/*
* Copyright (C) 2012
* Dale Weiler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "gmqcc.h"
/* compile-time constant for type constants */
typedef struct {
char *name;
int type;
float value[3];
char *string; /* string value if constant is string literal */
} constant;
VECTOR_MAKE(constant, compile_constants);
void compile_constant_debug() {
int iter = 0;
for(; iter < compile_constants_elements; iter++) {
constant *c = &compile_constants_data[iter];
switch(c->type) {
case TYPE_FLOAT: printf("constant: %s FLOAT %f\n", c->name, c->value[0]); break;
case TYPE_VECTOR: printf("constant: %s VECTOR {%f,%f,%f}\n",c->name, c->value[0], c->value[1], c->value[2]); break;
case TYPE_STRING: printf("constant: %s STRING %s\n", c->name, c->string); break;
case TYPE_VOID: printf("constant: %s VOID %s\n", c->name, c->string); break;
}
}
}
/*
* Generates a parse tree out of the lexees generated by the lexer. This
* is where the tree is built. This is where valid check is performed.
*/
int parse_gen(lex_file *file) {
int token = 0;
while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
switch (token) {
case TOKEN_TYPEDEF: {
char *f; /* from */
char *t; /* to */
token = lex_token(file);
token = lex_token(file); f = util_strdup(file->lastok);
token = lex_token(file);
token = lex_token(file); t = util_strdup(file->lastok);
typedef_add(file, f, t);
mem_d(f);
mem_d(t);
token = lex_token(file);
if (token == ' ')
token = lex_token(file);
if (token != ';')
error(file, ERROR_PARSE, "Expected a `;` at end of typedef statement");
token = lex_token(file);
break;
}
case TOKEN_VOID: goto fall;
case TOKEN_STRING: goto fall;
case TOKEN_VECTOR: goto fall;
case TOKEN_ENTITY: goto fall;
case TOKEN_FLOAT: goto fall;
{
fall:; {
char *name = NULL;
int type = token; /* story copy */
/* skip over space */
token = lex_token(file);
if (token == ' ')
token = lex_token(file);
/* save name */
name = util_strdup(file->lastok);
/* skip spaces */
token = lex_token(file);
if (token == ' ')
token = lex_token(file);
if (token == ';') {
/*
* Definitions go to the defs table, they don't have
* any sort of data with them yet.
*/
} else if (token == '=') {
token = lex_token(file);
if (token == ' ')
token = lex_token(file);
/* strings are in file->lastok */
switch (type) {
case TOKEN_VOID:
error(file, ERROR_PARSE, "Cannot assign value to type void\n");
/* TODO: Validate (end quote), strip quotes for constant add, name constant */
case TOKEN_STRING:
if (*file->lastok != '"')
error(file, ERROR_PARSE, "Expected a '\"' (quote) for string constant\n");
/* add the compile-time constant */
{
constant c;
c.name = util_strdup(name),
c.type = TYPE_STRING,
c.value[0] = 0;
c.value[1] = 0;
c.value[2] = 0;
c.string = util_strdup(file->lastok);
compile_constants_add(c);
}
break;
/* TODO: name constant, old qc vec literals, whitespace fixes, name constant */
case TOKEN_VECTOR: {
float compile_calc_x = 0;
float compile_calc_y = 0;
float compile_calc_z = 0;
int compile_calc_d = 0; /* dot? */
int compile_calc_s = 0; /* sign (-, +) */
char compile_data[1024];
char *compile_eval = compile_data;
if (token != '{')
error(file, ERROR_PARSE, "Expected initializer list {} for vector constant\n");
/*
* This parses a single vector element: x,y & z. This will handle all the
* complicated mechanics of a vector, and can be extended as well. This
* is a rather large macro, and is #undef'd after it's use below.
*/
#define PARSE_VEC_ELEMENT(NAME, BIT) \
token = lex_token(file); \
if (token == ' ') \
token = lex_token(file); \
if (token == '.') \
compile_calc_d = 1; \
if (!isdigit(token) && !compile_calc_d && token != '+' && token != '-') \
error(file, ERROR_PARSE,"Invalid constant initializer element %c for vector, must be numeric\n", NAME); \
if (token == '+') \
compile_calc_s = '+'; \
if (token == '-' && !compile_calc_s) \
compile_calc_s = '-'; \
while (isdigit(token) || token == '.' || token == '+' || token == '-') { \
*compile_eval++ = token; \
token = lex_token(file); \
if (token == '.' && compile_calc_d) { \
error(file, ERROR_PARSE, "Invalid constant initializer element %c for vector, must be numeric.\n", NAME); \
token = lex_token(file); \
} \
if ((token == '-' || token == '+') && compile_calc_s) { \
error(file, ERROR_PARSE, "Invalid constant initializer sign for vector element %c\n", NAME); \
token = lex_token(file); \
} \
else if (token == '.' && !compile_calc_d) \
compile_calc_d = 1; \
else if (token == '-' && !compile_calc_s) \
compile_calc_s = '-'; \
else if (token == '+' && !compile_calc_s) \
compile_calc_s = '+'; \
} \
if (token == ' ') \
token = lex_token(file); \
if (NAME != 'z') { \
if (token != ',' && token != ' ') \
error(file, ERROR_PARSE, "invalid constant initializer element %c for vector (missing spaces, or comma delimited list?)\n", NAME); \
} else if (token != '}') { \
error(file, ERROR_PARSE, "Expected `}` on end of constant initialization for vector\n"); \
} \
compile_calc_##BIT = atof(compile_data); \
compile_calc_d = 0; \
compile_calc_s = 0; \
compile_eval = &compile_data[0]; \
memset(compile_data, 0, sizeof(compile_data))
/*
* Parse all elements using the macro above.
* We must undef the macro afterwards.
*/
PARSE_VEC_ELEMENT('x', x);
PARSE_VEC_ELEMENT('y', y);
PARSE_VEC_ELEMENT('z', z);
#undef PARSE_VEC_ELEMENT
/* Check for the semi-colon... */
token = lex_token(file);
if (token == ' ')
token = lex_token(file);
if (token != ';')
error(file, ERROR_PARSE, "Expected `;` on end of constant initialization for vector\n");
/* add the compile-time constant */
{
constant c;
c.name = util_strdup(name),
c.type = TYPE_VECTOR,
c.value[0] = compile_calc_x;
c.value[1] = compile_calc_y;
c.value[2] = compile_calc_z;
c.string = NULL;
compile_constants_add(c);
}
break;
}
case TOKEN_ENTITY:
case TOKEN_FLOAT: /*TODO: validate, constant generation, name constant */
if (!isdigit(token))
error(file, ERROR_PARSE, "Expected numeric constant for float constant\n");
/* constant */
{
constant c;
c.name = util_strdup(name),
c.type = TOKEN_FLOAT,
c.value[0] = 0;
c.value[1] = 0;
c.value[2] = 0;
c.string = NULL;
compile_constants_add(c);
}
break;
}
} else if (token == '(') {
printf("FUNCTION ??\n");
}
mem_d(name);
}}
/*
* From here down is all language punctuation: There is no
* need to actual create tokens from these because they're already
* tokenized as these individual tokens (which are in a special area
* of the ascii table which doesn't conflict with our other tokens
* which are higer than the ascii table.)
*/
case '#':
token = lex_token(file); /* skip '#' */
if (token == ' ')
token = lex_token(file);
/*
* If we make it here we found a directive, the supported
* directives so far are #include.
*/
if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
char *copy = NULL;
lex_file *next = NULL;
/*
* We only suport include " ", not <> like in C (why?)
* because the latter is silly.
*/
while (*file->lastok != '"' && token != '\n')
token = lex_token(file);
if (token == '\n')
return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
copy = util_strdup(file->lastok);
next = lex_include(file, copy);
if (!next) {
error(file, ERROR_INTERNAL, "Include subsystem failure\n");
exit (-1);
}
/* constant */
{
constant c;
c.name = "#include",
c.type = TYPE_VOID,
c.value[0] = 0;
c.value[1] = 0;
c.value[2] = 0;
c.string = copy;
compile_constants_add(c);
}
parse_gen(next);
mem_d (copy);
lex_close(next);
}
/* skip all tokens to end of directive */
while (token != '\n')
token = lex_token(file);
break;
case LEX_IDENT:
token = lex_token(file);
break;
}
}
compile_constant_debug();
lex_reset(file);
/* free constants */
{
size_t i = 0;
for (; i < compile_constants_elements; i++) {
mem_d(compile_constants_data[i].name);
mem_d(compile_constants_data[i].string);
}
mem_d(compile_constants_data);
}
return 1;
}

View file

@ -625,6 +625,33 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
assignop = type_store_instr[exprs[0]->expression.vtype];
out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
break;
case opid2('+','='):
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
parseerror(parser, "Cannot add type %s and %s",
type_name[exprs[0]->expression.vtype],
type_name[exprs[1]->expression.vtype]);
return false;
}
if (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) {
parseerror(parser, "type error: %s - %s not defined",
type_name[exprs[0]->expression.vtype],
type_name[exprs[1]->expression.vtype]);
return false;
}
switch (exprs[0]->expression.vtype) {
case TYPE_FLOAT:
out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
break;
case TYPE_VECTOR:
out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
break;
default:
parseerror(parser, "type error: cannot add type %s to %s",
type_name[exprs[0]->expression.vtype],
type_name[exprs[1]->expression.vtype]);
return false;
};
break;
}
#undef NotSameType

View file

@ -1,85 +0,0 @@
/*
* Copyright (C) 2012
* Dale Weiler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "gmqcc.h"
static typedef_node *typedef_table[1024];
void typedef_init() {
int i;
for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++)
typedef_table[i] = NULL;
}
uint32_t typedef_hash(const char *s) {
return util_crc32(s, strlen(s), 1024);
}
typedef_node *typedef_find(const char *s) {
unsigned int hash = typedef_hash(s);
typedef_node *find = typedef_table[hash];
return find;
}
void typedef_clear() {
int i;
for(i = 1024; i > 0; i--) {
if(typedef_table[i]) {
mem_d(typedef_table[i]->name);
mem_d(typedef_table[i]);
}
}
}
int typedef_add(lex_file *file, const char *from, const char *to) {
unsigned int hash = typedef_hash(to);
typedef_node *find = typedef_table[hash];
if (find)
return error(file, ERROR_PARSE, "typedef for %s already exists or conflicts\n", to);
/* check if the type exists first */
if (strncmp(from, "float", sizeof("float")) == 0 ||
strncmp(from, "vector", sizeof("vector")) == 0 ||
strncmp(from, "string", sizeof("string")) == 0 ||
strncmp(from, "entity", sizeof("entity")) == 0 ||
strncmp(from, "void", sizeof("void")) == 0) {
typedef_table[hash] = mem_a(sizeof(typedef_node));
if (typedef_table[hash])
typedef_table[hash]->name = util_strdup(from);
else
return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
return -100;
} else {
/* search the typedefs for it (typedef-a-typedef?) */
find = typedef_table[typedef_hash(from)];
if (find) {
typedef_table[hash] = mem_a(sizeof(typedef_node));
if (typedef_table[hash])
typedef_table[hash]->name = util_strdup(find->name);
else
return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
return -100;
}
}
return error(file, ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from);
}