сделал литералы массива, тип массива, обращение к элементу массива
parent
3c31489253
commit
85405e614a
|
@ -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; }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println("work failed:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
func(msg string) {
|
||||
fmt.Println("Message:", msg)
|
||||
}("Hello, IIFE!")
|
||||
|
||||
return
|
||||
// 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)
|
||||
// }
|
||||
// }()
|
||||
|
||||
// func(msg string) {
|
||||
// fmt.Println("Message:", msg)
|
||||
// }("Hello, IIFE!")
|
||||
|
||||
// return
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
||||
}
|
|
@ -57,7 +57,7 @@ func main() {
|
|||
}
|
||||
for i := 10; i > 0; i-- { }
|
||||
a = 1;
|
||||
a = s;
|
||||
a = test;
|
||||
|
||||
s := "test string";
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"; // декларация нескольких переменных (строковые литералы)
|
||||
|
|
Loading…
Reference in New Issue