Skip to content

Commit

Permalink
fix index error in parser_set func
Browse files Browse the repository at this point in the history
  • Loading branch information
ysw421 committed Feb 3, 2024
1 parent f5fe6cc commit 78f8052
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/test3.or
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
f:=\left(3\right)
%f(x):=\begin{function}

print("hello", 5+2, "hello ", 32+1-3, "hello")
16 changes: 9 additions & 7 deletions src/Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,21 @@ Parser* init_parser(Lexer* lexer)

Parser* parser_set(Parser* parser, size_t pointer)
{
if (!parser || pointer < 0 || pointer > parser->size - 1)
if (!parser || pointer < 0 || pointer > parser->size)
{
printf("에러, 잘못된 parser 설정\n");
exit(1);
}
parser->pointer = pointer;
parser->prev_token = (pointer > 0)
? parser->tokens[pointer - 1]
: (void*) 0;
parser->token = parser->tokens[pointer - 1];
parser->next_token = (pointer < parser->size - 1)
parser->prev_token = (pointer >= 1)
? parser->tokens[pointer]
: (void*) 0;
parser->token = parser->tokens[pointer];
parser->next_token = (pointer < parser->size)
? parser->tokens[pointer + 1]
: (void*) 0;

return parser;
}

Parser* parser_advance(Parser* parser, int type)
Expand All @@ -155,7 +157,7 @@ Parser* parser_advance(Parser* parser, int type)
parser->prev_token = parser->token;
parser->token = parser->next_token;
parser->pointer ++;
if (parser->pointer + 2 <= parser->size)
if (parser->pointer < parser->size)
parser->next_token = parser->tokens[parser->pointer + 1];
else
parser->next_token = (void*) 0;
Expand Down
18 changes: 15 additions & 3 deletions src/Parser/parser_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,23 @@ AST* parser_parse_function(Parser* parser, AST* ast, Token* last_token,
else
{
token = parser->token;
if (token->type == TOKEN_BEGIN)
{}
size_t pointer = parser->pointer;
printf("@@@ %d\n", token->type == parser->tokens[pointer]->type);
if (
pointer + 3 <= parser->size
&& token->type == TOKEN_BEGIN
&& parser->next_token->type == TOKEN_LBRACE
&& parser->tokens[pointer + 2]->type == TOKEN_ID
&& (!strcmp(parser->tokens[pointer + 2]->value, "function")
|| !strcmp(parser->tokens[pointer + 2]->value, "fun"))
&& parser->tokens[pointer + 3]->type == TOKEN_RBRACE
)
{
printf("!@#$@#!@$@#$#@$#@\n");
}
else
{
printf("에러, ':=' 뒤에는 값이 와야함.");
printf("에러, ':=' 뒤에는 값이 와야함2.");
exit(1);
}
printf("@!#!@ %s\n", token->value);
Expand Down

0 comments on commit 78f8052

Please sign in to comment.