From 2973ff1f2f04c053e73644d8deb3218101a0f829 Mon Sep 17 00:00:00 2001 From: serr Date: Sun, 18 May 2025 17:55:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20FR?= =?UTF-8?q?OM=5FPACKAGE=5FIDENTIFIER=20-=20=D0=BE=D0=B1=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20=D1=87=D0=B5=D0=BC=D1=83=20?= =?UTF-8?q?=D1=82=D0=BE=20=D0=B2=20=D0=BA=D0=B0=D0=BA=D0=BE=D0=BC=20=D1=82?= =?UTF-8?q?=D0=BE=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=B5=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=20=D1=87=D1=82=D0=BE=20=D0=B1=D0=B5=D1=80=D0=B5=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D0=B8=D0=B7=20=D0=B4=D1=80=D1=83=D0=B3=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=B0=20=D0=B2?= =?UTF-8?q?=D1=8B=D0=B3=D0=BB=D1=8F=D0=B4=D0=B8=D1=82=20=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=20<=D0=B8=D0=BC=D1=8F=5F=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=B0.?= =?UTF-8?q?=D0=9D=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5>,=20=D0=B3?= =?UTF-8?q?=D0=B4=D0=B5=20=D0=9D=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5=D0=B3=D0=B4=D0=B0=20=D1=81=20=D0=B1=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D1=88=D0=BE=D0=B9=20=D0=B1=D1=83=D0=BA=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzers/test/test.l | 15 ++++++++++----- analyzers/test/test.y | 14 +++++++++----- tests/main.go | 16 ++++------------ tests/test_func_calls.txt | 14 ++++++++++++++ tests/test_multiple_declaration.txt | 6 +++++- 5 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 tests/test_func_calls.txt diff --git a/analyzers/test/test.l b/analyzers/test/test.l index 08523ed..ab16484 100644 --- a/analyzers/test/test.l +++ b/analyzers/test/test.l @@ -13,9 +13,10 @@ void yyerror(const char *s) { %} -DIGIT [0-9] -LETTER [a-zA-Z_] -LETTER_OR_DIGIT [a-zA-Z0-9_] +DIGIT [0-9] +BIG_LETTER [A-Z] +LETTER [a-zA-Z_] +LETTER_OR_DIGIT [a-zA-Z0-9_] %% @@ -117,9 +118,13 @@ LETTER_OR_DIGIT [a-zA-Z0-9_] return STRING_LITERAL; } -{LETTER}{LETTER_OR_DIGIT}* { +{LETTER}{LETTER_OR_DIGIT}*(\.{BIG_LETTER}{LETTER_OR_DIGIT}*)? { yylval.str = strdup(yytext); - return IDENTIFIER; + if (strchr(yytext, '.') != NULL) { + return FROM_PACKAGE_IDENTIFIER; + } else { + return IDENTIFIER; + } } [0-9]+\.[0-9]+ { diff --git a/analyzers/test/test.y b/analyzers/test/test.y index 71be3fc..f14175c 100644 --- a/analyzers/test/test.y +++ b/analyzers/test/test.y @@ -34,7 +34,7 @@ void free_node(char *str) { %token RUNE BYTE BOOL_LITERAL %token FLOAT32 FLOAT64 %token COMPLEX64 COMPLEX128 -%token IDENTIFIER +%token IDENTIFIER FROM_PACKAGE_IDENTIFIER %token AND OR NOT EQ NEQ LT GT LEQ GEQ %left PLUS_EQ MINUS_EQ MUL_EQ DIV_EQ MOD_EQ @@ -92,14 +92,17 @@ identifiers_list: | identifiers_list COMMA IDENTIFIER { } math_expr_or_literals_list: - literal { } + literal { } + | math_expr { } + | func_call { } | math_expr_or_literals_list COMMA literal - - | math_expr { } | math_expr_or_literals_list COMMA math_expr + | math_expr_or_literals_list COMMA func_call + ; math_expr_or_literals_list_or_empty: | math_expr_or_literals_list + // // condition @@ -199,6 +202,7 @@ math_expr: | IDENTIFIER { printf("IDENTIFIER: %s\n", $1); } | IDENTIFIER INC { printf("POST-INCREMENT: %s++\n", $1); } | IDENTIFIER DEC { printf("POST-DECREMENT: %s--\n", $1); } + | func_call { printf("FUNCTION CALL IN EXPR\n"); } ; log_expr: @@ -285,9 +289,9 @@ import_list: // // functions - func_call: IDENTIFIER LPAREN math_expr_or_literals_list_or_empty RPAREN + | FROM_PACKAGE_IDENTIFIER LPAREN math_expr_or_literals_list_or_empty RPAREN arg_declaration: IDENTIFIER type diff --git a/tests/main.go b/tests/main.go index 160371e..42e9ed7 100644 --- a/tests/main.go +++ b/tests/main.go @@ -1,17 +1,9 @@ package main -import "fmt" - -const ( - a = "123123" - b = "123123" -) +func test(a int) int { + return a +} func main() { - - for k, l := 0, 10; k < l; k, l = k+1, l-1 { - } - - fmt.Println(a, b) - + panic(test(test(1)*2 + 1)) } diff --git a/tests/test_func_calls.txt b/tests/test_func_calls.txt new file mode 100644 index 0000000..68f8dbc --- /dev/null +++ b/tests/test_func_calls.txt @@ -0,0 +1,14 @@ +package main; + +import ( + "fmt"; +) + +func test(a int) int { + return a; +} + +func main() { + a := test(1); // вызов функции обычной + fmt.Println(test(test(1)*2+3 - a)); // вызов функции из fmt +} diff --git a/tests/test_multiple_declaration.txt b/tests/test_multiple_declaration.txt index ff8640e..99dea9e 100644 --- a/tests/test_multiple_declaration.txt +++ b/tests/test_multiple_declaration.txt @@ -1,5 +1,9 @@ package main; +func dva() { + return 2; +} + func test() { a := 1; // декларация одной переменной (число) @@ -8,7 +12,7 @@ func test() { a, b := 1, 2; // декларация нескольких переменных (целые литералы) a, b := b, a; // swap a, b := "hello", "sailor"; // декларация нескольких переменных (строковые литералы) - a, b, c, d := "hello", 2, 3.123, true; // декларация нескольких переменных (всякое) + a, b, c, d, v := "hello", 2, 3.123, true, dva(); // декларация нескольких переменных (всякое) // цикл с множественной декларацией и множественным присвоением for k, l := 0, 10; k < l; k, l = k+1, l-1 {