funcs added

master
serr 2025-04-01 20:29:56 +03:00
parent 2841810eba
commit aaa21812f3
3 changed files with 37 additions and 3 deletions

View File

@ -9,8 +9,6 @@ void yyerror(const char *s) {
line_number,
s,
yytext);
exit(1);
}
%}
@ -36,6 +34,9 @@ void yyerror(const char *s) {
"if" { return IF; }
"else" { return ELSE; }
"func" { return FUNC; }
"," { return COMMA; }
"//" {
int c;
while ((c = input()) != '\n' && c != 0);

View File

@ -17,8 +17,9 @@ bool debug = false; // debug mode
%token <str> IDENTIFIER NUMBER
%token LBRACE RBRACE LPAREN RPAREN SEMICOLON ASSIGN PLUS MINUS MULT DIV MOD RET PRINT WHILE IF ELSE AND OR NOT
%token FUNC COMMA
%type <str> expr program statement block
%type <str> expr program statement block func_decl param_list params
%left OR
%left AND
@ -55,6 +56,28 @@ statement:
{ if (debug) printf("\033[1;34mELSE BLOCK STARTED\033[0m\n"); }
block
{ if (debug) printf("\033[1;34mELSE BLOCK ENDED\033[0m\n"); }
| func_decl
;
func_decl:
FUNC IDENTIFIER {
if (debug) printf("\033[1;35mFUNCTION: %s\033[0m\n", $2);
free($2);
}
LPAREN param_list RPAREN
{ if (debug) printf("\033[1;35mFUNCTION PARAMS END\033[0m\n"); }
block
{ if (debug) printf("\033[1;35mFUNCTION END\033[0m\n"); }
;
param_list:
| params
;
params:
IDENTIFIER { if (debug) printf("\033[1;35mPARAM: %s\033[0m\n", $1); free($1); }
| params COMMA IDENTIFIER { if (debug) printf("\033[1;35mPARAM: %s\033[0m\n", $3); free($3); }
;
// Блок - { program }, т.е. это последовательность утверждений и она находится в скобках { }

View File

@ -1,3 +1,13 @@
func FUNC_1(a, b, c) {
x = x + 1;
a = a + 1;
}
func FUNC_2() {
x = 5;
}
func main()
{
// 123123132
/* test */