-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 11d2778
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/EdmondTabaku/mojo-gojo/translator" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println("Usage: mojo-gojo <command> [directory/file]") | ||
os.Exit(1) | ||
} | ||
|
||
cmd := os.Args[1] | ||
dir := "." | ||
|
||
if len(os.Args) > 2 { | ||
dir = os.Args[2] | ||
} | ||
|
||
switch cmd { | ||
case "init": | ||
// Initialize a new Go module for package management | ||
modCmd := exec.Command("go", "mod", "init", dir) | ||
modCmd.Stdout = os.Stdout | ||
modCmd.Stderr = os.Stderr | ||
if err := modCmd.Run(); err != nil { | ||
fmt.Printf("Error initializing Go module: %s\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Initialized a new amazing mojo-gojo project:", dir) | ||
|
||
case "build": | ||
generatedFiles, err := translator.TranslateDirectory(dir) | ||
if err != nil { | ||
fmt.Printf("Error translating the awesome mojo-gojo files: %s\n", err) | ||
os.Exit(1) | ||
} | ||
defer deleteGeneratedFiles(generatedFiles) | ||
|
||
case "run": | ||
generatedFiles, err := translator.TranslateDirectory(dir) | ||
if err != nil { | ||
fmt.Printf("Error translating the awesome mojo-gojo files: %s\n", err) | ||
os.Exit(1) | ||
} | ||
defer deleteGeneratedFiles(generatedFiles) | ||
|
||
// Now, run the Go code | ||
cmd := exec.Command("go", "run", dir) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
fmt.Printf("Error running the Go code: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
default: | ||
fmt.Println("Unknown command. Valid commands are: init, build, run") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// deleteGeneratedFiles deletes the provided list of .go files. | ||
func deleteGeneratedFiles(files []string) { | ||
for _, file := range files { | ||
err := os.Remove(file) | ||
if err != nil { | ||
fmt.Printf("Error deleting generated file %s: %s\n", file, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/EdmondTabaku/mojo-gojo | ||
|
||
go 1.21.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package translator | ||
|
||
// emojiToKeyword contains mappings of emojis to their Go keyword counterparts. | ||
var emojiToKeyword = map[string]string{ | ||
"π¦": "package", | ||
"π§": "func", | ||
"π": "for", | ||
"π₯": "import", | ||
"β": "break", | ||
"π²": "default", | ||
"π": "interface", | ||
"π―": "select", | ||
"π§³": "case", | ||
"π΄": "defer", | ||
"π": "go", | ||
"πΊοΈ": "map", | ||
"ποΈ": "struct", | ||
"π«": "chan", | ||
"π€·": "else", | ||
"πΆββοΈ": "goto", | ||
"π": "switch", | ||
"π": "const", | ||
"π€Έ": "fallthrough", | ||
"π€": "if", | ||
"π": "range", | ||
"π": "type", | ||
"βοΈ": "continue", | ||
"π€": "return", | ||
"π€²": "var", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package translator | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// The suffix of a mojo-gojo file. | ||
const suffix = ".mg" | ||
|
||
// TranslateDirectory processes all mojo-gojo files in the provided directory, | ||
// translates them to go files, and saves the results. | ||
// It returns a list of the created go files. | ||
func TranslateDirectory(dir string) ([]string, error) { | ||
var generatedFiles []string | ||
|
||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() && strings.HasSuffix(info.Name(), suffix) { | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
translatedContent := translateEmojiToKeyword(string(data)) | ||
|
||
tempFilePath := strings.TrimSuffix(path, suffix) + ".go" | ||
err = ioutil.WriteFile(tempFilePath, []byte(translatedContent), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
generatedFiles = append(generatedFiles, tempFilePath) | ||
} | ||
return nil | ||
}) | ||
|
||
return generatedFiles, err | ||
} | ||
|
||
// translateEmojiToKeyword translates the content of a mojo-gojo file to a go file. | ||
func translateEmojiToKeyword(content string) string { | ||
for emoji, keyword := range emojiToKeyword { | ||
content = strings.ReplaceAll(content, emoji, keyword) | ||
} | ||
return content | ||
} |