Repository navigation
Expand file tree
/
Copy pathpython_to_go.go
More file actions
69 lines (64 loc) · 1.57 KB
/
Copy pathpython_to_go.go
File metadata and controls
69 lines (64 loc) · 1.57 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
package pythontogo
import (
"encoding/json"
"os"
"github.com/AvoidMe/gpython/builtin"
"github.com/AvoidMe/gpython/opcode"
)
type PythonBytecode struct {
Opcode int
Opname string
Arg int
Argval interface{}
Argrepr string
Offset int
Is_jump_target bool
}
func jsonTypeToPy(value interface{}, repr string) builtin.PyObject {
switch v := value.(type) {
case bool:
if v {
return builtin.PyTrue
} else {
return builtin.PyFalse
}
case string:
return &builtin.PyString{Value: []rune(v)}
case float64:
// Golang unmarshal treats every JSON number as float64:
// https://pkg.go.dev/encoding/json#Unmarshal
// Here we're trying to check if source value was int or float
if float64(int64(v)) == v {
return builtin.NewPyInt(int64(v))
} else {
return &builtin.PyFloat{Value: v}
}
case []interface{}:
value := []builtin.PyObject{}
for i := 0; i < len(v); i++ {
value = append(value, jsonTypeToPy(v[i], repr))
}
if repr[0] == '(' {
return &builtin.PyTuple{Value: value}
}
return &builtin.PyList{Value: value}
default:
panic("Json has undefined type")
}
}
func LoadJson() []opcode.Instruction {
var bytecode []PythonBytecode
var output []opcode.Instruction
dat, _ := os.ReadFile("output.json")
json.Unmarshal(dat, &bytecode)
for _, value := range bytecode {
op := opcode.Instruction{Opcode: value.Opcode, Arg: value.Arg}
if value.Argval != nil {
op.Args = jsonTypeToPy(value.Argval, value.Argrepr)
} else {
op.Args = builtin.PyNone
}
output = append(output, op)
}
return output
}