Added CONSTANT to the language and fixed Capitalization problems
This commit is contained in:
parent
18e433def4
commit
9fe1a878cc
31
cc.c
31
cc.c
|
@ -22,9 +22,11 @@
|
|||
#define FUNCTION 2
|
||||
#define LOCAL_VARIABLE 4
|
||||
#define ARGUEMENT 8
|
||||
#define CONSTANT 16
|
||||
|
||||
/* Globals */
|
||||
struct token_list* global_symbol_list;
|
||||
struct token_list* global_constant_list;
|
||||
struct token_list* global_token;
|
||||
struct token_list* current_target;
|
||||
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* 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, global_symbol_list);
|
||||
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)
|
||||
{
|
||||
case CONSTANT: out = double_emit("LOAD_IMMEDIATE_eax %", a->arguments->s, out, true); return out;;
|
||||
case FUNCTION: return out;
|
||||
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);
|
||||
|
@ -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);
|
||||
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);
|
||||
}
|
||||
|
@ -547,7 +551,6 @@ int if_count;
|
|||
struct token_list* process_if(struct token_list* out, struct token_list* function)
|
||||
{
|
||||
char* number_string = numerate_number(if_count);
|
||||
int number = if_count;
|
||||
if_count = if_count + 1;
|
||||
|
||||
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:
|
||||
* CONSTANT identifer value
|
||||
* type-name identifier ;
|
||||
* type-name identifier ( parameter-list ) ;
|
||||
* type-name identifier ( parameter-list ) statement
|
||||
|
@ -830,14 +834,23 @@ struct token_list* program(struct token_list* out)
|
|||
{
|
||||
while(NULL != global_token->next)
|
||||
{
|
||||
struct type* type_size = type_name();
|
||||
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);
|
||||
if(!strcmp(global_token->s, "CONSTANT"))
|
||||
{
|
||||
global_constant_list = sym_declare(global_token->next->s, CONSTANT, NULL, global_constant_list);
|
||||
global_constant_list->arguments = global_token->next->next;
|
||||
global_token = global_token->next->next->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Recieved %s in program\n", global_token->s);
|
||||
exit(EXIT_FAILURE);
|
||||
struct type* type_size = type_name();
|
||||
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;
|
||||
|
|
85
cc_reader.c
85
cc_reader.c
|
@ -40,6 +40,24 @@ char consume_word(struct token_list* current, char c, char frequent)
|
|||
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)
|
||||
{
|
||||
while(10 != ch) ch = fgetc(input);
|
||||
|
@ -48,42 +66,55 @@ char purge_macro(int ch)
|
|||
|
||||
int get_token(int c)
|
||||
{
|
||||
if('#' == c) c = purge_macro(c);
|
||||
bool w = true;
|
||||
|
||||
struct token_list* current = calloc(1, sizeof(struct token_list));
|
||||
current->s = calloc(MAX_STRING, sizeof(char));
|
||||
|
||||
while(w)
|
||||
{
|
||||
w = false;
|
||||
string_index = 0;
|
||||
reset:
|
||||
string_index = 0;
|
||||
|
||||
c = clearWhiteSpace(c);
|
||||
while((('a' <= c) & (c <= 'z')) | (('0' <= c) & (c <= '9')) | (c == '_')) c = consume_byte(current, c);
|
||||
if(string_index == 0) while((c == '<') | (c == '=') | (c == '>') | (c == '|') | (c == '&') | (c == '!')) c = consume_byte(current, c);
|
||||
if(string_index == 0)
|
||||
c = clearWhiteSpace(c);
|
||||
if('#' == c)
|
||||
{
|
||||
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);
|
||||
else if(c == '"') c = consume_word(current, c, '"');
|
||||
else if(c == '/')
|
||||
c = fgetc(input);
|
||||
while(c != '/')
|
||||
{
|
||||
c = consume_byte(current, c);
|
||||
if(c == '*')
|
||||
{
|
||||
c = fgetc(input);
|
||||
while(c != '/')
|
||||
{
|
||||
while(c != '*') c = fgetc(input);
|
||||
c = fgetc(input);
|
||||
}
|
||||
c = fgetc(input);
|
||||
w = true;
|
||||
}
|
||||
while(c != '*') c = fgetc(input);
|
||||
c = fgetc(input);
|
||||
}
|
||||
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->next = token;
|
||||
|
|
Loading…
Reference in New Issue