diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go deleted file mode 100644 index b23ff6c..0000000 --- a/analyzer/analyzer.go +++ /dev/null @@ -1,23 +0,0 @@ -package analyzer - -import ( - "github.com/MlkMahmud/jack-compiler/lexer" - "github.com/MlkMahmud/jack-compiler/parser" -) - -type JackAnalyzer struct { - lexer *lexer.Lexer - parser *parser.Parser -} - -func NewAnalyzer() *JackAnalyzer { - return &JackAnalyzer{ - lexer: lexer.NewLexer(), - parser: parser.NewParser(), - } -} - -func (analyzer *JackAnalyzer) Run(src string) { - tokens := analyzer.lexer.Tokenize(src) - analyzer.parser.Parse(tokens) -} \ No newline at end of file diff --git a/analyzer/analyzer_test.go b/analyzer/analyzer_test.go deleted file mode 100644 index 0a32055..0000000 --- a/analyzer/analyzer_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package analyzer_test - -import ( - "io" - "log" - "os" - "path" - "regexp" - "strings" - "testing" - - . "github.com/MlkMahmud/jack-compiler/analyzer" -) - -const TEST_DATA_PATH = "../testdata" - -func stripLineBreakAndTabCharacters(filename string) string { - file, _ := os.Open(filename) - bytes, err := io.ReadAll(file) - - if err != nil { - log.Fatal(err) - } - - fileContent := string(bytes) - re := regexp.MustCompile("[\r\t\n]") - return re.ReplaceAllLiteralString(fileContent, "") -} - -func TestJackAnalyzer(t *testing.T) { - files := []string{"Array", "Square", "SquareGame"} - jackAnalyzer := NewAnalyzer() - - for _, file := range files { - t.Run(file, func(t *testing.T) { - srcFilePath := path.Join(TEST_DATA_PATH, strings.Join([]string{file, "jack"}, ".")) - outFilePath := path.Join(TEST_DATA_PATH, strings.Join([]string{file, "xml"}, ".")) - cmpFilePath := path.Join(TEST_DATA_PATH, "expected", strings.Join([]string{file, "xml"}, ".")) - - jackAnalyzer.Run(srcFilePath) - - if stripLineBreakAndTabCharacters(cmpFilePath) != stripLineBreakAndTabCharacters(outFilePath) { - t.Errorf("Expected content of %s to match content of %s", outFilePath, cmpFilePath) - } - }) - } -} diff --git a/main.go b/main.go deleted file mode 100644 index 77be1ff..0000000 --- a/main.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "log" - "os" - "path/filepath" - "strings" - - "github.com/MlkMahmud/jack-compiler/analyzer" -) - -func printHelpMessage() { - log.SetFlags(0) - log.Fatalln(("usage:\n go run . .\t\t\tCompiles all the .jack files in the current directory\n go run . \t\tCompiles the specified .jack file\n go run . src/\t\t\tCompiles all the .jack files in the specified directory")) -} - -func main() { - if len(os.Args) != 2 { - printHelpMessage() - } - - JackAnalyzer := analyzer.NewAnalyzer() - src := os.Args[1] - stat, err := os.Stat(src) - if err != nil { - log.Fatal(err) - } - - if stat.IsDir() { - dir, err := os.ReadDir(src) - if err != nil { - log.Fatal(err) - } - - for _, file := range dir { - if strings.HasSuffix(file.Name(), ".jack") { - fileName := filepath.Join(src, file.Name()) - JackAnalyzer.Run(fileName) - } - } - } else { - if !strings.HasSuffix(src, ".jack") { - printHelpMessage() - } - JackAnalyzer.Run(src) - } -}