Skip to content

Commit ba93295

Browse files
committed
Add loop keyword for unconditional loops
1 parent 50c92a7 commit ba93295

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

thecl/eclmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ is_keyword(
120120
"times", "switch", "case", "default",
121121
"break", "async", "global", "sin",
122122
"cos", "sqrt", "rad", "false", "true",
123-
"continue", "string",
123+
"continue", "string", "loop",
124124
NULL
125125
};
126126
const char **kwp = keywords;

thecl/ecsparse.y

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ static const char sub_param_fi[] = {'f', 'i'};
231231
%token T_DO "do"
232232
%token T_WHILE "while"
233233
%token T_TIMES "times"
234+
%token T_LOOP "loop"
234235
%token T_SWITCH "switch"
235236
%token T_CASE "case"
236237
%token T_DEFAULT "default"
@@ -657,6 +658,7 @@ Block:
657658
| IfBlock
658659
| WhileBlock
659660
| TimesBlock
661+
| LoopBlock
660662
| SwitchBlock
661663
;
662664

@@ -677,7 +679,8 @@ BreakStatement:
677679
if (
678680
strncmp(head->data, "while", 5) == 0 ||
679681
strncmp(head->data, "switch", 6) == 0 ||
680-
strncmp(head->data, "times", 5) == 0
682+
strncmp(head->data, "times", 5) == 0 ||
683+
strncmp(head->data, "loop", 4) == 0
681684
) {
682685
char labelstr[256];
683686
snprintf(labelstr, 256, "%s_end", (char*)head->data);
@@ -698,7 +701,8 @@ ContinueStatement:
698701
for(; head; head = head->next) {
699702
if (
700703
strncmp(head->data, "while", 5) == 0 ||
701-
strncmp(head->data, "times", 5) == 0
704+
strncmp(head->data, "times", 5) == 0 ||
705+
strncmp(head->data, "loop", 4) == 0
702706
) {
703707
char labelstr[256];
704708
snprintf(labelstr, 256, "%s_st", (char*)head->data);
@@ -871,6 +875,32 @@ TimesBlock:
871875
}
872876
;
873877

878+
LoopBlock:
879+
"loop" {
880+
char labelstr[250];
881+
snprintf(labelstr, 250, "loop_%i_%i", yylloc.first_line, yylloc.first_column);
882+
char labelstr_st[256];
883+
char labelstr_end[256];
884+
snprintf(labelstr_st, 256, "%s_st", (char*)labelstr);
885+
snprintf(labelstr_end, 256, "%s_end", (char*)labelstr);
886+
887+
list_prepend_new(&state->block_stack, strdup(labelstr));
888+
label_create(state, labelstr_st);
889+
} CodeBlock {
890+
char labelstr_st[256];
891+
char labelstr_end[256];
892+
list_node_t *head = state->block_stack.head;
893+
snprintf(labelstr_st, 256, "%s_st", (char*)head->data);
894+
snprintf(labelstr_end, 256, "%s_end", (char*)head->data);
895+
896+
expression_create_goto(state, GOTO, labelstr_st);
897+
label_create(state, labelstr_end);
898+
899+
free(head->data);
900+
list_del(&state->block_stack, head);
901+
}
902+
;
903+
874904
SwitchBlock:
875905
"switch" '(' ExpressionAny[cond] ')' {
876906
char name[256];

thecl/ecsscan.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"do" return T_DO;
101101
"while" return T_WHILE;
102102
"times" return T_TIMES;
103+
"loop" return T_LOOP;
103104
"switch" return T_SWITCH;
104105
"case" return T_CASE;
105106
"default" return T_DEFAULT;

0 commit comments

Comments
 (0)