/*
* It's important that this table never exceed 32 keywords, the ascii
- * table starts at 33 (which we need)
+ * table starts at 33 (and we don't want conflicts)
*/
#define TOKEN_DO 0
#define TOKEN_ELSE 1
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 (FILE *);
while (isspace(ch) && ch != '\n')
ch = lex_getch(file);
- if (ch == '\n') {
- file->line ++;
+ if (ch == '\n')
return ch;
- }
-
lex_unget(ch, file);
return ' ';
}
memset(file->peek, 0, sizeof(file->peek ));
memset(file->lastok, 0, sizeof(file->lastok));
}
-
-int lex_debug(struct lex_file *file) {
- int list_do = 0;
- int list_else = 0;
- int list_if = 0;
- int list_while = 0;
- int list_break = 0;
- int list_continue = 0;
- int list_return = 0;
- int list_goto = 0;
- int list_for = 0;
- int token = 0;
- printf("===========================\nTOKENS: \n===========================\n");
- while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
- if (token != -1) {
- switch (token) {
- case 0: list_do ++; break;
- case 1: list_else ++; break;
- case 2: list_if ++; break;
- case 3: list_while ++; break;
- case 4: list_break ++; break;
- case 5: list_continue++; break;
- case 6: list_return ++; break;
- case 7: list_goto ++; break;
- case 8: list_for ++; break;
- }
- }
- if (token >= 33 && token <= 126)
- putchar(token);
- }
- printf("\n===========================\nBRANCHES \n===========================\n");
- printf("\t if % 8d\n", list_if);
- printf("\t else % 8d\n", list_else);
- printf("===========================\nLOOPS \n===========================\n");
- printf("\t for % 8d\n", list_for);
- printf("\t while % 8d\n", list_while);
- printf("\t do % 8d\n", list_do);
- printf("===========================\nSTATEMENTS \n===========================\n");
- printf("\t break % 8d\n", list_break);
- printf("\t continue % 8d\n", list_continue);
- printf("\t return % 8d\n", list_return);
- printf("\t goto % 8d\n", list_goto);
- printf("===========================\nIDENTIFIERS\n===========================\n");
- lex_reset(file);
- while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0)
- if (token == LEX_IDENT)
- printf("%s ", file->lastok);
- fputc('\n', stdout);
- lex_reset(file);
- return 1;
-}
#include <limits.h>
#include "gmqcc.h"
+//typedef int foo;
+
int usage(const char *name) {
printf("Usage: %s -f infile -o outfile\n", name);
return 0;
return error(ERROR_COMPILER, "Source file: %s not found\n", ifile);
} else {
struct lex_file *lex = lex_open(fp);
- lex_debug(lex);
parse (lex);
lex_close(lex);
}
#define PARSE_TYPE_MINUS 26
#define PARSE_TYPE_ADD 27
#define PARSE_TYPE_EQUAL 28
-#define PARSE_TYPE_LSS 29 // left subscript
-#define PARSE_TYPE_RSS 30
-#define PARSE_TYPE_LBS 31 // left bracket scope
-#define PARSE_TYPE_RBS 32 // right bracket scope
-#define PARSE_TYPE_ELIP 33 // ...
-#define PARSE_TYPE_DOT 34
-#define PARSE_TYPE_LT 35
-#define PARSE_TYPE_GT 36
-#define PARSE_TYPE_BAND 37
-#define PARSE_TYPE_BOR 38
-#define PARSE_TYPE_DONE 39 // finished statement
+#define PARSE_TYPE_LBS 29 // left bracket scope
+#define PARSE_TYPE_RBS 30 // right bracket scope
+#define PARSE_TYPE_ELIP 31 // ...
+#define PARSE_TYPE_DOT 32
+#define PARSE_TYPE_LT 33
+#define PARSE_TYPE_GT 34
+#define PARSE_TYPE_BAND 35
+#define PARSE_TYPE_BOR 36
+#define PARSE_TYPE_DONE 37 // finished statement
/*
* Adds a parse type to the parse tree, this is where all the hard
case PARSE_TYPE_BREAK: STORE("STATEMENT: BREAK \n");
case PARSE_TYPE_CONTINUE: STORE("STATEMENT: CONTINUE\n");
case PARSE_TYPE_GOTO: STORE("STATEMENT: GOTO\n");
+ case PARSE_TYPE_RETURN: STORE("STATEMENT: RETURN\n");
+ case PARSE_TYPE_DONE: STORE("STATEMENT: DONE\n");
case PARSE_TYPE_ELIP: STORE("DECLTYPE: VALIST\n");
case PARSE_TYPE_LBS: STORE("BLOCK: BEG\n");
case PARSE_TYPE_RBS: STORE("BLOCK: END\n");
+ case PARSE_TYPE_ELSE: STORE("BLOCK: ELSE\n");
+ case PARSE_TYPE_IF: STORE("BLOCK: IF\n");
case PARSE_TYPE_LAND: STORE("LOGICAL: AND\n");
case PARSE_TYPE_LNOT: STORE("LOGICAL: NOT\n");
case PARSE_TYPE_LPARTH: STORE("PARTH: BEG\n");
case PARSE_TYPE_RPARTH: STORE("PARTH: END\n");
+ case PARSE_TYPE_WHILE: STORE("LOOP: WHILE\n");
case PARSE_TYPE_FOR: STORE("LOOP: FOR\n");
case PARSE_TYPE_DO: STORE("LOOP: DO\n");
- case PARSE_TYPE_ELSE: STORE("BLOCK: ELSE\n");
- case PARSE_TYPE_IF: STORE("BLOCK: IF\n");
+
}
tree = tree->next;
}
case LEX_IDENT:
token = lex_token(file);
- printf("FOO: %s\n", file->lastok);
break;
-
+
+ /*
+ * This is a quick and easy way to do typedefs at parse time
+ * all power is in typedef_add(), in typedef.c. We handle
+ * the tokens accordingly here.
+ */
case TOKEN_TYPEDEF: {
char *f = NULL;
char *t = NULL;
typedef_add(f, t);
- /* free new strings */
+ /* free stdup strings */
mem_d(f);
mem_d(t);
break;
* 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.
+ * which are higer than the ascii table.)
*/
case '&': /* & */
token = lex_token(file);
return -100;
}
}
- return error(ERROR_PARSE, "cannot typedef %s (not a type)\n", from);
+ return error(ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from);
}