diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out diff --git a/internal/tokenizer.go b/internal/tokenizer.go index cec6e33..6bc80c9 100644 --- a/internal/tokenizer.go +++ b/internal/tokenizer.go @@ -1,4 +1,4 @@ -package tokenizer +package compiler type TokenType int diff --git a/main.go b/main.go index c842358..8485895 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,34 @@ package main import ( "fmt" + "io/fs" "os" + "path/filepath" "regexp" + // "strings" ) -func process(Path string) { - fmt.Println(Path) +func process(inputPath string) { + // outputPath := strings.Replace(inputPath, ".jack", ".xml", 1) + source, err := os.ReadFile(inputPath) + + if err != nil { + panic(err) + } + + fmt.Println(string(source)) +} + +func walker(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + + if match, _ := regexp.MatchString(".+\\.jack$", path); match { + process(path) + } + + return nil } func main() { @@ -17,9 +39,25 @@ func main() { panic("You must provide a path for a Jack file or a directory that contains Jack files to compile!") } - Path := args[1] + info, err := os.Stat(args[1]) - if match, _ := regexp.MatchString(".+\\.jack", Path); match { - process(Path) + if err != nil { + panic(err) } + + if info.IsDir() { + err := filepath.WalkDir(args[1], walker) + + if err != nil { + panic(err) + } + + return + } + + if match, _ := regexp.MatchString(".+\\.jack$", args[1]); !match { + panic("You must provide a path for a Jack file or a directory that contains Jack files to compile!") + } + + process(args[1]) }