From 85405e614aee98e2c4d56ac69d08d002c6ec6b74 Mon Sep 17 00:00:00 2001 From: serr Date: Tue, 20 May 2025 20:28:01 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BB?= =?UTF-8?q?=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D0=BB=D1=8B=20=D0=BC=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B2=D0=B0,=20=D1=82=D0=B8=D0=BF=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B8=D0=B2=D0=B0,=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20=D1=8D=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D1=83=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzers/test/test.l | 2 ++ analyzers/test/test.y | 43 +++++++++++++++++++++++++++-- tests/main.go | 37 ++++++++++++------------- tests/test_arrays.txt | 22 +++++++++++++++ tests/test_blocks.txt | 2 +- tests/test_funcs.txt | 3 ++ tests/test_multiple_declaration.txt | 4 +++ 7 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 tests/test_arrays.txt diff --git a/analyzers/test/test.l b/analyzers/test/test.l index bb92f84..b85db85 100644 --- a/analyzers/test/test.l +++ b/analyzers/test/test.l @@ -100,6 +100,8 @@ LETTER_OR_DIGIT [a-zA-Z0-9_] "}" { return RBRACE; } "(" { return LPAREN; } ")" { return RPAREN; } +"[" { return LBRACK; } +"]" { return RBRACK; } "," { return COMMA; } ";" { return SEMICOLON; } "..." { return DOTS; } diff --git a/analyzers/test/test.y b/analyzers/test/test.y index cc6ea8a..9b851b9 100644 --- a/analyzers/test/test.y +++ b/analyzers/test/test.y @@ -21,7 +21,7 @@ void free_node(char *str) { double num; } -%token SHORT_DECLARATION LBRACE RBRACE SEMICOLON ASSIGN LPAREN RPAREN COMMA COLON DOTS +%token SHORT_DECLARATION LBRACE RBRACE LBRACK RBRACK SEMICOLON ASSIGN LPAREN RPAREN COMMA COLON DOTS %token VAR FUNC RETURN STRING_LITERAL FLOAT_LITERAL COMPLEX_LITERAL NUMBER PACKAGE IMPORT %token INC DEC PLUS_EQ MINUS_EQ MUL_EQ DIV_EQ MOD_EQ %token AMPERSAND_EQ PIPE_EQ XOR_EQ LSHIFT_EQ RSHIFT_EQ AND_NOT_EQ @@ -66,6 +66,9 @@ statement: { printf("\033[1;33mSTATEMENT: variable declaration\033[0m\n"); } | var_multiple_short_declaration SEMICOLON { printf("\033[1;33mSTATEMENT: variable multiple declaration\033[0m\n"); } + | any_identifier ASSIGN math_expr SEMICOLON + | any_identifier ASSIGN literal SEMICOLON + | var_multiple_short_assignment SEMICOLON | func_declaration { printf("\033[1;33mSTATEMENT: function declaration\033[0m\n"); } | type_delcaration SEMICOLON @@ -85,13 +88,14 @@ block: any_identifier: IDENTIFIER | WITH_DOT_IDENTIFIER + | arr_element // // lists statements_list: + | statements_list expr SEMICOLON | statements_list statement | statements_list block - | statements_list expr SEMICOLON ; identifiers_list: @@ -99,7 +103,7 @@ identifiers_list: | identifiers_list COMMA IDENTIFIER { } any_identifiers_list: - | any_identifier { } + any_identifier { } | any_identifiers_list COMMA any_identifier { } math_expr_or_literals_list: @@ -114,6 +118,10 @@ math_expr_or_literals_list: math_expr_or_literals_list_or_empty: | math_expr_or_literals_list +literals_list: + literal + | literals_list COMMA literal + types_list: | type | types_list COMMA type @@ -293,6 +301,7 @@ func_types: type: int_types { } + | arr_types { } | struct_types { } | func_types { } | string_types { } @@ -307,6 +316,7 @@ type: // literals literal: STRING_LITERAL { } + | arr_literal { } | struct_literal { } | func_literal { } | COMPLEX_LITERAL { } @@ -339,6 +349,28 @@ import_list: ; // +// arrays + +// для случая arr5 := [4]int{1: 42, 3: 99}; с частичным указанием значений +arr_field: + NUMBER COLON math_expr + | NUMBER COLON literal + +arr_field_list: + | arr_field + | arr_field_list COMMA arr_field + +arr_literal: + LBRACK NUMBER RBRACK type LBRACE math_expr_or_literals_list_or_empty RBRACE + | LBRACK DOTS RBRACK type LBRACE math_expr_or_literals_list_or_empty RBRACE + | LBRACK NUMBER RBRACK type LBRACE arr_field_list RBRACE + +arr_types: + LBRACK NUMBER RBRACK type + +arr_element: + IDENTIFIER LBRACK NUMBER RBRACK + // functions func_literal: @@ -395,6 +427,11 @@ struct_literal: | any_identifier LBRACE math_expr_or_literals_list RBRACE { printf("STRUCT LITERAL\n"); } // +// assignment +var_multiple_short_assignment: + identifiers_list ASSIGN math_expr_or_literals_list + + // vars decl var_multiple_short_declaration: identifiers_list SHORT_DECLARATION math_expr_or_literals_list diff --git a/tests/main.go b/tests/main.go index b39b6ce..358ca98 100644 --- a/tests/main.go +++ b/tests/main.go @@ -1,10 +1,5 @@ package main -import ( - "fmt" - "log" -) - type person struct { name, second_name string age int @@ -27,23 +22,25 @@ func ujas() (func() func() func() int, int, string) { }, 1, "hello world" } func main() { - a := "Tom" + var a, b int + a, b = 1, 1 - fmt.Println(a) - arr := []int{1, 2, 3} - for idx, val := range arr { - fmt.Println(idx, val) - } + // a := "Tom" + // fmt.Println(a) + // arr := []int{1, 2, 3} + // for idx, val := range arr { + // fmt.Println(idx, val) + // } - defer func() { - if err := recover(); err != nil { - log.Println("work failed:", err) - } - }() + // defer func() { + // if err := recover(); err != nil { + // log.Println("work failed:", err) + // } + // }() - func(msg string) { - fmt.Println("Message:", msg) - }("Hello, IIFE!") + // func(msg string) { + // fmt.Println("Message:", msg) + // }("Hello, IIFE!") - return + // return } diff --git a/tests/test_arrays.txt b/tests/test_arrays.txt new file mode 100644 index 0000000..092070b --- /dev/null +++ b/tests/test_arrays.txt @@ -0,0 +1,22 @@ +package main; + +func main() { + // Явное указание размера и значений + arr := [4]int{3, 2, 5, 4}; + // Автовычисление размера + arr := [...]int{3, 2, 5, 4}; + // Частичное определение: все не определенные элементы - zero-values + arr8 := [3]int{}; + arr6 := [3]bool{true}; + arr5 := [4]int{1: 42, 3: 99}; + // + var arr7 [4]int; + arr7 = [4]int{1,2,3,4}; + arr7, arr8 = [4]int{1,2,3,4}, [4]int{1,2,3,4}; + // Изменение элемента массива + arr[1]; + arr[1] = 2+2*2; + arr[1] = [4]int{3, 2, 5, 4}; + arr[1] = "test"; + +} \ No newline at end of file diff --git a/tests/test_blocks.txt b/tests/test_blocks.txt index 332f1ff..64be4f6 100644 --- a/tests/test_blocks.txt +++ b/tests/test_blocks.txt @@ -57,7 +57,7 @@ func main() { } for i := 10; i > 0; i-- { } a = 1; - a = s; + a = test; s := "test string"; diff --git a/tests/test_funcs.txt b/tests/test_funcs.txt index c676f61..3395537 100644 --- a/tests/test_funcs.txt +++ b/tests/test_funcs.txt @@ -15,6 +15,9 @@ func iife(work int) { log.SetPrefix(fmt.Sprintf("%s | ", app.Cfg.ServerDomain)); + cache := candycache.Cacher(10 * time.Minute); + cache.Set("key7", -2.1231, 10*time.Minute); + router := setupRoutes(app); if app, err = models.InitApp(); err != nil { diff --git a/tests/test_multiple_declaration.txt b/tests/test_multiple_declaration.txt index b2a097d..2698c51 100644 --- a/tests/test_multiple_declaration.txt +++ b/tests/test_multiple_declaration.txt @@ -16,6 +16,10 @@ func test() { var a, b, c, d = 1,2,3,4; var a, b, c, d int = 1,2,3,4; + a = 123; + + s = "string"; + a, b := 1, 2; // декларация нескольких переменных (целые литералы) a, b := b, a; // swap a, b := "hello", "sailor"; // декларация нескольких переменных (строковые литералы)