добавил разные способы декларации пользовательских типов структур и обращение к полю структуры

master2
serr 2025-05-20 17:45:47 +03:00
parent dbe1ff7d32
commit ac720e3239
3 changed files with 50 additions and 28 deletions

View File

@ -83,7 +83,7 @@ block:
; ;
any_identifier: any_identifier:
IDENTIFIER IDENTIFIER
| WITH_DOT_IDENTIFIER | WITH_DOT_IDENTIFIER
// //
@ -203,19 +203,19 @@ expr:
; ;
math_expr: math_expr:
IDENTIFIER ASSIGN math_expr { } any_identifier ASSIGN math_expr { }
| IDENTIFIER PLUS_EQ math_expr | any_identifier PLUS_EQ math_expr
| IDENTIFIER MINUS_EQ math_expr | any_identifier MINUS_EQ math_expr
| IDENTIFIER MUL_EQ math_expr | any_identifier MUL_EQ math_expr
| IDENTIFIER DIV_EQ math_expr | any_identifier DIV_EQ math_expr
| IDENTIFIER MOD_EQ math_expr | any_identifier MOD_EQ math_expr
| IDENTIFIER AMPERSAND_EQ math_expr | any_identifier AMPERSAND_EQ math_expr
| IDENTIFIER PIPE_EQ math_expr | any_identifier PIPE_EQ math_expr
| IDENTIFIER XOR_EQ math_expr | any_identifier XOR_EQ math_expr
| IDENTIFIER LSHIFT_EQ math_expr | any_identifier LSHIFT_EQ math_expr
| IDENTIFIER RSHIFT_EQ math_expr | any_identifier RSHIFT_EQ math_expr
| IDENTIFIER AND_NOT_EQ math_expr | any_identifier AND_NOT_EQ math_expr
| math_expr PLUS math_expr { printf("PLUS\n"); } | math_expr PLUS math_expr { printf("PLUS\n"); }
| math_expr MINUS math_expr { printf("MINUS\n"); } | math_expr MINUS math_expr { printf("MINUS\n"); }
@ -227,9 +227,9 @@ math_expr:
| NUMBER { printf("NUMBER\n"); } | NUMBER { printf("NUMBER\n"); }
| FLOAT_LITERAL { printf("FLOAT LITERAL\n"); } | FLOAT_LITERAL { printf("FLOAT LITERAL\n"); }
| COMPLEX_LITERAL { printf("COMPLEX LITERAL\n"); } | COMPLEX_LITERAL { printf("COMPLEX LITERAL\n"); }
| IDENTIFIER { printf("IDENTIFIER: %s\n", $1); } | any_identifier { printf("IDENTIFIER\n"); }
| IDENTIFIER INC { printf("POST-INCREMENT: %s++\n", $1); } | any_identifier INC { printf("POST-INCREMENT: ++\n"); }
| IDENTIFIER DEC { printf("POST-DECREMENT: %s--\n", $1); } | any_identifier DEC { printf("POST-DECREMENT: --\n"); }
| func_call { printf("FUNCTION CALL IN EXPR\n"); } | func_call { printf("FUNCTION CALL IN EXPR\n"); }
; ;
@ -386,7 +386,7 @@ field_list:
| field_list COMMA field | field_list COMMA field
struct_literal: struct_literal:
IDENTIFIER LBRACE field_list RBRACE { printf("STRUCT LITERAL\n"); } any_identifier LBRACE field_list RBRACE { printf("STRUCT LITERAL\n"); }
// //
// vars decl // vars decl
@ -394,9 +394,14 @@ var_multiple_short_declaration:
identifiers_list SHORT_DECLARATION math_expr_or_literals_list identifiers_list SHORT_DECLARATION math_expr_or_literals_list
var_declaration: var_declaration:
VAR IDENTIFIER type { { printf("\033[1;33mVAR DECL without init value: %s\n\033[0m", $2); } } VAR IDENTIFIER any_identifier { { printf("\033[1;33mVAR DECL without init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN math_expr { { printf("\033[1;33mVAR DECL with math expr init value: %s\n\033[0m", $2); } } | VAR IDENTIFIER type { { printf("\033[1;33mVAR DECL without init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN literal { { printf("\033[1;33mVAR DECL with literal init value: %s\n\033[0m", $2); } } | VAR IDENTIFIER ASSIGN math_expr { { printf("\033[1;33mVAR DECL with math expr init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER ASSIGN literal { { printf("\033[1;33mVAR DECL with literal init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER any_identifier ASSIGN math_expr { { printf("\033[1;33mVAR DECL with type and math expr init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER any_identifier ASSIGN literal { { printf("\033[1;33mVAR DECL with type and literal init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN math_expr { { printf("\033[1;33mVAR DECL with type and math expr init value: %s\n\033[0m", $2); } }
| VAR IDENTIFIER type ASSIGN literal { { printf("\033[1;33mVAR DECL with type and literal init value: %s\n\033[0m", $2); } }
; ;
// type decl // type decl

View File

@ -29,9 +29,7 @@ func ujas() (func() func() func() int, int, string) {
func main() { func main() {
a := "Tom" a := "Tom"
var tom = person{name: a, age: 24}
a := 1e6
fmt.Println(a) fmt.Println(a)
arr := []int{1, 2, 3} arr := []int{1, 2, 3}
for idx, val := range arr { for idx, val := range arr {

View File

@ -1,5 +1,10 @@
package main; package main;
import "fmt"; import (
"fmt";
"your_project/models";
)
type mile uint; type mile uint;
type BinaryOp func(int, int) int; type BinaryOp func(int, int) int;
@ -8,15 +13,29 @@ type empty struct{
}; };
type person struct{ type person struct{
name, second_name string; name string;
age int; age int
work func()
}; };
func main() { func main() {
d := "Debilov"; undefined := person {};
tom := person {name: "Tom", second_name: d, age: 20+2*2};
var alice person = person{age: 23, name: "Alice"};
var tom = person {name: "Tom", age: 24};
var tom1 Person = Person{Name: "Tom", Age: 24};
tom2 := Person{Name: "Tom", Age: 24};
// Явное указание типа с пакетом
var tom3 models.Person = models.Person{Name: "Tom", Age: 24};
// Краткая форма
tom4 := models.Person{Name: "Tom", Age: 24};
// Вывод типа
var tom5 = models.Person{Name: "Tom", Age: 24};
fmt.Println(tom.name); // Tom
fmt.Println(tom.age); // 24
tom.age = 38; // изменяем значение
fmt.Println(tom.name, tom.age); // Tom 38
} }