Browse Source

WIP typeSpec

Frédéric G. MARAND 1 year ago
parent
commit
2e535a9e3f
5 changed files with 55 additions and 4 deletions
  1. 1 1
      decl.go
  2. 33 0
      expr.go
  3. 8 1
      main.go
  4. 11 1
      parse.go
  5. 2 1
      stmt.go

+ 1 - 1
decl.go

@@ -23,7 +23,7 @@ func parseGenDecl(loc string, d *ast.GenDecl) {
 		switch s := s.(type) {
 		case *ast.TypeSpec:
 			parseTypeSpec(loc, s)
-		case *ast.ImportSpec:
+		case *ast.ImportSpec, *ast.ValueSpec:
 			// rien
 		default:
 			fmt.Printf("%s/(%T)\n", loc, s)

+ 33 - 0
expr.go

@@ -0,0 +1,33 @@
+package main
+
+import (
+	"fmt"
+	"go/ast"
+)
+
+func parseTypeExpr(loc string, s ast.Expr, ln bool) {
+	switch s := s.(type) {
+	case *ast.ArrayType:
+		if s.Len == nil {
+			fmt.Print("slice of ")
+		} else {
+			fmt.Print("array of ")
+		}
+		parseTypeExpr(loc, s.Elt, false)
+	case *ast.ChanType:
+		fmt.Print("chan of ")
+		parseTypeExpr(loc, s.Value, false)
+	case *ast.Ident:
+		fmt.Print(s)
+	case *ast.MapType:
+		fmt.Print("map of ")
+		parseTypeExpr(loc, s.Value, false)
+		fmt.Print(" by ")
+		parseTypeExpr(loc, s.Key, false)
+	default:
+		fmt.Printf("type: %#v\n", s)
+	}
+	if ln {
+		fmt.Println()
+	}
+}

+ 8 - 1
main.go

@@ -11,7 +11,14 @@ import (
 	"log"
 )
 
-type packageScope bool
+type (
+	psDefined bool
+	psAlias   = bool
+	psSlice   []bool
+	psArray   [4]bool
+	psChan    chan bool
+	psMap     map[bool]bool
+)
 
 // showPackageImports will usually show nothing
 func showPackageImports(pkg *ast.Package) {

+ 11 - 1
parse.go

@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"go/ast"
+	"go/token"
 )
 
 func parseFile(f *ast.File) {
@@ -19,6 +20,15 @@ func parseFile(f *ast.File) {
 	}
 }
 
+// type specification: "type [=] <typ>"
 func parseTypeSpec(loc string, s *ast.TypeSpec) {
-	fmt.Printf("%s/typeSpec\n", loc)
+	// type foo bool: .Name + .Type / .Assign = NoPos (== 0)
+	// type foo = bool: .Name + .Type / .Assign = <pos>
+	name := s.Name.String()
+	if s.Assign == token.NoPos {
+		fmt.Printf("%s/Defined %s: ", loc, name)
+	} else {
+		fmt.Printf("%s/Aliased %s: ", loc, name)
+	}
+	parseTypeExpr(loc, s.Type, true)
 }

+ 2 - 1
stmt.go

@@ -11,7 +11,8 @@ func parseStmt(loc string, s ast.Stmt) {
 		parseCaseClause(loc, s)
 	case *ast.DeclStmt:
 		parseDeclStmt(loc, s)
-	case *ast.ExprStmt, *ast.AssignStmt, *ast.ReturnStmt:
+	case *ast.ExprStmt, *ast.AssignStmt, *ast.ReturnStmt,
+		*ast.BranchStmt: // break, continue, fallthrough, goto,
 		// fmt.Printf("%s/(%T)\n", loc, s)
 	case *ast.IfStmt:
 		parseIfStmt(loc, s)