-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (50 loc) · 1.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/gowasm/gox/parser"
"github.com/gowasm/gox/printer"
"github.com/gowasm/gox/token"
)
func main() {
if len(os.Args) > 1 {
Transpile(os.Args[1])
} else {
Transpile("goxtests")
}
}
type GoxTranspiler struct {
cfg *printer.Config
fset *token.FileSet
}
func (g *GoxTranspiler) TranspileFile(path string, f os.FileInfo, err error) error {
name := f.Name()
if !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".gox") {
fmt.Printf("Transpiling %s\n", path)
g.fset.AddFile(filepath.Join(path, name), -1, int(f.Size()))
file, err := parser.ParseFile(g.fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println("Can't parse file", err)
return err
}
//cfg.Fprint(os.Stdout, fset, file)
of, err := os.Create(path[:len(path)-1]) // lol
g.cfg.Fprint(of, g.fset, file)
if err != nil {
fmt.Printf("Failed with error: %v", err)
log.Fatalf("ParseFile(%s): %v", name, err)
return err
}
}
return nil
}
func Transpile(directory string) {
goxT := &GoxTranspiler{
cfg: &printer.Config{Mode: printer.GoxToGo | printer.RawFormat},
fset: token.NewFileSet(),
}
filepath.Walk(directory, goxT.TranspileFile)
}