From ebf1922c90ec1238a073020b019ee8cac2fd3c14 Mon Sep 17 00:00:00 2001 From: serr Date: Sun, 18 May 2025 19:19:54 +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=BB=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D0=BB=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20aka=20=D0=B0=D0=BD=D0=BE=D0=BD=D0=B8=D0=BC=D0=BD=D1=83?= =?UTF-8?q?=D1=8E=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzers/test/test.y | 8 ++++++++ tests/test.txt | 2 ++ tests/test_funcs.txt | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/test_funcs.txt diff --git a/analyzers/test/test.y b/analyzers/test/test.y index 6540b32..a787a31 100644 --- a/analyzers/test/test.y +++ b/analyzers/test/test.y @@ -285,6 +285,7 @@ type: // literals literal: STRING_LITERAL { } + | func_literal { } | COMPLEX_LITERAL { } | BOOL_LITERAL { } | FLOAT_LITERAL { } @@ -316,6 +317,13 @@ import_list: // // 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"); } + func_call: any_identifier LPAREN math_expr_or_literals_list_or_empty RPAREN diff --git a/tests/test.txt b/tests/test.txt index c0e7812..cc409f9 100644 --- a/tests/test.txt +++ b/tests/test.txt @@ -34,6 +34,8 @@ func test(a int, b string) { func main() { + CubeRoot(123.1); + var a int; var c complex128 = 555.12i+1.123i; diff --git a/tests/test_funcs.txt b/tests/test_funcs.txt new file mode 100644 index 0000000..0720738 --- /dev/null +++ b/tests/test_funcs.txt @@ -0,0 +1,34 @@ +package main; + +import "fmt"; + +// анонимные функции +func anon_func() { + // Присваиваем анонимную функцию переменной + square := func(x int) int { + return x * x; + }; + + fmt.Println(square(5)); // Выведет: 25 +} + +// замыкания +func intSeq() func() int { + i := 0; + return func() int { + i++; + return i; + }; +} + +func main() { + + nextInt := intSeq(); + + fmt.Println(nextInt()); + fmt.Println(nextInt()); + fmt.Println(nextInt()); + + newInts := intSeq(); + fmt.Println(newInts()); +} \ No newline at end of file