v2: parse `filter()`

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

View File

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

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
}
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) {
p.next()
field_name := p.check_name()
if field_name == 'filter' {
p.table.open_scope()
p.filter(left_type)
}
// Method call
if p.tok.kind == .lpar {
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)
mut node := ast.Expr{}
node = sel_expr
if field_name == 'filter' {
p.table.close_scope()
}
return node,typ
}
@ -1069,7 +1083,8 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
// this allows overiding the builtins type
// with the real struct type info parsed from builtin
ret = p.table.register_builtin_type_symbol(t)
} else {
}
else {
ret = p.table.register_type_symbol(t)
}
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.unresolved << expr
}
return table.new_type((-idx)-1)
return table.new_type((-idx) - 1)
}
fn verror(s string) {