Added CONSTANT to the language and fixed Capitalization problems

This commit is contained in:
Jeremiah Orians 2017-12-19 15:44:27 -05:00
parent 18e433def4
commit 9fe1a878cc
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
2 changed files with 80 additions and 36 deletions

31
cc.c
View File

@ -22,9 +22,11 @@
#define FUNCTION 2 #define FUNCTION 2
#define LOCAL_VARIABLE 4 #define LOCAL_VARIABLE 4
#define ARGUEMENT 8 #define ARGUEMENT 8
#define CONSTANT 16
/* Globals */ /* Globals */
struct token_list* global_symbol_list; struct token_list* global_symbol_list;
struct token_list* global_constant_list;
struct token_list* global_token; struct token_list* global_token;
struct token_list* current_target; struct token_list* current_target;
struct token_list* strings_list; struct token_list* strings_list;
@ -132,7 +134,8 @@ int stack_index(struct token_list* a, struct token_list* function)
struct token_list* sym_get_value(char *s, struct token_list* out, struct token_list* function) struct token_list* sym_get_value(char *s, struct token_list* out, struct token_list* function)
{ {
struct token_list* a = sym_lookup(s, function->locals); struct token_list* a = sym_lookup(s, global_constant_list);
if(NULL == a) a= sym_lookup(s, function->locals);
if(NULL == a) a = sym_lookup(s, function->arguments); if(NULL == a) a = sym_lookup(s, function->arguments);
if(NULL == a) a = sym_lookup(s, global_symbol_list); if(NULL == a) a = sym_lookup(s, global_symbol_list);
if(a == NULL) if(a == NULL)
@ -146,6 +149,7 @@ struct token_list* sym_get_value(char *s, struct token_list* out, struct token_l
switch(a->type) switch(a->type)
{ {
case CONSTANT: out = double_emit("LOAD_IMMEDIATE_eax %", a->arguments->s, out, true); return out;;
case FUNCTION: return out; case FUNCTION: return out;
case GLOBAL: out = double_emit("LOAD_IMMEDIATE_eax &GLOBAL_", s, out, true); break; case GLOBAL: out = double_emit("LOAD_IMMEDIATE_eax &GLOBAL_", s, out, true); break;
default: out = double_emit("LOAD_EFFECTIVE_ADDRESS %", numerate_number(stack_index(a, function)), out, false); default: out = double_emit("LOAD_EFFECTIVE_ADDRESS %", numerate_number(stack_index(a, function)), out, false);
@ -179,7 +183,7 @@ struct token_list* primary_expr(struct token_list* out, struct token_list* funct
out = double_emit("LOAD_IMMEDIATE_eax %", global_token->s, out, true); out = double_emit("LOAD_IMMEDIATE_eax %", global_token->s, out, true);
global_token = global_token->next; global_token = global_token->next;
} }
else if(('a' <= global_token->s[0]) & (global_token->s[0] <= 'z')) else if((('a' <= global_token->s[0]) & (global_token->s[0] <= 'z')) | (('A' <= global_token->s[0]) & (global_token->s[0] <= 'Z')))
{ {
out = sym_get_value(global_token->s, out, function); out = sym_get_value(global_token->s, out, function);
} }
@ -547,7 +551,6 @@ int if_count;
struct token_list* process_if(struct token_list* out, struct token_list* function) struct token_list* process_if(struct token_list* out, struct token_list* function)
{ {
char* number_string = numerate_number(if_count); char* number_string = numerate_number(if_count);
int number = if_count;
if_count = if_count + 1; if_count = if_count + 1;
out = double_emit("# IF_",number_string, out, false); out = double_emit("# IF_",number_string, out, false);
@ -815,6 +818,7 @@ struct token_list* declare_function(struct token_list* out, struct type* type)
* declaration program * declaration program
* *
* declaration: * declaration:
* CONSTANT identifer value
* type-name identifier ; * type-name identifier ;
* type-name identifier ( parameter-list ) ; * type-name identifier ( parameter-list ) ;
* type-name identifier ( parameter-list ) statement * type-name identifier ( parameter-list ) statement
@ -830,14 +834,23 @@ struct token_list* program(struct token_list* out)
{ {
while(NULL != global_token->next) while(NULL != global_token->next)
{ {
struct type* type_size = type_name(); if(!strcmp(global_token->s, "CONSTANT"))
global_token = global_token->next; {
if(global_token->s[0] == ';') out = declare_global(out, type_size); global_constant_list = sym_declare(global_token->next->s, CONSTANT, NULL, global_constant_list);
else if(global_token->s[0] == '(') out = declare_function(out, type_size); global_constant_list->arguments = global_token->next->next;
global_token = global_token->next->next->next;
}
else else
{ {
fprintf(stderr, "Recieved %s in program\n", global_token->s); struct type* type_size = type_name();
exit(EXIT_FAILURE); global_token = global_token->next;
if(global_token->s[0] == ';') out = declare_global(out, type_size);
else if(global_token->s[0] == '(') out = declare_function(out, type_size);
else
{
fprintf(stderr, "Recieved %s in program\n", global_token->s);
exit(EXIT_FAILURE);
}
} }
} }
return out; return out;

View File

@ -40,6 +40,24 @@ char consume_word(struct token_list* current, char c, char frequent)
return fgetc(input); return fgetc(input);
} }
char preserve_keyword(struct token_list* current, char c)
{
while((('a' <= c) & (c <= 'z')) | (('A' <= c) & (c <= 'Z')) | (('0' <= c) & (c <= '9')) | (c == '_'))
{
c = consume_byte(current, c);
}
return c;
}
char preserve_symbol(struct token_list* current, char c)
{
while((c == '<') | (c == '=') | (c == '>') | (c == '|') | (c == '&') | (c == '!'))
{
c = consume_byte(current, c);
}
return c;
}
char purge_macro(int ch) char purge_macro(int ch)
{ {
while(10 != ch) ch = fgetc(input); while(10 != ch) ch = fgetc(input);
@ -48,42 +66,55 @@ char purge_macro(int ch)
int get_token(int c) int get_token(int c)
{ {
if('#' == c) c = purge_macro(c);
bool w = true;
struct token_list* current = calloc(1, sizeof(struct token_list)); struct token_list* current = calloc(1, sizeof(struct token_list));
current->s = calloc(MAX_STRING, sizeof(char)); current->s = calloc(MAX_STRING, sizeof(char));
while(w) reset:
{ string_index = 0;
w = false;
string_index = 0;
c = clearWhiteSpace(c); c = clearWhiteSpace(c);
while((('a' <= c) & (c <= 'z')) | (('0' <= c) & (c <= '9')) | (c == '_')) c = consume_byte(current, c); if('#' == c)
if(string_index == 0) while((c == '<') | (c == '=') | (c == '>') | (c == '|') | (c == '&') | (c == '!')) c = consume_byte(current, c); {
if(string_index == 0) c = purge_macro(c);
goto reset;
}
else if((('a' <= c) & (c <= 'z')) | (('A' <= c) & (c <= 'Z')) | (('0' <= c) & (c <= '9')) | (c == '_'))
{
c = preserve_keyword(current, c);
}
else if((c == '<') | (c == '=') | (c == '>') | (c == '|') | (c == '&') | (c == '!'))
{
c = preserve_symbol(current, c);
}
else if(c == '\'')
{
c = consume_word(current, c, '\'');
}
else if(c == '"')
{
c = consume_word(current, c, '"');
}
else if(c == '/')
{
c = consume_byte(current, c);
if(c == '*')
{ {
if(c == 39) c = consume_word(current, c, 39); c = fgetc(input);
else if(c == '"') c = consume_word(current, c, '"'); while(c != '/')
else if(c == '/')
{ {
c = consume_byte(current, c); while(c != '*') c = fgetc(input);
if(c == '*') c = fgetc(input);
{
c = fgetc(input);
while(c != '/')
{
while(c != '*') c = fgetc(input);
c = fgetc(input);
}
c = fgetc(input);
w = true;
}
} }
else if(c != EOF) c = consume_byte(current, c); c = fgetc(input);
goto reset;
}
else if(c == '/')
{
c = fgetc(input);
goto reset;
} }
} }
else if(c != EOF) c = consume_byte(current, c);
current->prev = token; current->prev = token;
current->next = token; current->next = token;