-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
92 lines (84 loc) · 1.67 KB
/
encode.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
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
package hackassembler
import (
"fmt"
"strconv"
)
func Translate(ins Instruction) (string, error) {
switch ins.Type {
case AInstruction:
num, err := strconv.ParseInt(ins.Symbol, 10, 64)
if err != nil {
return "", err
}
return fmt.Sprintf("%016s", strconv.FormatInt(num, 2)), nil
case CInstruction:
return fmt.Sprintf("111%s%s%s", TranslateComp(ins.Comp), TranslateDest(ins.Dest), TranslateJump(ins.Jump)), nil
case LInstruction:
// TODO: Implement this, later.
return "", nil
default:
return "", fmt.Errorf("unknown instruction type: %s", ins.Type)
}
}
var allDests = map[string]string{
"": "000",
"M": "001",
"D": "010",
"DM": "011",
"MD": "011",
"A": "100",
"AM": "101",
"MA": "101",
"AD": "110",
"DA": "110",
"ADM": "111",
}
func TranslateDest(dest string) string {
return allDests[dest]
}
var allComps = map[string]string{
"0": "0101010",
"1": "0111111",
"-1": "0111010",
"D": "0001100",
"A": "0110000",
"!D": "0001101",
"!A": "0110001",
"-D": "0001111",
"-A": "0110011",
"D+1": "0011111",
"A+1": "0110111",
"D-1": "0001110",
"A-1": "0110010",
"D+A": "0000010",
"D-A": "0010011",
"A-D": "0000111",
"D&A": "0000000",
"D|A": "0010101",
"M": "1110000",
"!M": "1110001",
"-M": "1110011",
"M+1": "1110111",
"M-1": "1110010",
"D+M": "1000010",
"D-M": "1010011",
"M-D": "1000111",
"D&M": "1000000",
"D|M": "1010101",
}
func TranslateComp(comp string) string {
return allComps[comp]
}
var allJumps = map[string]string{
"": "000",
"JGT": "001",
"JEQ": "010",
"JGE": "011",
"JLT": "100",
"JNE": "101",
"JLE": "110",
"JMP": "111",
}
func TranslateJump(jump string) string {
return allJumps[jump]
}