2017-01-14 5 views
-1

그래서, 입력으로 바이트 코드 파일을 읽는 간단한 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) 
     } 
    } 
} 
+0

하면 내용을 볼 수있는 파일을 Hexdump에서는 할 수

package main import ( "fmt" "io/ioutil" "os" ) func main() { //gets command line args userArgs := os.Args[1:] _, err := ioutil.ReadFile(userArgs[0]) // just pass the file name if err != nil { fmt.Print(err) } } 
Sairam

+0

'012c 3837 3700' –

+0

그 hexdump –

답변

2

파이썬 3를 사용하는 경우

이 파일은 바이너리 모드로 열어야합니다?
0

?

나는 golang이 파일 내용을 찾을 수없는 이유를 설명 할 수있는 세 번째 줄 뒤에

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: write() argument must be str, not bytearray 

를 얻을.

또한 bytes을 사용하지 않으므로 gocode가 컴파일되지 않습니다. 대신해라.

btest = open("test.thief", "wb") 
+0

Im on python3, 아마도 thats why? 다른 일부 코드에서는 바이트를 사용하지만 오류가 발생하지 않으므로 관련이 없으므로 제거했습니다. –

+0

이것은 파일의 16 진수입니다 –

+0

012c 3837 3700 –

관련 문제