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

[if vfmt] for fgen() functions

This commit is contained in:
Alexander Medvednikov 2019-10-27 02:36:43 +03:00
parent d81b0675f0
commit d00fdca38f
4 changed files with 19 additions and 7 deletions

View File

@ -5,7 +5,7 @@
module builtin
struct Option {
data [255]byte
data [300]byte
error string
ecode int
ok bool
@ -14,8 +14,8 @@ struct Option {
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, size int) Option {
if size >= 255 {
panic('option size too big: $size (max is 255), this is a temporary limit')
if size >= 300 {
panic('option size too big: $size (max is 300), this is a temporary limit')
}
res := Option {
ok: true

View File

@ -43,6 +43,7 @@ mut:
dispatch_of TypeInst // current type inst of this generic instance
body_idx int // idx of the first body statement
fn_name_token_idx int // used by error reporting
comptime_define string
}
struct TypeInst {
@ -197,6 +198,7 @@ fn (p mut Parser) fn_decl() {
is_public: p.tok == .key_pub || p.is_vh // functions defined in .vh are always public
is_unsafe: p.attr == 'unsafe_fn'
is_deprecated: p.attr == 'deprecated'
comptime_define: if p.attr.starts_with('if ') { p.attr.right(3) } else { '' }
}
is_live := p.attr == 'live' && !p.pref.is_so && p.pref.is_live
if p.attr == 'live' && p.first_pass() && !p.pref.is_live && !p.pref.is_so {
@ -677,6 +679,10 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
}
p.error('function `$f.name` is private')
}
is_comptime_define := f.comptime_define != '' && f.comptime_define != p.pref.comptime_define
if is_comptime_define {
p.cgen.nogen = true
}
p.calling_c = f.is_c
if f.is_c && !p.builtin_mod {
if f.name == 'free' {
@ -739,6 +745,9 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
p.gen(')')
p.calling_c = false
if is_comptime_define {
p.cgen.nogen = false
}
// println('end of fn call typ=$f.typ')
}

View File

@ -1853,9 +1853,6 @@ fn (p mut Parser) var_expr(v Var) string {
p.next()
mut typ := v.typ
// Function pointer?
//println('CALLING FN PTR')
//p.print_tok()
if typ.starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(typ)
p.gen('(')
@ -2038,7 +2035,7 @@ struct $typ.name {
exit(1)
}
p.fn_call(mut method, method_ph, '', str_typ)
// Methods returning `array` should return `array_string`
// Methods returning `array` should return `array_string` etc
if method.typ == 'array' && typ.name.starts_with('array_') {
return typ.name
}

View File

@ -17,6 +17,7 @@ fn (scanner mut Scanner) fgen(s_ string) {
scanner.fmt_line_empty = false
}
//[if vfmt]
fn (scanner mut Scanner) fgenln(s_ string) {
mut s := s_
if scanner.fmt_line_empty {
@ -26,14 +27,17 @@ fn (scanner mut Scanner) fgenln(s_ string) {
scanner.fmt_line_empty = true
}
//[if vfmt]
fn (p mut Parser) fgen(s string) {
p.scanner.fgen(s)
}
//[if vfmt]
fn (p mut Parser) fspace() {
p.fgen(' ')
}
//[if vfmt]
fn (p mut Parser) fgenln(s string) {
p.scanner.fgenln(s)
}
@ -51,10 +55,12 @@ fn (p mut Parser) peek() TokenKind {
}
*/
//[if vfmt]
fn (p mut Parser) fmt_inc() {
p.scanner.fmt_indent++
}
//[if vfmt]
fn (p mut Parser) fmt_dec() {
p.scanner.fmt_indent--
}