mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
array initialization
This commit is contained in:
parent
1d9916f93e
commit
2d597d7804
@ -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 {
|
||||
|
@ -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')
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -4,9 +4,7 @@ fn function1() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
fn foo(a int) {
|
||||
|
||||
}
|
||||
fn foo(a int) {}
|
||||
|
||||
struct User {
|
||||
name string
|
||||
@ -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() {
|
||||
}
|
@ -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{}
|
||||
|
Loading…
Reference in New Issue
Block a user