Skip to content

Commit

Permalink
Lecture 88 - IMplementing goto
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonzapeducation committed Jun 29, 2022
1 parent 05be196 commit 2ee466e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ struct node
struct vector* cases;
bool has_default_case;
} switch_stmt;

struct _goto_stmt
{
struct node* label;
} _goto;
} stmt;


Expand Down Expand Up @@ -600,6 +605,7 @@ bool node_is_value_type(struct node* node);

struct node* struct_node_for_name(struct compile_process* current_process, const char* name);

void make_goto_node(struct node* label_node);
void make_label_node(struct node* name_node);

void make_continue_node();
Expand Down
5 changes: 5 additions & 0 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ struct node* node_peek_expressionable_or_null()
return node_is_expressionable(last_node) ? last_node : NULL;
}

void make_goto_node(struct node* label_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_GOTO, .stmt._goto.label=label_node});
}

void make_label_node(struct node* name_node)
{
node_create(&(struct node){.type=NODE_TYPE_LABEL, .label.name=name_node});
Expand Down
22 changes: 22 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,15 @@ void parse_break(struct history* history)
make_break_node();
}

void parse_goto(struct history* history)
{
expect_keyword("goto");
parse_identifier(history_begin(0));
expect_sym(';');

struct node* label_node = node_pop();
make_goto_node(label_node);
}
void parse_label(struct history* history)
{
expect_sym(':');
Expand All @@ -1512,10 +1521,12 @@ void parse_keyword(struct history *history)
if (S_EQ(token->sval, "break"))
{
parse_break(history);
return;
}
else if(S_EQ(token->sval, "continue"))
{
parse_continue(history);
return;
}
else if (S_EQ(token->sval, "return"))
{
Expand All @@ -1530,19 +1541,30 @@ void parse_keyword(struct history *history)
else if (S_EQ(token->sval, "for"))
{
parse_for_stmt(history);
return;
}
else if (S_EQ(token->sval, "while"))
{
parse_while(history);
return;
}
else if (S_EQ(token->sval, "do"))
{
parse_do_while(history);
return;
}
else if(S_EQ(token->sval, "switch"))
{
parse_switch(history);
return;
}
else if(S_EQ(token->sval, "goto"))
{
parse_goto(history);
return;
}

compiler_error(current_process, "Invalid keyword\n");
}


Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ int main(int x)
while(1)
{
continue;
goto abc;
}

abc:
}

0 comments on commit 2ee466e

Please sign in to comment.