feat: check the validity of the passed argument

This commit is contained in:
2026-04-17 12:26:37 +01:00
parent a54831a979
commit 982045cce6
3 changed files with 45 additions and 6 deletions
+1
View File
@@ -0,0 +1 @@
out
+1 -1
View File
@@ -1,4 +1,4 @@
package tokenizer
package compiler
type TokenType int
+43 -5
View File
@@ -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])
}