Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Sep 3, 2023
1 parent 62024ad commit 68a5d30
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 90 deletions.
153 changes: 99 additions & 54 deletions gnovm/cmd/gnoffee/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"go/ast"
"go/parser"
Expand All @@ -10,79 +11,123 @@ import (
"os"
"path/filepath"

"github.com/gnolang/gno/gnovm/pkg/gnoffee" // replace with your actual import path
"github.com/gnolang/gno/gnovm/pkg/gnoffee"
)

var writeFlag bool

func init() {
flag.BoolVar(&writeFlag, "w", false, "write result to gnoffee.gen.go file instead of stdout")
}

func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gnoffee <package-path or file1.go file2.go ... or ->")
flag.Parse()
args := flag.Args()

if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Usage: gnoffee [-w] <package-path or file.gnoffee or '-'>")
return
}

var filesToProcess []string
err := doMain(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

arg := os.Args[1]
if arg == "-" {
// Read from stdin and process
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("Error reading from stdin:", err)
return
}
func doMain(arg string) error {
fset, pkg, err := processPackageOrFileOrStdin(arg)
if err != nil {
return fmt.Errorf("parse error: %w", err)
}

processData(data)
return
} else if filepath.Ext(arg) == ".gnoffee" {
filesToProcess = append(filesToProcess, arg)
} else {
// Treat it as a directory and gather all .gnoffee files
err := filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".gnoffee" {
filesToProcess = append(filesToProcess, path)
}
return nil
})
if err != nil {
fmt.Println("Error walking the directory:", err)
return
}
newFile, err := gnoffee.Stage2(pkg)
if err != nil {
return fmt.Errorf("processing the AST: %w", err)
}

// Parse each file and process
for _, file := range filesToProcess {
data, err := ioutil.ReadFile(file)
// combine existing files into newFile to generate a unique file for the whole package.
for _, file := range pkg {
newFile.Decls = append(newFile.Decls, file.Decls...)
}

if writeFlag {
filename := "gnoffee.gen.go"
f, err := os.Create(filename)
if err != nil {
fmt.Printf("Error reading file %s: %v\n", file, err)
continue
return fmt.Errorf("creating file %q: %w", filename, err)
}
defer f.Close()

processData(data)
err = printer.Fprint(f, fset, newFile)
if err != nil {
return fmt.Errorf("writing to file %q: %w", filename, err)
}
} else {
_ = printer.Fprint(os.Stdout, fset, newFile)
}
return nil
}

func processData(data []byte) {
source := string(data)
source = gnoffee.Stage1(source)
func processPackageOrFileOrStdin(arg string) (*token.FileSet, map[string]*ast.File, error) {
var fset = token.NewFileSet()

Check failure on line 74 in gnovm/cmd/gnoffee/main.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
var pkg = map[string]*ast.File{}

fset := token.NewFileSet()
parsedFile, err := parser.ParseFile(fset, "", source, parser.ParseComments)
if err != nil {
fmt.Println("Error parsing the Go file:", err)
return
}
processFile := func(data []byte, filename string) error {
source := string(data)
source = gnoffee.Stage1(source)

newFile, err := gnoffee.Stage2(map[string]*ast.File{"": parsedFile})
if err != nil {
fmt.Println("Error processing the AST:", err)
return
parsedFile, err := parser.ParseFile(fset, filename, source, parser.ParseComments)
if err != nil {
return fmt.Errorf("parsing file %q: %v", filename, err)

Check failure on line 83 in gnovm/cmd/gnoffee/main.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
pkg[filename] = parsedFile
return nil
}

err = printer.Fprint(os.Stdout, fset, newFile)
if err != nil {
fmt.Println("Error writing to stdout:", err)
return
// process arg
if arg == "-" {
// Read from stdin and process
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, nil, fmt.Errorf("reading from stdin: %w", err)
}
if err := processFile(data, "stdin.gnoffee"); err != nil {
return nil, nil, err
}
} else {
// If it's a directory, gather all .go and .gnoffee files and process accordingly
if info, err := os.Stat(arg); err == nil && info.IsDir() {
err := filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

ext := filepath.Ext(path)
if ext == ".gnoffee" {
data, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("reading file %q: %v", path, err)

Check failure on line 111 in gnovm/cmd/gnoffee/main.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
if err := processFile(data, path); err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, nil, err
}
} else {
data, err := ioutil.ReadFile(arg)
if err != nil {
return nil, nil, fmt.Errorf("reading file %q: %w", arg, err)
}
if err := processFile(data, arg); err != nil {
return nil, nil, err
}
}
}
return fset, pkg, nil
}
60 changes: 60 additions & 0 deletions gnovm/cmd/gnoffee/testdata/valid_sample_with_export.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Test with a valid sample.gnoffee

gnoffee -w .

! stderr .+
! stdout .+

cmp gen.golden gnoffee.gen.go

-- sample.gnoffee --
package sample

type foo struct{}

export baz as Bar

var baz = foo{}

func (f *foo) Hello() string {
return "Hello from foo!"
}

func (f *foo) Bye() {
println("Goodbye from foo!")
}

type Bar interface {
Hello() string
Bye()
}

-- gen.golden --
package sample

// This function was generated by gnoffee due to the export directive.
func Hello() string {
return baz.Hello()
}

// This function was generated by gnoffee due to the export directive.
func Bye() {
baz.Bye()
}

type foo struct{}

var baz = foo{}

func (f *foo) Hello() string {
return "Hello from foo!"
}

func (f *foo) Bye() {
println("Goodbye from foo!")
}

type Bar interface {
Hello() string
Bye()
}
36 changes: 0 additions & 36 deletions gnovm/cmd/gnoffee/testdata/valid_test.txtar

This file was deleted.

0 comments on commit 68a5d30

Please sign in to comment.