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

index expression

This commit is contained in:
Alexander Medvednikov 2020-01-07 12:14:10 +01:00
parent b7509577b5
commit 706c6066d5
9 changed files with 58 additions and 14 deletions

View File

@ -269,7 +269,8 @@ pub fn (a array) reverse() array {
data: calloc(a.cap * a.element_size) data: calloc(a.cap * a.element_size)
} }
for i := 0; i < a.len; i++ { 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 return arr
} }

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 | 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 | pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt ForStmt | StructDecl | ForCStmt | ForInStmt
@ -191,6 +191,13 @@ pub:
right Expr right Expr
} }
pub struct IndexExpr {
pub:
// op token.Kind
left Expr
index Expr
}
pub struct IfExpr { pub struct IfExpr {
pub: pub:
tok_kind token.Kind tok_kind token.Kind

View File

@ -239,6 +239,12 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('.') g.write('.')
g.write(it.field) g.write(it.field)
} }
ast.IndexExpr {
g.expr(it.left)
g.write('[')
g.expr(it.index)
g.write(']')
}
ast.IfExpr { ast.IfExpr {
// If expression? Assign the value to a temp var. // If expression? Assign the value to a temp var.
// Previously ?: was used, but it's too unreliable. // Previously ?: was used, but it's too unreliable.

View File

@ -27,6 +27,10 @@ void foo(int a) {
i < 10; i++; 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(); void n = get_int2();
} }

View File

@ -30,6 +30,8 @@ fn foo(a int) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
} }
nums := [1,2,3]
number := nums[0]
n := get_int2() n := get_int2()
} }

View File

@ -134,7 +134,7 @@ pub fn (p mut Parser) parse_ti() types.TypeIdent {
return types.new_builtin_ti(._f32, nr_muls) return types.new_builtin_ti(._f32, nr_muls)
} }
'f64' { 'f64' {
return types.new_builtin_ti(._f64, nr_muls) return types.new_builtin_ti(.f64, nr_muls)
} }
'string' { 'string' {
return types.new_builtin_ti(._string, nr_muls) return types.new_builtin_ti(._string, nr_muls)

View File

@ -320,12 +320,12 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) {
node,ti = p.string_expr() node,ti = p.string_expr()
} }
// -1, -a etc // -1, -a etc
.minus { .minus, .amp {
node,ti = p.prefix_expr() node,ti = p.prefix_expr()
} }
.amp { // .amp {
p.next() // p.next()
} // }
.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
@ -359,9 +359,13 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,types.TypeIdent) {
else if p.tok.kind == .dot { else if p.tok.kind == .dot {
node,ti = p.dot_expr(node) 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() { else if p.tok.kind.is_infix() {
node,ti = p.infix_expr(node) node,ti = p.infix_expr(node)
} }
// Postfix
else if p.tok.kind in [.inc, .dec] { else if p.tok.kind in [.inc, .dec] {
node = ast.PostfixExpr{ node = ast.PostfixExpr{
op: p.tok.kind op: p.tok.kind
@ -389,6 +393,23 @@ fn (p mut Parser) prefix_expr() (ast.Expr,types.TypeIdent) {
return expr,ti 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) { fn (p mut Parser) dot_expr(left ast.Expr) (ast.Expr,types.TypeIdent) {
p.next() p.next()
field_name := p.check_name() field_name := p.check_name()
@ -571,7 +592,7 @@ fn (p mut Parser) if_expr() (ast.Expr,types.TypeIdent) {
else_stmts: else_stmts else_stmts: else_stmts
ti: ti ti: ti
// left: left // left: left
} }
p.inside_if = false p.inside_if = false
return node,ti return node,ti
@ -636,7 +657,7 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.TypeIdent) {
// val: lit.f64() // val: lit.f64()
val: lit val: lit
} }
ti = types.new_builtin_ti(._f64, 0) ti = types.new_builtin_ti(.f64, 0)
} }
else { else {
node = ast.IntegerLiteral{ node = ast.IntegerLiteral{
@ -770,7 +791,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
return ast.VarDecl{ return ast.VarDecl{
name: name name: name
expr: expr // p.expr(token.lowest_prec) expr: expr // p.expr(token.lowest_prec)
ti: ti ti: ti
} }
} }

View File

@ -358,6 +358,9 @@ const (
// precedence returns a tokens precedence if defined, otherwise lowest_prec // precedence returns a tokens precedence if defined, otherwise lowest_prec
pub fn (tok Token) precedence() int { pub fn (tok Token) precedence() int {
match tok.kind { match tok.kind {
.lsbr {
return 9
}
.dot { .dot {
return 8 return 8
} }

View File

@ -21,7 +21,7 @@ pub enum Kind {
_u32, _u32,
_u64, _u64,
_f32, _f32,
_f64, f64,
_string, _string,
_char, _char,
_bool, _bool,
@ -32,7 +32,7 @@ pub enum Kind {
_variadic _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 Int | Float | String | Char | Byte | Bool | Array | ArrayFixed | Map | MultiReturn | Variadic
pub struct TypeIdent { pub struct TypeIdent {
@ -75,7 +75,7 @@ pub fn (ti &TypeIdent) is_int() bool {
[inline] [inline]
pub fn (ti &TypeIdent) is_float() bool { pub fn (ti &TypeIdent) is_float() bool {
return ti.kind in [._f32, ._f64] return ti.kind in [._f32, .f64]
} }
[inline] [inline]
@ -145,7 +145,7 @@ pub fn (k Kind) str() string {
._f32{ ._f32{
'f32' 'f32'
} }
._f64{ .f64{
'f64' 'f64'
} }
._string{ ._string{