Skip to content

Commit

Permalink
creating symbol token
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 10, 2024
1 parent 33b837a commit 841445e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
10 changes: 10 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ struct pos
case '.': \
case '?'

#define SYMBOL_CASE \
case '{': \
case '}': \
case ':': \
case ';': \
case '#': \
case '\\': \
case ')': \
case ']'

enum
{
LEXICAL_ANALYSIS_ALL_OK,
Expand Down
28 changes: 26 additions & 2 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ void read_op_flush_back_keep_first(struct buffer *buffer)
}
}

const char *
read_op()
const char *read_op()
{
bool single_operator = true;
char op = nextc();
Expand Down Expand Up @@ -240,6 +239,15 @@ static void lex_new_expression()
}
}

static void lex_finish_expression()
{
lex_process->current_expression_count--;
if (lex_process->current_expression_count < 0)
{
compiler_error(lex_process->compiler, "Closed expression that was never opened\n");
}
}

bool lex_is_in_expression()
{
return lex_process->current_expression_count >= 0;
Expand All @@ -265,6 +273,18 @@ static struct token *token_make_operator_or_string()
return token;
}

static struct token *token_make_symbol()
{
char c = nextc();
if (c == ')')
{
lex_finish_expression();
}

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

struct token *
read_next_token()
{
Expand All @@ -280,6 +300,10 @@ read_next_token()
token = token_make_operator_or_string();
break;

SYMBOL_CASE:
token = token_make_symbol();
break;

case '"':
token = token_make_string('"', '"');
break;
Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50 + 20 - 10 ++
50 + 20 - 10 ++(50 + 20)[#]

0 comments on commit 841445e

Please sign in to comment.