Skip to content

Commit

Permalink
struct comments; interfaces; sum type match instead of node_type_str …
Browse files Browse the repository at this point in the history
…match
  • Loading branch information
medvednikov committed Aug 23, 2024
1 parent e3c435e commit ead685d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
12 changes: 9 additions & 3 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Stmt = AssignStmt

struct InvalidExpr {}

type Type = StructType | ArrayType | Ident | StarExpr | SelectorExpr
type Type = StructType | ArrayType | FuncType | InterfaceType | Ident | StarExpr | SelectorExpr

struct GoFile {
name Ident @[json: 'Name']
Expand Down Expand Up @@ -84,8 +84,9 @@ struct Ellipsis {
}

struct FuncType {
params FieldList @[json: 'Params']
results FieldList @[json: 'Results']
node_type_str string @[json: '_type']
params FieldList @[json: 'Params']
results FieldList @[json: 'Results']
}

struct ValueSpec {
Expand Down Expand Up @@ -157,6 +158,11 @@ struct StructType {
fields FieldList @[json: 'Fields']
}

struct InterfaceType {
node_type_str string @[json: '_type']
methods FieldList @[json: 'Methods']
}

struct FieldList {
list []Field @[json: 'List']
}
Expand Down
7 changes: 4 additions & 3 deletions fn_decl.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
fn (mut app App) func_decl(decl Decl) {
if decl.doc.list.len > 0 {
app.comments(decl.doc)
}
app.comments(decl.doc)
method_name := decl.name.name.to_lower()
// Capital? Then it's public in Go
is_pub := decl.name.name[0].is_capital()
Expand Down Expand Up @@ -81,6 +79,9 @@ fn (mut app App) func_params(params FieldList) {
}

fn (mut app App) comments(doc Doc) {
if doc.list.len == 0 {
return
}
for x in doc.list {
app.genln(x.text)
}
Expand Down
6 changes: 6 additions & 0 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ fn (mut app App) typ(t Type) {
StructType {
app.gen('STRUCT TYPE')
}
InterfaceType {
app.gen('INTERFACE TYPE')
}
FuncType {
app.gen('FUNC TYPE')
}
}
app.force_upper = false
}
Expand Down
38 changes: 32 additions & 6 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn (mut app App) gen_decl_stmt(stmt Stmt) {
}

fn (mut app App) gen_decl(decl Decl) {
app.comments(decl.doc)
if decl.tok == 'const' {
// app.genln('//constb')
app.const_block(decl)
Expand All @@ -16,12 +17,19 @@ fn (mut app App) gen_decl(decl Decl) {
// is_enum_decl := decl.specs[0].node_type_str == 'TypeSpec' && decl.
for spec in decl.specs {
if spec.node_type_str == 'TypeSpec' {
if spec.typ.node_type_str == 'StructType' {
app.struct_decl(spec)
} else if spec.typ.node_type_str == 'InterfaceType' {
app.interface_decl(spec)
} else {
app.type_decl(spec)
// println('TYPE NAME=${spec.name.name}')
// println(spec.typ)
// println(spec.typ.node_type_str)
match spec.typ {
InterfaceType {
app.interface_decl(spec)
}
StructType {
app.struct_decl(spec)
}
else {
app.type_decl(spec)
}
}
} else if spec.node_type_str == 'ImportSpec' && spec.path.value != '' {
app.import_spec(spec)
Expand Down Expand Up @@ -121,6 +129,24 @@ fn (mut app App) struct_decl(spec Spec) {
}

fn (mut app App) interface_decl(spec Spec) {
name := spec.name.name
app.genln('interface ${name} {')
i_type := spec.typ as InterfaceType
// println('TTT ${i_type}')
for field in i_type.methods.list {
// type_name := type_or_ident(field.typ)
for n in field.names {
// app.genln('\t${go2v_ident(n.name)} ${go2v_type(type_name)}')
app.gen('\t')
// app.force_upper = true
app.gen(app.go2v_ident(n.name))
app.gen('() ')
app.force_upper = true
app.typ(field.typ)
app.genln('')
}
}
app.genln('}\n')
}

fn (mut app App) composite_lit(c CompositeLit) {
Expand Down

0 comments on commit ead685d

Please sign in to comment.