-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (48 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/MlkMahmud/jack-compiler/lexer"
"github.com/MlkMahmud/jack-compiler/parser"
)
func printHelpMessage() {
log.SetFlags(0)
log.Fatalln(("usage:\n go run main.go --src .\t\t\tCompiles all the .jack files in the current directory\n go run main.go --src <fileName.jack>\tCompiles the specified .jack file\n go run main.go --src <dirName>\t\tCompiles all the .jack files in the specified directory"))
}
func main() {
var source string
flag.StringVar(&source, "src", "", "Path to a '.jack' file or a directory containing one or more '.jack' files.")
flag.Parse()
info, err := os.Stat(source)
if err != nil {
log.Fatal(err)
}
lexer := lexer.NewLexer()
parser := parser.NewParser()
jackFiles := []string{}
if info.IsDir() {
entries, err := os.ReadDir(source)
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
if fileName := entry.Name(); strings.HasSuffix(fileName, ".jack") {
jackFiles = append(jackFiles, filepath.Join(source, fileName))
}
}
} else {
if !strings.HasSuffix(source, ".jack") {
printHelpMessage()
}
jackFiles = append(jackFiles, source)
}
for _, src := range jackFiles {
tokens := lexer.Tokenize(src)
class := parser.Parse(tokens)
fmt.Printf("ClassName: %s\nVar Count: %d\nSubroutine Count: %d\n", class.Name, len(class.Vars), len(class.Subroutines))
}
}