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

vh generation: consts + cleanup

This commit is contained in:
Alexander Medvednikov
2019-10-23 08:18:44 +03:00
parent fcead2f79e
commit 8b74c711c6
5 changed files with 69 additions and 121 deletions

View File

@ -15,108 +15,6 @@ import (
They are used together with pre-compiled modules.
*/
// "fn foo(a int) string"
fn (f &Fn) v_definition() string {
//t :=time.ticks()
mut sb := strings.new_builder(100)
if f.is_public {
sb.write('pub ')
}
sb.write('fn ')
if f.is_c {
sb.write('C.')
}
if f.is_method {
recv := f.args[0]
typ := v_type_str(recv.typ).replace('*', '')
mut mu := if recv.is_mut { 'mut' } else { '' }
if recv.ref {
mu = '&'
}
sb.write('($recv.name $mu $typ) ')
}
if f.name.contains('__') {
sb.write(f.name.all_after('__') + '(')
} else {
sb.write('$f.name(')
}
for i, arg in f.args {
if i == 0 && f.is_method { // skip the receiver
continue
}
typ := v_type_str(arg.typ).replace('*', '&')
if arg.name == '' {
sb.write(typ)
} else {
sb.write('$arg.name $typ')
}
if i != f.args.len - 1 {
sb.write(', ')
}
}
sb.write(')')
if f.typ != 'void' {
typ := v_type_str(f.typ).replace('*', '&')
sb.write(' ')
sb.write(typ)
sb.writeln(' ')
}
//println('ms: ${time.ticks() - t}')
return sb.str()
}
fn v_type_str(typ_ string) string {
mut typ := if typ_.ends_with('*') {
'*' + typ_.left(typ_.len - 1)
} else {
typ_
}
typ = typ.replace('Option_', '?')
// fn parent/alias?
if typ.starts_with('fn ') {
mut types := []string
fi_lpar := typ.index_byte(`(`)
li_rpar := typ.last_index_byte(`)`)
ret_type := typ.right(li_rpar+1)
for t in typ.substr(fi_lpar+1, li_rpar).split(',') {
types << v_type_str(t)
}
return 'fn (' + types.join(', ') + ')$ret_type'
}
typ = typ.replace('Option_', '?')
// multiple return
if typ.contains('_V_MulRet') {
words := typ.replace('_V_MulRet_', '').replace('_PTR_', '*').split('_V_')
typ = '('
for i in 0 .. words.len {
typ += v_type_str(words[i])
if i != words.len - 1 {
typ += ','
}
}
typ += ')'
return typ
}
//println('"$typ"')
if typ == '*void' {
return 'voidptr'
}
if typ == '*byte' {
return 'byteptr'
}
if typ.starts_with('array_') {
return '[]' + typ.right(6)
}
if typ.contains('__') {
opt := typ.starts_with('?')
typ = typ.all_after('__')
if opt {
typ = '?' + typ
}
}
return typ
}
// `mod` == "vlib/os"
fn generate_vh(mod string) {
println('\n\n\n\nGenerating a V header file for module `$mod`')
@ -145,6 +43,8 @@ fn generate_vh(mod string) {
//v.pref.generating_vh = true
for file in filtered {
mut p := v.new_parser_from_file(file)
p.scanner.is_vh = true
println('kek $file')
p.parse(.decl)
for i, tok in p.tokens {
if !p.tok.is_decl() {
@ -187,10 +87,23 @@ fn generate_fn(file os.File, tokens []Token, i int) {
}
fn generate_const(file os.File, tokens []Token, i int) {
//mut out := strings.new_builder(100)
mut out := strings.new_builder(100)
mut tok := tokens[i]
for i < tokens.len && tok.tok != .rpar {
out.write(tok.str())
out.write(' ')
if tokens[i+2].tok == .assign {
out.write('\n\t')
}
i++
tok = tokens[i]
}
out.writeln('\n)')
file.writeln(out.str())
}
/*
fn (v &V) generate_vh_old() {
println('\n\n\n\nGenerating a V header file for module `$v.mod`')
mod_path := v.mod.replace('.', os.path_separator)
@ -333,4 +246,5 @@ fn (v &V) generate_vh_old() {
}
*/
}
*/