From 2d597d78048aca2df32097f2d00ef584251675f5 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 30 Dec 2019 09:38:12 +0100 Subject: [PATCH] array initialization --- vlib/v/ast/ast.v | 8 +++++++- vlib/v/gen/cgen.v | 8 ++++++++ vlib/v/gen/cgen_test.v | 2 +- vlib/v/gen/tests/{1.v => 1.vv} | 0 vlib/v/gen/tests/2.c | 6 ++++++ vlib/v/gen/tests/{2.v => 2.vv} | 19 +++++++++--------- vlib/v/parser/parser.v | 36 ++++++++++++++++++++++++++++++++++ 7 files changed, 67 insertions(+), 12 deletions(-) rename vlib/v/gen/tests/{1.v => 1.vv} (100%) rename vlib/v/gen/tests/{2.v => 2.vv} (81%) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index fe0096c6aa..37c33d7e57 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 +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt | ForStmt | StructDecl @@ -181,6 +181,12 @@ pub: op token.TokenKind } +pub struct ArrayInit { +pub: + exprs []Expr + typ types.Type +} + // string representaiton of expr pub fn (x Expr) str() string { match x { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 1c0f609b33..0de87a4407 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -144,6 +144,14 @@ fn (g mut Gen) expr(node ast.Expr) { } g.write(')') } + ast.ArrayInit { + g.writeln('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($it.typ.name), {\t') + for expr in it.exprs { + g.expr(expr) + g.write(', ') + } + g.write('\n})') + } ast.Ident { g.write('$it.name') } diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index f008c38b21..6cb1717b8d 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -17,7 +17,7 @@ fn test_c_files() { vexe := os.getenv('VEXE') vroot := filepath.dir(vexe) for i in 1 .. nr_tests + 1 { - text := os.read_file('$vroot/vlib/v/gen/tests/${i}.v') or { + text := os.read_file('$vroot/vlib/v/gen/tests/${i}.vv') or { panic(err) } ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { diff --git a/vlib/v/gen/tests/1.v b/vlib/v/gen/tests/1.vv similarity index 100% rename from vlib/v/gen/tests/1.v rename to vlib/v/gen/tests/1.vv diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index d2e8814688..f928d64723 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -41,6 +41,12 @@ void init_user() { }; } +void init_array() { + int nums = new_array_from_c_array(3, 3, sizeof(int), { + 1, 2, 3, + }); +} + int main() { return 0; } diff --git a/vlib/v/gen/tests/2.v b/vlib/v/gen/tests/2.vv similarity index 81% rename from vlib/v/gen/tests/2.v rename to vlib/v/gen/tests/2.vv index 48eaaee8c5..60d59c65f3 100644 --- a/vlib/v/gen/tests/2.v +++ b/vlib/v/gen/tests/2.vv @@ -4,9 +4,7 @@ fn function1() int { return 0 } -fn foo(a int) { - -} +fn foo(a int) {} struct User { name string @@ -22,8 +20,8 @@ fn function2() { x += 1 m += 2 function1() - //a += 1 - //c := 0 + // a += 1 + // c := 0 if true { foo(10) x += 8 @@ -36,9 +34,7 @@ fn function2() { } e := 1 + 2 > 0 e2 := 1 + 2 < 0 - - ////x += 1 - //} + // x += 1 j := 0 } @@ -48,7 +44,10 @@ fn init_user() { } } -fn main() { +fn init_array() { + nums := [1,2,3] + } - +fn main() { + } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 747198fc24..1f3b38087c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -195,6 +195,10 @@ pub fn (p &Parser) error(s string) { exit(1) } +pub fn (p &Parser) warn(s string) { + println(term.blue('x.v:$p.tok.line_nr: $s')) +} + pub fn (p mut Parser) call_expr() (ast.CallExpr,types.Type) { // println('got fn call') fn_name := p.tok.lit @@ -276,6 +280,9 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { p.next() } } + .lsbr { + node,typ = p.array_init() + } .key_true, .key_false { node = ast.BoolLiteral{ val: p.tok.kind == .key_true @@ -390,6 +397,35 @@ fn (p mut Parser) parse_string_literal() (ast.Expr,types.Type) { return node,types.string_type } +fn (p mut Parser) array_init() (ast.Expr,types.Type) { + p.check(.lsbr) + mut val_type := types.void_type + mut exprs := []ast.Expr + mut i := 0 + for p.tok.kind != .rsbr { + expr,typ := p.expr(0) + // The first element's type + if i == 0 { + val_type = typ + } + else if !types.check(val_type, typ) { + p.error('expected array element with type `$val_type.name`') + } + exprs << expr + i++ + if p.tok.kind == .comma { + p.check(.comma) + } + } + mut node := ast.Expr{} + node = ast.ArrayInit{ + typ: val_type + exprs: exprs + } + p.check(.rsbr) + return node,val_type +} + fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) { lit := p.tok.lit mut node := ast.Expr{}