Repository navigation
Expand file tree
/
Copy pathparser.go
More file actions
122 lines (111 loc) · 2.95 KB
/
Copy pathparser.go
File metadata and controls
122 lines (111 loc) · 2.95 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
)
type Parser struct {
FilesToTranslate []string
}
func (parser *Parser) Initialize(sourceInput string) {
e := filepath.Walk(sourceInput, func(path string, f os.FileInfo, err error) error {
if extension := filepath.Ext(f.Name()); extension == ".asm" {
fullName, _ := filepath.Abs(path)
parser.FilesToTranslate = append(parser.FilesToTranslate, fullName)
}
return err
})
CheckError(e)
}
func (parser *Parser) IsSourceFile(f os.FileInfo) bool {
if extension := filepath.Ext(f.Name()); extension == ".asm" {
return true
}
return false
}
func (parser *Parser) IsLabel(token string) bool {
if strings.HasPrefix(token, "(") {
return true
}
return false
}
func (parser *Parser) IsAInstruction(token string) bool {
if strings.HasPrefix(token, "@") {
return true
}
return false
}
func (parser *Parser) SetDestinationFile(fn string) string {
hackExt := ".hack"
return fn[:len(fn)-len(".asm")] + hackExt
}
func (parser *Parser) GetAssemblyCode(fn string, symbolTable *SymbolTable) []string {
file, err := os.Open(fn)
CheckError(err)
scanner := bufio.NewScanner(file)
counter := -1
var assemblyCode []string
for scanner.Scan() {
line := scanner.Text()
subs := strings.Split(line, "//")
if len(subs[0]) > 0 {
codeSnippet := strings.TrimSpace(subs[0])
if parser.IsLabel(codeSnippet) {
symbolTable.StoreLabel(codeSnippet, counter+1)
} else {
counter += 1
assemblyCode = append(assemblyCode, codeSnippet)
}
}
}
return assemblyCode
}
func (parser *Parser) GetAddress(token string, table *SymbolTable) int {
token = token[1:]
address, err := strconv.Atoi(token)
if err == nil {
return address
}
address, found := table.GetSymbolAddress(token)
if found != true {
address = table.StoreVariable(token)
}
return address
}
func (parser *Parser) ParseCInstruction(codeSnippet string) (string, string, string) {
var comp, dest, jmp string
subs := strings.Split(codeSnippet, "=")
if len(subs) == 2 {
dest, comp, jmp = subs[0], subs[1], "null"
} else {
subs = strings.Split(codeSnippet, ";")
dest, comp, jmp = "null", subs[0], subs[1]
}
return comp, dest, jmp
}
func (parser *Parser) TranslateFile(fn string, st *SymbolTable, tr HackTranslator) {
assemblyCode := parser.GetAssemblyCode(fn, st)
df := parser.SetDestinationFile(fn)
f, err := os.Create(df)
CheckError(err)
buffer := bufio.NewWriter(f)
for _, codeSnippet := range assemblyCode {
if parser.IsAInstruction(codeSnippet) {
address := parser.GetAddress(codeSnippet, st)
code := tr.TranslateAInstruction(address)
buffer.WriteString(code + "\n")
} else {
comp, dest, jmp := parser.ParseCInstruction(codeSnippet)
compCode := tr.TranslateComp(comp)
destCode := tr.TranslateDest(dest)
jmpCode := tr.TranslateJmp(jmp)
code := tr.TranslateCInstruction(compCode, destCode, jmpCode)
buffer.WriteString(code + "\n")
}
}
err = buffer.Flush()
CheckError(err)
f.Close()
}