Skip to content

Commit

Permalink
adding first bit of a compiled xml Compiler program
Browse files Browse the repository at this point in the history
  • 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.
55 changes: 55 additions & 0 deletions Compiler/main.go
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)
}

}

0 comments on commit 95ae3c0

Please sign in to comment.