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)
}
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
}

View File

@ -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

View File

@ -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.

View File

@ -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();
}

View File

@ -30,6 +30,8 @@ fn foo(a int) {
for i := 0; i < 10; i++ {
}
nums := [1,2,3]
number := nums[0]
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)
}
'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)

View File

@ -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()
@ -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{

View File

@ -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
}

View File

@ -21,7 +21,7 @@ pub enum Kind {
_u32,
_u64,
_f32,
_f64,
f64,
_string,
_char,
_bool,
@ -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{