1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

array initialization

This commit is contained in:
Alexander Medvednikov 2019-12-30 09:38:12 +01:00
parent 1d9916f93e
commit 2d597d7804
7 changed files with 67 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import (
) )
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | 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 | pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt |
ForStmt | StructDecl ForStmt | StructDecl
@ -181,6 +181,12 @@ pub:
op token.TokenKind op token.TokenKind
} }
pub struct ArrayInit {
pub:
exprs []Expr
typ types.Type
}
// string representaiton of expr // string representaiton of expr
pub fn (x Expr) str() string { pub fn (x Expr) str() string {
match x { match x {

View File

@ -144,6 +144,14 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
g.write(')') 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 { ast.Ident {
g.write('$it.name') g.write('$it.name')
} }

View File

@ -17,7 +17,7 @@ fn test_c_files() {
vexe := os.getenv('VEXE') vexe := os.getenv('VEXE')
vroot := filepath.dir(vexe) vroot := filepath.dir(vexe)
for i in 1 .. nr_tests + 1 { 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) panic(err)
} }
ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or {

View File

@ -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() { int main() {
return 0; return 0;
} }

View File

@ -4,9 +4,7 @@ fn function1() int {
return 0 return 0
} }
fn foo(a int) { fn foo(a int) {}
}
struct User { struct User {
name string name string
@ -22,8 +20,8 @@ fn function2() {
x += 1 x += 1
m += 2 m += 2
function1() function1()
//a += 1 // a += 1
//c := 0 // c := 0
if true { if true {
foo(10) foo(10)
x += 8 x += 8
@ -36,9 +34,7 @@ fn function2() {
} }
e := 1 + 2 > 0 e := 1 + 2 > 0
e2 := 1 + 2 < 0 e2 := 1 + 2 < 0
// x += 1
////x += 1
//}
j := 0 j := 0
} }
@ -48,7 +44,10 @@ fn init_user() {
} }
} }
fn main() { fn init_array() {
nums := [1,2,3]
} }
fn main() {
}

View File

@ -195,6 +195,10 @@ pub fn (p &Parser) error(s string) {
exit(1) 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) { pub fn (p mut Parser) call_expr() (ast.CallExpr,types.Type) {
// println('got fn call') // println('got fn call')
fn_name := p.tok.lit fn_name := p.tok.lit
@ -276,6 +280,9 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
p.next() p.next()
} }
} }
.lsbr {
node,typ = p.array_init()
}
.key_true, .key_false { .key_true, .key_false {
node = ast.BoolLiteral{ node = ast.BoolLiteral{
val: p.tok.kind == .key_true 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 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) { fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
lit := p.tok.lit lit := p.tok.lit
mut node := ast.Expr{} mut node := ast.Expr{}