mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: high order functions
This commit is contained in:
parent
2838d12227
commit
17212f816c
@ -449,7 +449,6 @@ pub fn (a []char) index(v char) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// []int.reduce executes a given reducer function on each element of the array,
|
// []int.reduce executes a given reducer function on each element of the array,
|
||||||
// resulting in a single output value.
|
// resulting in a single output value.
|
||||||
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
||||||
@ -461,6 +460,7 @@ pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
|||||||
return _accum
|
return _accum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
||||||
// []int == []int (also for: i64, f32, f64, byte, string)
|
// []int == []int (also for: i64, f32, f64, byte, string)
|
||||||
fn array_eq<T>(a1, a2 []T) bool {
|
fn array_eq<T>(a1, a2 []T) bool {
|
||||||
|
@ -92,7 +92,6 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
|||||||
p.check(.gt)
|
p.check(.gt)
|
||||||
}
|
}
|
||||||
// println('fn decl $name')
|
// println('fn decl $name')
|
||||||
p.check(.lpar)
|
|
||||||
// Args
|
// Args
|
||||||
mut args := []table.Var
|
mut args := []table.Var
|
||||||
ast_args,is_variadic := p.fn_args()
|
ast_args,is_variadic := p.fn_args()
|
||||||
@ -122,7 +121,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
|||||||
*/
|
*/
|
||||||
// Return type
|
// Return type
|
||||||
mut typ := table.void_type
|
mut typ := table.void_type
|
||||||
if p.tok.kind in [.name, .lpar, .amp, .lsbr, .question] {
|
if p.tok.kind.is_start_of_type() {
|
||||||
typ = p.parse_type()
|
typ = p.parse_type()
|
||||||
}
|
}
|
||||||
p.return_type = typ
|
p.return_type = typ
|
||||||
@ -165,6 +164,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) fn_args() ([]ast.Arg,bool) {
|
fn (p mut Parser) fn_args() ([]ast.Arg,bool) {
|
||||||
|
p.check(.lpar)
|
||||||
mut args := []ast.Arg
|
mut args := []ast.Arg
|
||||||
mut is_variadic := false
|
mut is_variadic := false
|
||||||
// `int, int, string` (no names, just types)
|
// `int, int, string` (no names, just types)
|
||||||
|
@ -67,8 +67,13 @@ pub fn (p mut Parser) parse_multi_return_type() table.Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) parse_fn_type() table.Type {
|
pub fn (p mut Parser) parse_fn_type() table.Type {
|
||||||
// p.check(.key_fn)
|
p.warn('parrse fn')
|
||||||
p.fn_decl()
|
p.check(.key_fn)
|
||||||
|
// p.fn_decl()
|
||||||
|
p.fn_args()
|
||||||
|
if p.tok.kind.is_start_of_type() {
|
||||||
|
p.parse_type()
|
||||||
|
}
|
||||||
return table.int_type
|
return table.int_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -635,7 +635,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||||||
node,typ = p.dot_expr(node, typ)
|
node,typ = p.dot_expr(node, typ)
|
||||||
}
|
}
|
||||||
else if p.tok.kind == .lsbr {
|
else if p.tok.kind == .lsbr {
|
||||||
//node = p.index_expr(node) // , typ)
|
// node = p.index_expr(node) // , typ)
|
||||||
ie_node,ie_typ := p.index_expr(node, typ)
|
ie_node,ie_typ := p.index_expr(node, typ)
|
||||||
node = ie_node
|
node = ie_node
|
||||||
typ = ie_typ
|
typ = ie_typ
|
||||||
@ -869,7 +869,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||||||
}
|
}
|
||||||
// `for i in vals`, `for i in start .. end`
|
// `for i in vals`, `for i in start .. end`
|
||||||
else if p.peek_tok.kind in [.key_in, .comma] {
|
else if p.peek_tok.kind in [.key_in, .comma] {
|
||||||
var_name := p.check_name()
|
var_name := p.check_name()
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
val_name := p.check_name()
|
val_name := p.check_name()
|
||||||
@ -900,7 +900,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||||||
// elem_type_sym := p.table.get_type_symbol(elem_type)
|
// elem_type_sym := p.table.get_type_symbol(elem_type)
|
||||||
// p.error('cannot loop over type: $elem_type_sym.name')
|
// p.error('cannot loop over type: $elem_type_sym.name')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 0 .. 10
|
// 0 .. 10
|
||||||
// start := p.tok.lit.int()
|
// start := p.tok.lit.int()
|
||||||
@ -1104,6 +1104,10 @@ fn (p mut Parser) module_decl() ast.Module {
|
|||||||
|
|
||||||
fn (p mut Parser) parse_import() ast.Import {
|
fn (p mut Parser) parse_import() ast.Import {
|
||||||
mod_name := p.check_name()
|
mod_name := p.check_name()
|
||||||
|
if p.tok.kind == .dot {
|
||||||
|
p.next()
|
||||||
|
p.check_name()
|
||||||
|
}
|
||||||
mut mod_alias := mod_name
|
mut mod_alias := mod_name
|
||||||
if p.tok.kind == .key_as {
|
if p.tok.kind == .key_as {
|
||||||
p.check(.key_as)
|
p.check(.key_as)
|
||||||
|
@ -463,6 +463,10 @@ pub fn (tok Kind) is_relational() bool {
|
|||||||
.lt, .le, .gt, .ge, .eq, .ne]
|
.lt, .le, .gt, .ge, .eq, .ne]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (k Kind) is_start_of_type() bool {
|
||||||
|
return k in [.name, .lpar, .amp, .lsbr, .question]
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (kind Kind) is_infix() bool {
|
pub fn (kind Kind) is_infix() bool {
|
||||||
return kind in [.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in,
|
return kind in [.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in,
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user