-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (42 loc) · 1.13 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
codewriter "vm-translator/lib/code-writer"
parser "vm-translator/lib/parser"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
inputFileName := os.Args[1]
outputFilename := strings.TrimSuffix(inputFileName, filepath.Ext(inputFileName)) + ".asm"
className := strings.TrimSuffix(filepath.Base(inputFileName), filepath.Ext(inputFileName))
inFile, err := os.Open(inputFileName)
check(err)
defer inFile.Close()
reader := bufio.NewReader(inFile)
outFile, err := os.Create(outputFilename)
check(err)
defer outFile.Close()
writer := bufio.NewWriter(outFile)
parser.Init(reader)
codewriter.Init(writer, className)
for parser.HasMoreLines() {
parser.Advance()
index, _ := strconv.Atoi(parser.Arg2())
if parser.CommandType() == parser.C_PUSH || parser.CommandType() == parser.C_POP {
codewriter.WritePushPop(parser.CommandType().String(), parser.Arg1(), index)
} else if parser.CommandType() == parser.C_ARITHMETIC {
codewriter.WriteArithmetic(parser.Arg1())
}
}
writer.Flush()
fmt.Println("Compilation succeed!")
}