diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index fab9d620c9..dd1bacdde4 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -269,7 +269,8 @@ pub fn (a array) reverse() array { data: calloc(a.cap * a.element_size) } for i := 0; i < a.len; i++ { - C.memcpy(arr.data + i * arr.element_size, &a[a.len - 1 - i], arr.element_size) + C.memcpy(arr.data + i * arr.element_size, + &a[a.len - 1 - i], arr.element_size) } return arr } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 26be1f1f70..08a667106e 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -9,7 +9,7 @@ import ( ) pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | -FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr | MethodCallExpr +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | ForStmt | StructDecl | ForCStmt | ForInStmt @@ -191,6 +191,13 @@ pub: right Expr } +pub struct IndexExpr { +pub: +// op token.Kind + left Expr + index Expr +} + pub struct IfExpr { pub: tok_kind token.Kind diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 55c7898441..cf7dff44d9 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -239,6 +239,12 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('.') g.write(it.field) } + ast.IndexExpr { + g.expr(it.left) + g.write('[') + g.expr(it.index) + g.write(']') + } ast.IfExpr { // If expression? Assign the value to a temp var. // Previously ?: was used, but it's too unreliable. diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index cdd73e8154..7889d0e822 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -27,6 +27,10 @@ void foo(int a) { i < 10; i++; ) { } + int nums = new_array_from_c_array(3, 3, sizeof(int), { + 1, 2, 3, + }); + int number = nums[0]; void n = get_int2(); } diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index 4fc4cea9de..90c0bc9de4 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -30,6 +30,8 @@ fn foo(a int) { for i := 0; i < 10; i++ { } + nums := [1,2,3] + number := nums[0] n := get_int2() } diff --git a/vlib/v/parser/a_type.v b/vlib/v/parser/a_type.v index 71bc023f2d..ccaaa3c79f 100644 --- a/vlib/v/parser/a_type.v +++ b/vlib/v/parser/a_type.v @@ -134,7 +134,7 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent { return types.new_builtin_ti(._f32, nr_muls) } 'f64' { - return types.new_builtin_ti(._f64, nr_muls) + return types.new_builtin_ti(.f64, nr_muls) } 'string' { return types.new_builtin_ti(._string, nr_muls) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e2c25d9ad2..e4fe424893 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -320,12 +320,12 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) { node,ti = p.string_expr() } // -1, -a etc - .minus { + .minus, .amp { node,ti = p.prefix_expr() } - .amp { - p.next() - } + // .amp { + // p.next() + // } .key_true, .key_false { node = ast.BoolLiteral{ val: p.tok.kind == .key_true @@ -359,9 +359,13 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) { else if p.tok.kind == .dot { node,ti = p.dot_expr(node) } + else if p.tok.kind == .lsbr { + node,ti = p.index_expr(node) + } else if p.tok.kind.is_infix() { node,ti = p.infix_expr(node) } + // Postfix else if p.tok.kind in [.inc, .dec] { node = ast.PostfixExpr{ op: p.tok.kind @@ -389,6 +393,23 @@ fn (p mut Parser) prefix_expr() (ast.Expr,types.TypeIdent) { return expr,ti } +fn (p mut Parser) index_expr(left ast.Expr) (ast.Expr,types.TypeIdent) { + // println('index expr$p.tok.str() line=$p.tok.line_nr') + p.next() + println('start expr') + index,_ := p.expr(0) + println('end expr') + p.check(.rsbr) + println('got ]') + ti := types.int_ti + mut node := ast.Expr{} + node = ast.IndexExpr{ + left: left + index: index + } + return node,ti +} + fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) { p.next() field_name := p.check_name() @@ -571,7 +592,7 @@ fn (p mut Parser) if_expr() (ast.Expr,types.TypeIdent) { else_stmts: else_stmts ti: ti // left: left - + } p.inside_if = false return node,ti @@ -636,7 +657,7 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.TypeIdent) { // val: lit.f64() val: lit } - ti = types.new_builtin_ti(._f64, 0) + ti = types.new_builtin_ti(.f64, 0) } else { node = ast.IntegerLiteral{ @@ -770,7 +791,7 @@ fn (p mut Parser) var_decl() ast.VarDecl { return ast.VarDecl{ name: name expr: expr // p.expr(token.lowest_prec) - + ti: ti } } diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 66a0f559bd..63dbe019ce 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -358,6 +358,9 @@ const ( // precedence returns a tokens precedence if defined, otherwise lowest_prec pub fn (tok Token) precedence() int { match tok.kind { + .lsbr { + return 9 + } .dot { return 8 } diff --git a/vlib/v/types/types.v b/vlib/v/types/types.v index 8308168a55..2da0e32b0c 100644 --- a/vlib/v/types/types.v +++ b/vlib/v/types/types.v @@ -21,7 +21,7 @@ pub enum Kind { _u32, _u64, _f32, - _f64, + f64, _string, _char, _bool, @@ -32,7 +32,7 @@ pub enum Kind { _variadic } -pub type Type = Placeholder | Void | Voidptr | Charptr | Byteptr | Const | Enum | Struct | +pub type Type = Placeholder | Void | Voidptr | Charptr | Byteptr | Const | Enum | Struct | Int | Float | String | Char | Byte | Bool | Array | ArrayFixed | Map | MultiReturn | Variadic pub struct TypeIdent { @@ -75,7 +75,7 @@ pub fn (ti &TypeIdent) is_int() bool { [inline] pub fn (ti &TypeIdent) is_float() bool { - return ti.kind in [._f32, ._f64] + return ti.kind in [._f32, .f64] } [inline] @@ -145,7 +145,7 @@ pub fn (k Kind) str() string { ._f32{ 'f32' } - ._f64{ + .f64{ 'f64' } ._string{