그래서, 입력으로 바이트 코드 파일을 읽는 간단한 AOT 가상 컴퓨터를 golang에서 만들려고합니다. 아주 기본적으로 노력하고있어, 파일에 바이트를 쓰고 ioutil
으로 읽습니다. 그러나 null 참조 오류가 발생했습니다. 쓰기 후 이동 파일에서 바이트를 읽을 수 없습니다.
이
파일에 쓰기에 사용되는 내 파이썬 코드입니다 : 이것은 바이트package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
//gets command line args
userArgs := os.Args[1:]
bytes, err := ioutil.ReadFile(userArgs[0]) // just pass the file name
if err != nil {
fmt.Print(err)
}
fmt.Println(bytes)
machine := NewEnv()
ReadBytes(machine, bytes)
}
을 읽을 사용하고 내 이동 파일의 코드
btest = open("test.thief", "w")
bytes_to_write = bytearray([1, 44, 56, 55, 55, 0])
btest.write(bytes_to_write)
btest.close()
그리고 이것은이다 오류 나는 점점 :
Joshuas-MacBook-Pro-3:thief Josh$ ./bin/Thief test.thief
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2af3]
goroutine 1 [running]:
main.ReadBytes(0xc82000e390, 0xc82000c480, 0x6, 0x206)
/Users/Josh/thief/src/Thief/read.go:26 +0x353
main.main()
/Users/Josh/thief/src/Thief/Main.go:18 +0x1f2
또한 파이썬 코드로 이진 모드로 파일을 열어 보았지만 p 같은 오류가 발생합니다. 기본적으로이 목표는 바이트 배열을 추출 할 수 있기를 원하지만 테마를 작성하는 방법도 있습니다.
바이트를 잘못 쓰고 있습니까? 여기
는 16 진 덤프입니다 :012c 3837 3700
편집 :이
package main
//file for machine functions and operations
//prints string format of some bytes
func print(env Env, data []byte) {
fmt.Println(string(data))
}
var OPERS map[byte]func(Env, []byte)
//0 is reserved as the null terminator
func init(){
OPERS = map[byte]func(Env, []byte){
1:print,
}
}
env.go
opfuncs.go 내 이동 파일을의 나머지 부분은
입니다 012 3,516,
package main
//file for the env struct
type Env struct {
items map[string]interface{}
}
func NewEnv() Env {
return Env{make(map[string]interface{})}
}
//general getter
func (self *Env) get(key string) interface{} {
val, has := self.items[key]
if has {
return val
} else {
return nil
}
}
//general setter
func (self *Env) set(key string, value interface{}) {
self.items[key] = value
}
func (self *Env) contains(key string) bool {
_, has := self.items[key]
return has
}
func (self *Env) declare(key string) {
self.items[key] = Null{}
}
func (self *Env) is_null(key string) bool {
_, ok := self.items[key].(Null)
return ok
}
//deletes an item from storage
func (self *Env) del(key string) {
delete(self.items, key)
}
read.go 당신이 당신의 파이썬 코드에서 파일을 작성하지 않는 것 같습니다
package main
//file that contains the reading function for the VM
func ReadBytes(env Env, in []byte) {
bytes := make([]byte, 1)
fn := byte(0)
mode := 0
for i:=0;i<len(in);i++ {
switch mode {
case 0:
fn = byte(i)
mode = 1
case 1:
if i != len(in)-1 {
if in[i+1] == 0 {
bytes = append(bytes, in[i])
mode = 2
} else {
bytes = append(bytes, in[i])
}
} else {
bytes = append(bytes, in[i])
}
case 2:
OPERS[fn](env, bytes)
mode = 0
fn = byte(0)
bytes = make([]byte, 1)
}
}
}
하면 내용을 볼 수있는 파일을 Hexdump에서는 할 수
– Sairam'012c 3837 3700' –
그 hexdump –