From faf37642dcfdf38fbe40d0e719c6b07b24c7fb73 Mon Sep 17 00:00:00 2001 From: serr Date: Tue, 20 May 2025 23:07:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzers/test/test.y | 33 +++++++++++++++++++++++++-------- tests/test_funcs.txt | 6 +++++- tests/test_methods.txt | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 tests/test_methods.txt diff --git a/analyzers/test/test.y b/analyzers/test/test.y index cdfbd82..edc9801 100644 --- a/analyzers/test/test.y +++ b/analyzers/test/test.y @@ -58,6 +58,7 @@ program: ; statement: + | method_declaration | GO func_call SEMICOLON | DEFER func_call SEMICOLON | func_call SEMICOLON @@ -306,7 +307,9 @@ func_types: FUNC LPAREN arg_list RPAREN return_type type: + int_types { } + | IDENTIFIER | chan_types | map_types | slice_types @@ -391,19 +394,25 @@ slice_types: // functions -func_literal: - FUNC - { printf("\033[1;35mHELLO, ANON FUNC\n\033[0m"); } - LPAREN arg_list RPAREN return_type block - { printf("\033[1;35mBY, ANON FUNC\n\n\033[0m"); } +// Анонимная функция +anon_func: + FUNC LPAREN arg_list RPAREN return_type block + { printf("\033[1;35mANONYMOUS FUNC\n\033[0m"); } + ; +// Литерал функции (если нужно для других правил) +func_literal: anon_func; + +// Вызов функции func_call: any_identifier LPAREN math_expr_or_literals_list_or_empty RPAREN - | func_literal LPAREN math_expr_or_literals_list_or_empty RPAREN + | anon_func LPAREN math_expr_or_literals_list_or_empty RPAREN + | anon_func // Позволяет использовать func(){} как значение + ; arg_declaration: identifiers_list type - { printf("\033[1;35mARG DECLARATIONS:\n\033[0m"); } + { printf("\033[1;35mARG DECLARATIONS\n\033[0m"); } ; arg_list: @@ -423,7 +432,15 @@ func_declaration: LPAREN arg_list RPAREN return_type block { printf("\033[1;35mBY, FUNC: %s\n\n\033[0m", $2); } ; -// + +// Объявление метода +method_declaration: + FUNC LPAREN IDENTIFIER type RPAREN IDENTIFIER + { printf("\033[1;35mMETHOD DECL: %s\n\033[0m", $6); } + LPAREN arg_list RPAREN return_type block + { printf("\033[1;35mEND METHOD\n\033[0m"); } + ; + // map map_field: diff --git a/tests/test_funcs.txt b/tests/test_funcs.txt index 3de95f2..e12db80 100644 --- a/tests/test_funcs.txt +++ b/tests/test_funcs.txt @@ -37,8 +37,12 @@ func iife(work int) { for i := 1; i < 3; i++ { + + go func(a int) { + fmt.Println(123); + }(); + func(){}(); - func(){fmt.Println(123);}(); go func(a, b string) { fmt.Println("Message:", a + b); diff --git a/tests/test_methods.txt b/tests/test_methods.txt new file mode 100644 index 0000000..08f19f0 --- /dev/null +++ b/tests/test_methods.txt @@ -0,0 +1,24 @@ +package main; + +// Circle - структура, представляющая круг +type Circle struct { + Radius float64 +}; + +// Реализация методов интерфейса Geometry для Circle +func (c Circle) Area() float64 { + return math.Pi * c.Radius * c.Radius; +} + +func (c Circle) Perimeter() float64 { + return 2 * math.Pi * c.Radius; +} + +func (c Circle) Name() string { + return "Круг"; +} + +func main() { + + +} \ No newline at end of file