|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/lemon-mint/vstruct/compile/backend/dart" |
| 13 | + "github.com/lemon-mint/vstruct/compile/backend/golang" |
| 14 | + "github.com/lemon-mint/vstruct/compile/backend/python" |
| 15 | + "github.com/lemon-mint/vstruct/compile/backend/rust" |
| 16 | + "github.com/lemon-mint/vstruct/compile/frontend" |
| 17 | + "github.com/lemon-mint/vstruct/ir" |
| 18 | + "github.com/lemon-mint/vstruct/lexer" |
| 19 | + "github.com/lemon-mint/vstruct/parser" |
| 20 | +) |
| 21 | + |
| 22 | +func ReadFileAsString(fileName string) string { |
| 23 | + os.Open(fileName) |
| 24 | + file, err := os.Open(fileName) |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + defer file.Close() |
| 29 | + data, err := io.ReadAll(file) |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + return string(data) |
| 34 | +} |
| 35 | + |
| 36 | +func CreateDirectory(path string) error { |
| 37 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 38 | + err := os.MkdirAll(path, 0755) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("error creating directory: %w", err) |
| 41 | + } |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + if len(os.Args) < 3 { |
| 48 | + fmt.Println("Usage: vstruct <language: go, rust, dart, py> <file>...") |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | + generate := (func(w io.Writer, i *ir.IR, packageName string) error)(nil) |
| 52 | + modelPath := filepath.Join("vstruct", "model") |
| 53 | + ext := "" |
| 54 | + |
| 55 | + language := os.Args[1] |
| 56 | + switch language { |
| 57 | + case "go": |
| 58 | + generate = golang.Generate |
| 59 | + modelPath = filepath.Join(".", modelPath) |
| 60 | + ext = ".go" |
| 61 | + case "rust": |
| 62 | + generate = rust.Generate |
| 63 | + modelPath = filepath.Join("src", modelPath) |
| 64 | + ext = ".rs" |
| 65 | + case "dart": |
| 66 | + generate = dart.Generate |
| 67 | + modelPath = filepath.Join("lib", modelPath) |
| 68 | + ext = ".dart" |
| 69 | + case "py": |
| 70 | + generate = python.Generate |
| 71 | + modelPath = filepath.Join(".", modelPath) |
| 72 | + ext = ".py" |
| 73 | + default: |
| 74 | + fmt.Printf("Unknown language: %s\n", language) |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + for _, path := range os.Args[2:] { |
| 79 | + if !strings.HasSuffix(path, ".vstruct") { |
| 80 | + path = path + ".vstruct" |
| 81 | + } |
| 82 | + |
| 83 | + filename, err := filepath.Abs(path) |
| 84 | + if err != nil { |
| 85 | + log.Println(err) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + input := ReadFileAsString(filename) |
| 90 | + lex := lexer.NewLexer([]rune(input), path) |
| 91 | + p := parser.New(lex) |
| 92 | + file, err := p.Parse() |
| 93 | + if err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + front := frontend.New(file) |
| 97 | + err = front.Compile() |
| 98 | + if err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + goir := front.Output() |
| 102 | + goir.Options.UseUnsafe = true |
| 103 | + |
| 104 | + packageName := filepath.Base(path) |
| 105 | + packageName = strings.TrimSuffix(packageName, ".vstruct") |
| 106 | + |
| 107 | + var buf bytes.Buffer |
| 108 | + err = generate(&buf, goir, packageName) |
| 109 | + if err != nil { |
| 110 | + fmt.Println(buf.String()) |
| 111 | + panic(err) |
| 112 | + } |
| 113 | + out := buf.String() |
| 114 | + fmt.Println(out) |
| 115 | + |
| 116 | + if err := CreateDirectory(filepath.Join(modelPath, packageName)); err != nil { |
| 117 | + panic(err) |
| 118 | + } |
| 119 | + f, err := os.Create(filepath.Join(modelPath, packageName, packageName+ext)) |
| 120 | + if err != nil { |
| 121 | + panic(err) |
| 122 | + } |
| 123 | + f.WriteString(out) |
| 124 | + f.Close() |
| 125 | + } |
| 126 | +} |
0 commit comments