From ea8f3d2aeb41a18069452027b7416c3f98cae8cb Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 23 Aug 2024 14:29:52 +0300 Subject: [PATCH] use type sum type in Spec --- ast.v | 22 ++++++++++++++-------- main.v | 3 +++ struct.v | 5 +++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ast.v b/ast.v index cc91dd0..44d13cb 100644 --- a/ast.v +++ b/ast.v @@ -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'] @@ -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 { @@ -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'] } diff --git a/main.v b/main.v index 1afc346..60dc5c8 100644 --- a/main.v +++ b/main.v @@ -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 } diff --git a/struct.v b/struct.v index 30a3607..fd526ce 100644 --- a/struct.v +++ b/struct.v @@ -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)}')