2017-11-05 14:23:17 +00:00
|
|
|
/* Copyright (C) 2016 Jeremiah Orians
|
|
|
|
* This file is part of stage0.
|
|
|
|
*
|
|
|
|
* stage0 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* stage0 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with stage0. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cc.h"
|
|
|
|
FILE* input;
|
2017-11-08 23:05:54 +00:00
|
|
|
struct token_list* token;
|
2018-05-26 21:33:00 +01:00
|
|
|
int line;
|
|
|
|
char* file;
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-07-21 17:45:19 +01:00
|
|
|
int clearWhiteSpace(int c)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-05-26 21:33:00 +01:00
|
|
|
if((32 == c) || (9 == c)) return clearWhiteSpace(fgetc(input));
|
|
|
|
else if (10 == c)
|
|
|
|
{
|
|
|
|
line = line + 1;
|
|
|
|
return clearWhiteSpace(fgetc(input));
|
|
|
|
}
|
2017-11-05 14:23:17 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
int consume_byte(int c)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
hold_string[string_index] = c;
|
2017-11-05 14:23:17 +00:00
|
|
|
string_index = string_index + 1;
|
|
|
|
return fgetc(input);
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
int consume_word(int c, int frequent)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-06-02 01:05:27 +01:00
|
|
|
int escape = FALSE;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(!escape && '\\' == c ) escape = TRUE;
|
|
|
|
else escape = FALSE;
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_byte(c);
|
2018-06-02 01:05:27 +01:00
|
|
|
} while(escape || (c != frequent));
|
2017-12-14 22:01:30 +00:00
|
|
|
return fgetc(input);
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
void fixup_label()
|
2018-01-25 01:00:22 +00:00
|
|
|
{
|
|
|
|
int hold = ':';
|
|
|
|
int prev;
|
|
|
|
int i = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
prev = hold;
|
2018-07-26 23:27:32 +01:00
|
|
|
hold = hold_string[i];
|
|
|
|
hold_string[i] = prev;
|
2018-01-25 01:00:22 +00:00
|
|
|
i = i + 1;
|
|
|
|
} while(0 != hold);
|
|
|
|
}
|
|
|
|
|
2018-08-10 22:30:48 +01:00
|
|
|
int in_set(int c, char* s)
|
|
|
|
{
|
|
|
|
while(0 != s[0])
|
|
|
|
{
|
|
|
|
if(c == s[0]) return TRUE;
|
|
|
|
s = s + 1;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
int preserve_keyword(int c)
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-08-10 22:30:48 +01:00
|
|
|
while(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_byte(c);
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
2018-01-25 01:00:22 +00:00
|
|
|
if(':' == c)
|
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
fixup_label();
|
2018-01-25 01:00:22 +00:00
|
|
|
return 32;
|
|
|
|
}
|
2017-12-19 20:44:27 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
int preserve_symbol(int c)
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-08-10 22:30:48 +01:00
|
|
|
while(in_set(c, "<=>|&!-"))
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_byte(c);
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:45:19 +01:00
|
|
|
int purge_macro(int ch)
|
2017-11-08 23:05:54 +00:00
|
|
|
{
|
|
|
|
while(10 != ch) ch = fgetc(input);
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
void reset_hold_string()
|
|
|
|
{
|
|
|
|
int i = string_index + 2;
|
|
|
|
while(0 != i)
|
|
|
|
{
|
|
|
|
hold_string[i] = 0;
|
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 14:23:17 +00:00
|
|
|
int get_token(int c)
|
|
|
|
{
|
|
|
|
struct token_list* current = calloc(1, sizeof(struct token_list));
|
|
|
|
|
2017-12-19 20:44:27 +00:00
|
|
|
reset:
|
2018-07-26 23:27:32 +01:00
|
|
|
reset_hold_string();
|
2017-12-19 20:44:27 +00:00
|
|
|
string_index = 0;
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2017-12-19 20:44:27 +00:00
|
|
|
c = clearWhiteSpace(c);
|
|
|
|
if('#' == c)
|
|
|
|
{
|
|
|
|
c = purge_macro(c);
|
|
|
|
goto reset;
|
|
|
|
}
|
2018-08-10 22:30:48 +01:00
|
|
|
else if(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = preserve_keyword(c);
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
2018-08-10 22:30:48 +01:00
|
|
|
else if(in_set(c, "<=>|&!-"))
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = preserve_symbol(c);
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
2018-07-26 23:27:32 +01:00
|
|
|
else if(c == '\'')
|
2018-01-25 01:00:22 +00:00
|
|
|
{ /* 39 == ' */
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_word(c, '\'');
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
else if(c == '"')
|
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_word(c, '"');
|
2017-12-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
else if(c == '/')
|
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_byte(c);
|
2017-12-19 20:44:27 +00:00
|
|
|
if(c == '*')
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2017-12-19 20:44:27 +00:00
|
|
|
c = fgetc(input);
|
|
|
|
while(c != '/')
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-05-26 21:33:00 +01:00
|
|
|
while(c != '*')
|
|
|
|
{
|
|
|
|
c = fgetc(input);
|
|
|
|
if(10 == c) line = line + 1;
|
|
|
|
}
|
2017-12-19 20:44:27 +00:00
|
|
|
c = fgetc(input);
|
2018-05-26 21:33:00 +01:00
|
|
|
if(10 == c) line = line + 1;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
2017-12-19 20:44:27 +00:00
|
|
|
c = fgetc(input);
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
else if(c == '/')
|
|
|
|
{
|
|
|
|
c = fgetc(input);
|
|
|
|
goto reset;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
else if(c == EOF)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
free(current);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-26 23:27:32 +01:00
|
|
|
c = consume_byte(c);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-07-26 23:27:32 +01:00
|
|
|
/* More efficiently allocate memory for string */
|
|
|
|
current->s = calloc(string_index + 2, sizeof(char));
|
|
|
|
copy_string(current->s, hold_string);
|
|
|
|
|
2017-11-08 23:05:54 +00:00
|
|
|
current->prev = token;
|
|
|
|
current->next = token;
|
2018-05-26 21:33:00 +01:00
|
|
|
current->linenumber = line;
|
|
|
|
current->filename = file;
|
2017-11-08 23:05:54 +00:00
|
|
|
token = current;
|
2017-11-05 14:23:17 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct token_list* reverse_list(struct token_list* head)
|
|
|
|
{
|
|
|
|
struct token_list* root = NULL;
|
|
|
|
while(NULL != head)
|
|
|
|
{
|
|
|
|
struct token_list* next = head->next;
|
|
|
|
head->next = root;
|
|
|
|
root = head;
|
|
|
|
head = next;
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2018-05-26 21:33:00 +01:00
|
|
|
struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
input = a;
|
2018-05-26 21:33:00 +01:00
|
|
|
line = 1;
|
|
|
|
file = filename;
|
2018-01-21 03:56:50 +00:00
|
|
|
token = current;
|
2017-11-05 14:23:17 +00:00
|
|
|
int ch =fgetc(input);
|
2018-05-26 21:33:00 +01:00
|
|
|
while(EOF != ch) ch = get_token(ch);
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
return token;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|