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

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

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

View File

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

View File

@ -1,5 +1,10 @@
package main;
import "fmt";
import (
"fmt";
"your_project/models";
)
type mile uint;
type BinaryOp func(int, int) int;
@ -8,15 +13,29 @@ type empty struct{
};
type person struct{
name, second_name string;
age int;
work func()
name string;
age int
};
func main() {
d := "Debilov";
tom := person {name: "Tom", second_name: d, age: 20+2*2};
undefined := person {};
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
}