Skip to content

Commit

Permalink
creating number types
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Mar 2, 2024
1 parent 9a7cc8d commit 887d10b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
13 changes: 13 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ enum
TOKEN_TYPE_NEWLINE,
};

enum
{
NUMBER_TYPE_NORMAL,
NUMBER_TYPE_LONG,
NUMBER_TYPE_FLOAT,
NUMBER_TYPE_DOUBLE,
};

struct token
{
int type;
Expand All @@ -88,6 +96,11 @@ struct token
void *any;
};

struct token_number
{
int type;
} num;

// True if there is a whitespace between the current token and next token
bool whitespace;

Expand Down
68 changes: 44 additions & 24 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,30 @@ unsigned long long read_number()
return atoll(s);
}

struct token *token_make_number_for_value(unsigned long number)
int lexer_number_type(char c)
{
return token_create(&(struct token){.type = TOKEN_TYPE_NUMBER, .llnum = number});
int res = NUMBER_TYPE_NORMAL;
if (c == 'L')
{
res = NUMBER_TYPE_LONG;
}
else if (c == 'f')
{
res = NUMBER_TYPE_FLOAT;
}

return res;
}

struct token *
token_make_number_for_value(unsigned long number)
{
int number_type = lexer_number_type(peekc());
if (number_type != NUMBER_TYPE_NORMAL)
{
nextc();
}
return token_create(&(struct token){.type = TOKEN_TYPE_NUMBER, .llnum = number, .num.type = number_type});
}

struct token *token_make_number()
Expand Down Expand Up @@ -637,42 +658,41 @@ int lex(struct lex_process *process)

char lexer_string_buffer_next_char(struct lex_process *process)
{
struct buffer *buf = lex_process_private(process);
return buffer_read(buf);
struct buffer *buf = lex_process_private(process);
return buffer_read(buf);
}

char lexer_string_buffer_peek_char(struct lex_process *process)
{
struct buffer *buf = lex_process_private(process);
return buffer_peek(buf);
struct buffer *buf = lex_process_private(process);
return buffer_peek(buf);
}

void lexer_string_buffer_push_char(struct lex_process *process, char c)
{
struct buffer *buf = lex_process_private(process);
buffer_write(buf, c);
struct buffer *buf = lex_process_private(process);
buffer_write(buf, c);
}

struct lex_process_functions lexer_string_buffer_functions = {
.next_char=lexer_string_buffer_next_char,
.peek_char=lexer_string_buffer_peek_char,
.push_char=lexer_string_buffer_push_char
};
.next_char = lexer_string_buffer_next_char,
.peek_char = lexer_string_buffer_peek_char,
.push_char = lexer_string_buffer_push_char};

struct lex_process *tokens_build_for_string(struct compile_process *compiler, const char *str)
{
struct buffer *buffer = buffer_create();
buffer_printf(buffer, str);
struct lex_process *lex_process = lex_process_create(compiler, &lexer_string_buffer_functions, buffer);
if (!lex_process)
{
return NULL;
}
struct buffer *buffer = buffer_create();
buffer_printf(buffer, str);
struct lex_process *lex_process = lex_process_create(compiler, &lexer_string_buffer_functions, buffer);
if (!lex_process)
{
return NULL;
}

if (lex(lex_process) != LEXICAL_ANALYSIS_ALL_OK)
{
return NULL;
}
if (lex(lex_process) != LEXICAL_ANALYSIS_ALL_OK)
{
return NULL;
}

return lex_process;
return lex_process;
}
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(20+10)
int x = 123f;

0 comments on commit 887d10b

Please sign in to comment.