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