mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
index expression
This commit is contained in:
parent
b7509577b5
commit
706c6066d5
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,8 @@ fn foo(a int) {
|
||||
for i := 0; i < 10; i++ {
|
||||
|
||||
}
|
||||
nums := [1,2,3]
|
||||
number := nums[0]
|
||||
n := get_int2()
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
@ -571,7 +592,7 @@ fn (p mut Parser) if_expr() (ast.Expr,types.TypeIdent) {
|
||||
else_stmts: else_stmts
|
||||
ti: ti
|
||||
// left: left
|
||||
|
||||
|
||||
}
|
||||
p.inside_if = false
|
||||
return node,ti
|
||||
@ -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{
|
||||
@ -770,7 +791,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
|
||||
return ast.VarDecl{
|
||||
name: name
|
||||
expr: expr // p.expr(token.lowest_prec)
|
||||
|
||||
|
||||
ti: ti
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ pub enum Kind {
|
||||
_u32,
|
||||
_u64,
|
||||
_f32,
|
||||
_f64,
|
||||
f64,
|
||||
_string,
|
||||
_char,
|
||||
_bool,
|
||||
@ -32,7 +32,7 @@ pub enum Kind {
|
||||
_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
|
||||
|
||||
pub struct TypeIdent {
|
||||
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user