Implemented support for #include, but it doesn't add an include to the lexer yet.

This commit is contained in:
Dale Weiler 2012-04-11 12:13:31 -04:00
parent 9a7fa82bd0
commit 51e2f2b731
6 changed files with 65 additions and 14 deletions

7
code.c
View file

@ -151,6 +151,7 @@ VECTOR_MAKE(char, code_strings );
prog_header code_header;
void code_write() {
#if 0
/* Add test program */
code_strings_add('\0');
@ -188,7 +189,7 @@ void code_write() {
code_functions_add((prog_section_function){0, 0, 0, 0, .name=0, 0, 0, {0}}); /* NULL */
code_functions_add((prog_section_function){1, 0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
code_functions_add((prog_section_function){-2, 0, 0, 0, .name=8, 0, 0, {0}}); /* print */
code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print */
code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13, 0,0, {0}}); /* m_keydown */
code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10, 0,0, {0}});
code_functions_add((prog_section_function){0, 0, 0, 0, .name=14+13+10+7, 0,0, {0}});
@ -208,8 +209,10 @@ void code_write() {
code_header.globals = (prog_section){code_header.functions.offset + sizeof(prog_section_function) *code_functions_elements, code_globals_elements };
code_header.strings = (prog_section){code_header.globals.offset + sizeof(int) *code_globals_elements, code_strings_elements };
code_header.entfield = 0; /* TODO: */
#endif
/* write out everything one SHOT! */
FILE *fp = fopen("program.dat", "wb");
fwrite(&code_header, 1, sizeof(prog_header), fp);
fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);

1
lex.c
View file

@ -302,6 +302,7 @@ int lex_token(struct lex_file *file) {
if (typedef_find(file->lastok))
TEST_TYPE(typedef_find(file->lastok)->name);
#undef TEST_TYPE
return LEX_IDENT;
}
return ch;

8
main.c
View file

@ -20,10 +20,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "gmqcc.h"
# include <stdlib.h>
#include <string.h>
# include <limits.h>
# include "gmqcc.h"
int main(int argc, char **argv) {
argc--;

48
parse.c
View file

@ -23,6 +23,7 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gmqcc.h"
/*
@ -278,7 +279,18 @@ int parse_tree(struct lex_file *file) {
case TOKEN_CONTINUE:
PARSE_TREE_ADD(PARSE_TYPE_CONTINUE);
break;
/*
* DO loops work like so:
* __do_loop_beg:
* IFNOT __do_loop_beg#
* GOTO __do_loop_end
* INSTR1
* INSTR2
* ......
* GOTO __do_loop_beg
*/
case TOKEN_DO: PARSE_PERFORM(PARSE_TYPE_DO, {});
case TOKEN_WHILE: PARSE_PERFORM(PARSE_TYPE_WHILE, {});
case TOKEN_BREAK: PARSE_PERFORM(PARSE_TYPE_BREAK, {});
@ -297,14 +309,36 @@ int parse_tree(struct lex_file *file) {
* which are higer than the ascii table.)
*/
case '#':
token = lex_token(file); /* skip '#' */
while (isspace(token)) {
if (token == '\n') {
return error(ERROR_PARSE, "Expected valid preprocessor directive after `#` %s\n");
}
token = lex_token(file); /* try again */
}
/*
* Skip the preprocessor for now: We'll implement our own
* eventually. For now we need to make sure directives are
* not accidently tokenized.
* If we make it here we found a directive, the supported
* directives so far are #include.
*/
token = lex_token(file);
token = lex_token(file);
if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
//lex_include("file");
/*
* We need to compose a name till we find either a
* '"',> or a <, (for includes), if we hit a '\n' then
* clearly the person miswrote the include.
*/
while (*file->lastok != '"' && token != '\n')
token = lex_token(file);
if (token == '\n')
return error(ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
//lex_token(file);
//lex_token(file);
printf("include: %s\n", file->lastok);
}
/* skip all tokens to end of directive */
while (token != '\n')
token = lex_token(file);

13
test/include.qc Normal file
View file

@ -0,0 +1,13 @@
/*
* all of these includes should work. No matter what the spacing
* is, we rely on it.
*/
#include "include.h"
#include "include.h"
#include "include.h"
#include "include.h"
#include "include.h"
# include "include.h"
# include "include.h"
#include "include.h"
# include "include.h"

View file

@ -80,7 +80,7 @@ int typedef_add(const char *from, const char *to) {
strncmp(from, "void", sizeof("void")) == 0) {
typedef_table[hash] = mem_a(sizeof(typedef_node));
typedef_table[hash]->name = strdup(from);
typedef_table[hash]->name = util_strdup(from);
return -100;
} else {
/* search the typedefs for it (typedef-a-typedef?) */