добавил литерал функциональный aka анонимную функцию

master2
serr 2025-05-18 19:19:54 +03:00
parent be95ad05e3
commit ebf1922c90
3 changed files with 44 additions and 0 deletions

View File

@ -285,6 +285,7 @@ type:
// literals // literals
literal: literal:
STRING_LITERAL { } STRING_LITERAL { }
| func_literal { }
| COMPLEX_LITERAL { } | COMPLEX_LITERAL { }
| BOOL_LITERAL { } | BOOL_LITERAL { }
| FLOAT_LITERAL { } | FLOAT_LITERAL { }
@ -316,6 +317,13 @@ import_list:
// //
// functions // 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: func_call:
any_identifier LPAREN math_expr_or_literals_list_or_empty RPAREN any_identifier LPAREN math_expr_or_literals_list_or_empty RPAREN

View File

@ -34,6 +34,8 @@ func test(a int, b string) {
func main() { func main() {
CubeRoot(123.1);
var a int; var a int;
var c complex128 = 555.12i+1.123i; var c complex128 = 555.12i+1.123i;

34
tests/test_funcs.txt Normal file
View File

@ -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());
}