Skip to content

Commit

Permalink
handling quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 17, 2024
1 parent 33611ea commit f9b8a4a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
55 changes: 53 additions & 2 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ static void pushc(char c)
lex_process->function->push_char(lex_process, c);
}

static struct pos lex_file_position()
static char assert_next_char(char c)
{
char next_c = nextc();
assert(c == next_c);
return nextc;
}

static struct pos
lex_file_position()
{
return lex_process->pos;
}
Expand Down Expand Up @@ -414,8 +422,47 @@ struct token *token_make_newline()
return token_create(&(struct token){.type = TOKEN_TYPE_NEWLINE});
}

char lex_get_escaped_char(char c)
{
char co = 0;
switch (c)
{
case 'n':
co = '\n';
break;
case '\\':
co = '\\';
break;
case 't':
co = '\t';
break;
case '\'':
co = '\'';
break;
}
return co;
}

struct token *
read_next_token()
token_make_quote()
{
assert_next_char('\'');
char c = nextc();
if (c == '\\')
{
c = nextc();
c = lex_get_escaped_char(c);
}

if (nextc() != '\'')
{
compiler_error(lex_process->compiler, "Quote was not closed by ' character");
}

return token_create(&(struct token){.type = TOKEN_TYPE_NUMBER, .cval = c});
}

struct token *read_next_token()
{
struct token *token = NULL;
char c = peekc();
Expand Down Expand Up @@ -444,6 +491,10 @@ read_next_token()
token = token_make_string('"', '"');
break;

case '\'':
token = token_make_quote();
break;

// Ignoring whitespaces
case ' ':
case '\t':
Expand Down
2 changes: 2 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/*
multile line comment
*/

'a' '\n' '\t'

0 comments on commit f9b8a4a

Please sign in to comment.