-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding first bit of a compiled xml Compiler program
- Loading branch information
Isaiah Becker-Mayer
committed
Mar 16, 2021
1 parent
d0762ce
commit 95ae3c0
Showing
1 changed file
with
55 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,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// isJack determines whether a string ends in ".jack" or not | ||
func isJack(in string) bool { | ||
split := strings.Split(in, ".") | ||
return split[len(split)-1] == "jack" | ||
} | ||
|
||
// jackFilter filters a list of strings down to only those which end in ".jack" | ||
func jackFilter(ss []string) (ret []string) { | ||
for _, s := range ss { | ||
if isJack(s) { | ||
ret = append(ret, s) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func main() { | ||
// First argument should be .jack file or directory of .jack files. Currently | ||
// do not support multiple directory builds. | ||
toCompile := os.Args[1] | ||
var files []string | ||
|
||
// try to list all the files in a directory by the name of the first command line argument, | ||
// or just list the single jack file argument | ||
if err := filepath.Walk(toCompile, func(path string, info os.FileInfo, err error) error { | ||
if path != toCompile && info.IsDir() { | ||
// only walk a single level of directories | ||
return filepath.SkipDir | ||
} | ||
if isJack(path) { | ||
files = append(files, path) | ||
} | ||
return nil | ||
}); err != nil { | ||
panic(err.Error()) | ||
} | ||
if len(files) == 0 { | ||
panic(fmt.Sprintf("invalid compilation input: \"%v\"; first argument must be either a .jack file or a directory with at least one .jack file in it", toCompile)) | ||
} | ||
|
||
// for each jack file | ||
for _, file := range files { | ||
fmt.Println(file) | ||
} | ||
|
||
} |