-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (36 loc) · 904 Bytes
/
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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/hlmerscher/hack-assembler-go/assembler"
)
func main() {
var filename string
flag.StringVar(&filename, "f", "", "the filename of the asm source file")
flag.Parse()
if filename == "" {
panic("filename is missing")
}
fmt.Printf("input:\t%s\n", filename)
asmFile := openAsmFile(filename)
defer asmFile.Close()
binary := assembler.Assemble(asmFile)
writeToHackFile(filename, binary)
}
func openAsmFile(filename string) *os.File {
inputFile, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("error opening file <%s>\n", err))
}
return inputFile
}
func writeToHackFile(filename string, content string) {
outputFilename := strings.Replace(filename, ".asm", ".hack", 1)
fmt.Printf("output:\t%s\n", outputFilename)
err := os.WriteFile(outputFilename, []byte(content), 0666)
if err != nil {
panic(err)
}
}