Skip to content

Commit

Permalink
creating string token
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 3, 2024
1 parent 5e32b5f commit cabc013
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files.associations": {
"*.ejs": "html",
"*.h": "c"
"*.h": "c",
"cstdlib": "c"
}
}
25 changes: 25 additions & 0 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "helpers/vector.h"
#include "helpers/buffer.h"
#include <string.h>
#include <assert.h>

#define LEX_GETC_IF(buffer, c, exp) \
for (c = peekc(); exp; c = peekc()) \
Expand Down Expand Up @@ -94,6 +95,26 @@ struct token *token_make_number()
return token_make_number_for_value(read_number());
}

static struct token *token_make_string(char start_delim, char end_delim)
{
struct buffer *buf = buffer_create();
assert(nextc() == start_delim);
char c = nextc();
for (; c != end_delim && c != EOF; c = nextc())
{
if (c == '\\')
{
// handle escape character
continue;
}

buffer_write(buf, c);
}

buffer_write(buf, 0x00);
return token_create(&(struct token){.type = TOKEN_TYPE_STRING, .sval = buffer_ptr(buf)});
}

struct token *read_next_token()
{
struct token *token = NULL;
Expand All @@ -104,6 +125,10 @@ struct token *read_next_token()
token = token_make_number();
break;

case '"':
token = token_make_string('"', '"');
break;

// Ignoring whitespaces
case ' ':
case '\t':
Expand Down
Empty file removed test
Empty file.
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123 456
"hello" 123 "world"

0 comments on commit cabc013

Please sign in to comment.