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

v2: parse filter()

This commit is contained in:
Alexander Medvednikov 2020-02-10 14:42:57 +01:00
parent 3f6ccd3120
commit c9f619dc72
4 changed files with 35 additions and 20 deletions

View File

@ -9,23 +9,19 @@ import (
) )
const ( const (
simple_cmd = [ simple_cmd = ['fmt',
'fmt', 'up',
'up', 'create',
'create', 'test', 'test-fmt', 'test-compiler',
'test', 'test-fmt', 'test-compiler', 'bin2v',
'bin2v', 'repl',
'repl', 'build-tools', 'build-examples', 'build-vbinaries']
'build-tools', 'build-examples', 'build-vbinaries'
]
) )
fn main() { fn main() {
arg := join_flags_and_argument() arg := join_flags_and_argument()
command, option := get_basic_command_and_option(arg) command,option := get_basic_command_and_option(arg)
is_verbose := '-verbose' in arg || '--verbose' in arg is_verbose := '-verbose' in arg || '--verbose' in arg
if '-v' in option || '--version' in option || command == 'version' { if '-v' in option || '--version' in option || command == 'version' {
// Print the version and exit. // Print the version and exit.
version_hash := compiler.vhash() version_hash := compiler.vhash()
@ -35,29 +31,26 @@ fn main() {
if '-h' in option || '--help' in option || command == 'help' { if '-h' in option || '--help' in option || command == 'help' {
if is_verbose { if is_verbose {
println(verbose_help_text) println(verbose_help_text)
} else { }
else {
println(help_text) println(help_text)
} }
return return
} }
if is_verbose { if is_verbose {
eprintln('v args: $arg') eprintln('v args: $arg')
eprintln('v command: $command') eprintln('v command: $command')
eprintln('v options: $option') eprintln('v options: $option')
} }
if command in simple_cmd { if command in simple_cmd {
//External tools // External tools
launch_tool(is_verbose, 'v' + command, command) launch_tool(is_verbose, 'v' + command, command)
return return
} }
if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) { if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) {
compile(command, arg) compile(command, arg)
return return
} }
match command { match command {
'', '-' { '', '-' {
if arg.len == 1 { if arg.len == 1 {

View File

@ -347,6 +347,12 @@ pub:
name string name string
} }
// filter(), map()
pub struct Lambda {
pub:
name string
}
pub struct AssignStmt { pub struct AssignStmt {
pub: pub:
left []Ident left []Ident

1
vlib/v/parser/comptime.v Normal file
View File

@ -0,0 +1 @@
module parser

View File

@ -665,9 +665,20 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.Expr {
// return node,typ // return node,typ
} }
fn (p mut Parser) filter(typ table.Type) {
p.table.register_var(table.Var{
name: 'it'
typ: typ
})
}
fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) (ast.Expr,table.Type) { fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) (ast.Expr,table.Type) {
p.next() p.next()
field_name := p.check_name() field_name := p.check_name()
if field_name == 'filter' {
p.table.open_scope()
p.filter(left_type)
}
// Method call // Method call
if p.tok.kind == .lpar { if p.tok.kind == .lpar {
p.next() p.next()
@ -697,6 +708,9 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) (ast.Expr,table.
typ := p.add_unresolved('${table.type_idx(left_type)}.$field_name', sel_expr) typ := p.add_unresolved('${table.type_idx(left_type)}.$field_name', sel_expr)
mut node := ast.Expr{} mut node := ast.Expr{}
node = sel_expr node = sel_expr
if field_name == 'filter' {
p.table.close_scope()
}
return node,typ return node,typ
} }
@ -1069,7 +1083,8 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
// this allows overiding the builtins type // this allows overiding the builtins type
// with the real struct type info parsed from builtin // with the real struct type info parsed from builtin
ret = p.table.register_builtin_type_symbol(t) ret = p.table.register_builtin_type_symbol(t)
} else { }
else {
ret = p.table.register_type_symbol(t) ret = p.table.register_type_symbol(t)
} }
if ret == -1 { if ret == -1 {
@ -1256,7 +1271,7 @@ fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.Type {
p.table.unresolved_idxs[key] = idx p.table.unresolved_idxs[key] = idx
p.unresolved << expr p.unresolved << expr
} }
return table.new_type((-idx)-1) return table.new_type((-idx) - 1)
} }
fn verror(s string) { fn verror(s string) {