From 2bafd41183156fabe5073bba4944c717557985e9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 4 Dec 2019 02:52:32 +0300 Subject: [PATCH] `&C.Foo(0)` cast to replace `&C.Foo{!}` hack --- vlib/compiler/expression.v | 24 ++++++++++++++++++++---- vlib/compiler/gen_c.v | 2 ++ vlib/os/os_nix.v | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index 748f8f5252..4117347743 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -182,6 +182,22 @@ fn (p mut Parser) name_expr() string { if p.peek() == .lcbr && p.table.known_type(name) { return p.get_struct_type(name, true, ptr) } + if ptr && p.peek() == .lpar { + peek2 := p.tokens[p.token_idx+1] + // `&C.Foo(0)` cast (replacing old `&C.Foo{!}`) + if peek2.tok == .number && peek2.lit == '0' { + p.cgen.insert_before('struct /*C.Foo(0)*/ ') + p.gen('0') + p.next() + p.next() + p.next() + p.next() + return name + '*' + } + // `&C.Foo(foo)` cast + p.cast(name + '*') + return name + '*' + } // C function if p.peek() == .lpar { return p.get_c_func_type(name) @@ -232,22 +248,22 @@ fn (p mut Parser) name_expr() string { if p.known_var_check_new_var(name) { return p.get_var_type(name, ptr, deref_nr) } - // if known_type || is_c_struct_init || (p.first_pass() && p.peek() == .lcbr) { // known type? int(4.5) or Color.green (enum) if p.table.known_type(name) { // cast expression: float(5), byte(0), (*int)(ptr) etc - if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) { + //if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) { + if p.peek() == .lpar || (deref && p.peek() == .rpar) { if deref { name += '*'.repeat(deref_nr ) } else if ptr { name += '*'.repeat(mul_nr) } - p.gen('(') + //p.gen('(') mut typ := name p.cast(typ) - p.gen(')') + //p.gen(')') for p.tok == .dot { typ = p.dot(typ, ph) } diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index 6a145d7fac..9c1608a7de 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -507,6 +507,8 @@ fn (p mut Parser) gen_empty_map(typ string) { } fn (p mut Parser) cast(typ string) { + p.gen('(') + defer { p.gen(')') } p.next() pos := p.cgen.add_placeholder() if p.tok == .rpar { diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 6cee8983e1..06885ed1c3 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -31,6 +31,7 @@ pub fn ls(path string) ?[]string { if isnil(dir) { return error('ls() couldnt open dir "$path"') } + //mut ent := &C.dirent(0) mut ent := &C.dirent{!} for { ent = C.readdir(dir)