From c9f619dc72fa56d7e4b650e1d523baa69003c885 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 10 Feb 2020 14:42:57 +0100 Subject: [PATCH] v2: parse `filter()` --- cmd/v/v.v | 29 +++++++++++------------------ vlib/v/ast/ast.v | 6 ++++++ vlib/v/parser/comptime.v | 1 + vlib/v/parser/parser.v | 19 +++++++++++++++++-- 4 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 vlib/v/parser/comptime.v diff --git a/cmd/v/v.v b/cmd/v/v.v index 8e92662325..a3d5caa606 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -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 { diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index b36ee1ed9e..04b8795118 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -347,6 +347,12 @@ pub: name string } +// filter(), map() +pub struct Lambda { +pub: + name string +} + pub struct AssignStmt { pub: left []Ident diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v new file mode 100644 index 0000000000..234e51e2bb --- /dev/null +++ b/vlib/v/parser/comptime.v @@ -0,0 +1 @@ +module parser diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 73037358a1..3ad90b2b7c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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) {