aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-03-01 17:14:49 +0100
committerDimitri Sokolyuk <demon@dim13.org>2019-03-01 17:14:49 +0100
commite4b7bcd0903b4d2bcfe305e4e78415e32a3a32b5 (patch)
tree2543af136980a5d5aa85c61afbb9e42d814e8f3a
parent1f029b4c3173d2102fda5d91ec5e9e860ccd3428 (diff)
add encode
-rw-r--r--parse.go7
-rw-r--r--parse_test.go2
2 files changed, 7 insertions, 2 deletions
diff --git a/parse.go b/parse.go
index 76579ae..0c9d5a2 100644
--- a/parse.go
+++ b/parse.go
@@ -2,7 +2,7 @@ package j1
import "fmt"
-// Decode instruction
+// Decode instruction from binary form
func Decode(v uint16) Instruction {
switch {
case isLiteral(v):
@@ -19,6 +19,11 @@ func Decode(v uint16) Instruction {
return nil
}
+// Encode instruction to binary form
+func Encode(i Instruction) uint16 {
+ return i.compile()
+}
+
// Instruction interface
type Instruction interface {
value() uint16
diff --git a/parse_test.go b/parse_test.go
index 859f933..3e7ee2c 100644
--- a/parse_test.go
+++ b/parse_test.go
@@ -39,7 +39,7 @@ func TestDecode(t *testing.T) {
if ins != tc.ins {
t.Errorf("got %v, want %v", ins, tc.ins)
}
- if v := ins.compile(); v != tc.bin {
+ if v := Encode(ins); v != tc.bin {
t.Errorf("got %v, want %v", v, tc.bin)
}
})