Skip to content

Commit

Permalink
use type sum type in Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Aug 23, 2024
1 parent 028a7fd commit ea8f3d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
22 changes: 14 additions & 8 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 Type2 = ArrayType | Ident | StarExpr | SelectorExpr
type Type2 = StructType | ArrayType | Ident | StarExpr | SelectorExpr

struct GoFile {
name Ident @[json: 'Name']
Expand All @@ -62,13 +62,14 @@ struct Decl {
}

struct Spec {
node_type_str string @[json: '_type']
name Ident @[json: 'Name']
names []Ident @[json: 'Names']
values []Expr @[json: 'Values']
typ Type @[json: 'Type']
args []Expr @[json: 'Args']
path BasicLit @[json: 'Path']
node_type_str string @[json: '_type']
name Ident @[json: 'Name']
names []Ident @[json: 'Names']
values []Expr @[json: 'Values']
// typ Type @[json: 'Type']
typ Type2 @[json: 'Type']
args []Expr @[json: 'Args']
path BasicLit @[json: 'Path']
}

struct ArrayType {
Expand Down Expand Up @@ -156,6 +157,11 @@ struct Type {
fields FieldList @[json: 'Fields']
}

struct StructType {
node_type_str string @[json: '_type']
fields FieldList @[json: 'Fields']
}

struct FieldList {
list []Field @[json: 'List']
}
Expand Down
3 changes: 3 additions & 0 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ fn (mut app App) typ(t Type2) {
SelectorExpr {
app.selector_expr(t)
}
StructType {
app.gen('STRUCT TYPE')
}
}
app.force_upper = false
}
Expand Down
5 changes: 3 additions & 2 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ fn (mut app App) import_spec(spec Spec) {
fn (mut app App) struct_decl(spec Spec) {
struct_name := spec.name.name
app.genln('struct ${struct_name} {')
if spec.typ.fields.list.len > 0 {
struct_type := spec.typ as StructType
if struct_type.fields.list.len > 0 {
app.genln('pub mut:')
}
for field in spec.typ.fields.list {
for field in struct_type.fields.list {
// type_name := type_or_ident(field.typ)
for n in field.names {
// app.genln('\t${go2v_ident(n.name)} ${go2v_type(type_name)}')
Expand Down

0 comments on commit ea8f3d2

Please sign in to comment.