for added

master
serr 2025-04-01 21:15:52 +03:00
parent e80f2e0392
commit 148e0e23c6
3 changed files with 19 additions and 1 deletions

View File

@ -9,6 +9,8 @@ void yyerror(const char *s) {
line_number, line_number,
s, s,
yytext); yytext);
exit(-1);
} }
%} %}
@ -37,6 +39,7 @@ void yyerror(const char *s) {
"while" { return WHILE; } "while" { return WHILE; }
"do" { return DO; } "do" { return DO; }
"for" { return FOR; }
"if" { return IF; } "if" { return IF; }
"else" { return ELSE; } "else" { return ELSE; }

View File

@ -16,7 +16,7 @@ bool debug = false; // debug mode
} }
%token <str> IDENTIFIER NUMBER %token <str> IDENTIFIER NUMBER
%token LBRACE RBRACE LPAREN RPAREN SEMICOLON ASSIGN PLUS MINUS MULT DIV MOD RET PRINT WHILE DO IF ELSE AND OR NOT LT GT EQ %token LBRACE RBRACE LPAREN RPAREN SEMICOLON ASSIGN PLUS MINUS MULT DIV MOD RET PRINT WHILE DO FOR IF ELSE AND OR NOT LT GT EQ
%token FUNC COMMA %token FUNC COMMA
%type <str> expr program statement block func_decl param_list params %type <str> expr program statement block func_decl param_list params
@ -55,6 +55,17 @@ statement:
LPAREN expr RPAREN SEMICOLON LPAREN expr RPAREN SEMICOLON
{ if (debug) printf("\033[1;34mDO-WHILE LOOP ENDED\033[0m\n"); } { if (debug) printf("\033[1;34mDO-WHILE LOOP ENDED\033[0m\n"); }
| FOR { if (debug) printf("\033[1;34mFOR LOOP DETECTED\033[0m\n"); }
LPAREN
expr SEMICOLON { if (debug) printf("\033[1;34mFOR INITIALIZE ENDED\033[0m\n"); }
expr SEMICOLON { if (debug) printf("\033[1;34mFOR CONDITION ENDED\033[0m\n"); }
expr
RPAREN
{ if (debug) printf("\033[1;34mFOR BODY STARTS\033[0m\n"); }
block
{ if (debug) printf("\033[1;34mFOR LOOP ENDED\033[0m\n"); }
;
| IF LPAREN expr RPAREN block | IF LPAREN expr RPAREN block
{ if (debug) printf("\033[1;34mIF BLOCK ENDED\033[0m\n"); } { if (debug) printf("\033[1;34mIF BLOCK ENDED\033[0m\n"); }
| IF LPAREN expr RPAREN block | IF LPAREN expr RPAREN block

View File

@ -1,5 +1,9 @@
func FUNC_1(a, b, c) { func FUNC_1(a, b, c) {
for (i = 1; i < 3; i = i + 1) {
print x;
}
x = x + 1; x = x + 1;
a = a + 1; a = a + 1;
} }