clean / improve code generation module

This commit is contained in:
timurgordon
2025-02-01 11:55:45 +03:00
parent eeb2602bcf
commit f6fe3d4fda
6 changed files with 44 additions and 57 deletions

View File

@@ -5,6 +5,7 @@ import freeflowuniverse.herolib.core.pathlib
pub interface IFolder {
name string
files []IFile
modules []Module
write(string, WriteOptions) !
}
@@ -12,6 +13,7 @@ pub struct Folder {
pub:
name string
files []IFile
modules []Module
}
pub fn (f Folder) write(path string, options WriteOptions) ! {
@@ -27,4 +29,7 @@ pub fn (f Folder) write(path string, options WriteOptions) ! {
for file in f.files {
file.write(dir.path, options)!
}
for mod in f.modules {
mod.write(dir.path, options)!
}
}

View File

@@ -9,6 +9,7 @@ pub mut:
name string
files []IFile
folders []IFolder
modules []Module
// model VFile
// methods VFile
}
@@ -42,6 +43,10 @@ pub fn (mod Module) write(path string, options WriteOptions) ! {
folder.write(module_dir.path, options)!
}
for mod_ in mod.modules {
mod_.write(module_dir.path, options)!
}
if options.format {
os.execute('v fmt -w ${module_dir.path}')
}

View File

@@ -42,7 +42,7 @@ pub fn new_param(params Params) !Param {
pub fn (param Param) vgen() string {
sym := param.typ.symbol()
param_name := texttools.name_fix_snake(param.name)
param_name := texttools.snake_case(param.name)
mut vstr := '${param_name} ${sym}'
if param.mutable {
vstr = 'mut ${vstr}'
@@ -51,7 +51,7 @@ pub fn (param Param) vgen() string {
}
pub fn (p Param) typescript() string {
name := texttools.name_fix_snake(p.name)
name := texttools.camel_case(p.name)
suffix := if p.is_optional {'?'} else {''}
return '${name}${suffix}: ${p.typ.typescript()}'
}

View File

@@ -28,8 +28,29 @@ pub mut:
structure Struct @[str: skip]
}
pub fn (field StructField) vgen() string {
mut vstr := field.Param.vgen()
if field.description != '' {
vstr += '// ${field.description}'
}
return vstr
}
pub fn (structure Struct) get_type_symbol() string {
mut symbol := if structure.mod != '' {
'${structure.mod.all_after_last('.')}.${structure.name}'
} else {
structure.name
}
if structure.generics.len > 0 {
symbol = '${symbol}${vgen_generics(structure.generics)}'
}
return symbol
}
pub fn (s Struct) typescript() string {
name := texttools.name_fix_pascal(s.name)
fields := s.fields.map(it.typescript()).join_lines()
return 'export interface ${name} {\n${fields}\n}'
}
}

View File

@@ -127,3 +127,8 @@ pub fn (t Type) typescript() string {
Void {''}
}
}
// TODO: enfore that cant be both mutable and shared
pub fn (t Type) vgen() string {
return t.symbol()
}

View File

@@ -42,46 +42,6 @@ pub fn (import_ Import) vgen() string {
return 'import ${import_.mod} ${types_str}'
}
// TODO: enfore that cant be both mutable and shared
pub fn (t Type) vgen() string {
return t.symbol()
}
pub fn (field StructField) vgen() string {
symbol := field.get_type_symbol()
mut vstr := '${field.name} ${symbol}'
if field.description != '' {
vstr += '// ${field.description}'
}
return vstr
}
pub fn (field StructField) get_type_symbol() string {
mut field_str := if field.structure.name != '' {
field.structure.get_type_symbol()
} else {
field.typ.symbol()
}
if field.is_ref {
field_str = '&${field_str}'
}
return field_str
}
pub fn (structure Struct) get_type_symbol() string {
mut symbol := if structure.mod != '' {
'${structure.mod.all_after_last('.')}.${structure.name}'
} else {
structure.name
}
if structure.generics.len > 0 {
symbol = '${symbol}${vgen_generics(structure.generics)}'
}
return symbol
}
pub fn vgen_generics(generics map[string]string) string {
if generics.keys().len == 0 {
@@ -99,7 +59,7 @@ pub fn (function Function) vgen(options WriteOptions) string {
mut params_ := function.params.clone()
optionals := function.params.filter(it.is_optional)
options_struct := Struct{
name: '${texttools.name_fix_snake_to_pascal(function.name)}Options'
name: '${texttools.snake_case_to_pascal(function.name)}Options'
attrs: [Attribute{
name: 'params'
}]
@@ -159,10 +119,10 @@ pub fn (struct_ Struct) vgen() string {
''
}
priv_fields := struct_.fields.filter(!it.is_mut && !it.is_pub).map(generate_struct_field(it))
pub_fields := struct_.fields.filter(!it.is_mut && it.is_pub).map(generate_struct_field(it))
mut_fields := struct_.fields.filter(it.is_mut && !it.is_pub).map(generate_struct_field(it))
pub_mut_fields := struct_.fields.filter(it.is_mut && it.is_pub).map(generate_struct_field(it))
priv_fields := struct_.fields.filter(!it.is_mut && !it.is_pub).map(it.vgen())
pub_fields := struct_.fields.filter(!it.is_mut && it.is_pub).map(it.vgen())
mut_fields := struct_.fields.filter(it.is_mut && !it.is_pub).map(it.vgen())
pub_mut_fields := struct_.fields.filter(it.is_mut && it.is_pub).map(it.vgen())
mut struct_str := $tmpl('templates/struct/struct.v.template')
if false {
@@ -179,15 +139,6 @@ pub fn (struct_ Struct) vgen() string {
// return result.output
}
pub fn generate_struct_field(field StructField) string {
symbol := field.get_type_symbol()
mut vstr := '${field.name} ${symbol}'
if field.description != '' {
vstr += '// ${field.description}'
}
return vstr
}
pub fn (custom CustomCode) vgen() string {
return custom.text
}