добавил методы

master2
serr 2025-05-20 23:07:26 +03:00
parent 173943589a
commit faf37642dc
3 changed files with 54 additions and 9 deletions

View File

@ -58,6 +58,7 @@ program:
; ;
statement: statement:
| method_declaration
| GO func_call SEMICOLON | GO func_call SEMICOLON
| DEFER func_call SEMICOLON | DEFER func_call SEMICOLON
| func_call SEMICOLON | func_call SEMICOLON
@ -306,7 +307,9 @@ func_types:
FUNC LPAREN arg_list RPAREN return_type FUNC LPAREN arg_list RPAREN return_type
type: type:
int_types { } int_types { }
| IDENTIFIER
| chan_types | chan_types
| map_types | map_types
| slice_types | slice_types
@ -391,19 +394,25 @@ slice_types:
// functions // functions
func_literal: // Анонимная функция
FUNC anon_func:
{ printf("\033[1;35mHELLO, ANON FUNC\n\033[0m"); } FUNC LPAREN arg_list RPAREN return_type block
LPAREN arg_list RPAREN return_type block { printf("\033[1;35mANONYMOUS FUNC\n\033[0m"); }
{ printf("\033[1;35mBY, ANON FUNC\n\n\033[0m"); } ;
// Литерал функции (если нужно для других правил)
func_literal: anon_func;
// Вызов функции
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
| 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: arg_declaration:
identifiers_list type identifiers_list type
{ printf("\033[1;35mARG DECLARATIONS:\n\033[0m"); } { printf("\033[1;35mARG DECLARATIONS\n\033[0m"); }
; ;
arg_list: arg_list:
@ -423,7 +432,15 @@ func_declaration:
LPAREN arg_list RPAREN return_type block LPAREN arg_list RPAREN return_type block
{ printf("\033[1;35mBY, FUNC: %s\n\n\033[0m", $2); } { 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
map_field: map_field:

View File

@ -37,8 +37,12 @@ func iife(work int) {
for i := 1; i < 3; i++ { for i := 1; i < 3; i++ {
go func(a int) {
fmt.Println(123);
}();
func(){}(); func(){}();
func(){fmt.Println(123);}();
go func(a, b string) { go func(a, b string) {
fmt.Println("Message:", a + b); fmt.Println("Message:", a + b);

24
tests/test_methods.txt Normal file
View File

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