blob: 746899635e83ab8ae6fbe0f7eeb642d5fef4430e (
plain) (
blame)
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
|
from ctypes import *
from enum import Enum
dll = CDLL("./libcreole.so")
class CompileRet(Enum):
COMPILE_OK = 0
OPCODE_READ_ERROR = 1
OPCODE_MALFORMED = 2
ARG_READ_ERROR = 3
ARG_MALFORMED = 4
LAST_READ_ERROR = 5
LAST_MALFORMED = 6
LABEL_OVERFLOW = 7
TYPE_ERROR = 8
class CIns(Structure):
_fields_ = [("opcode", c_int),
("w_flags", c_ubyte * 3),
("w", c_uint * 3)]
class CReader(Structure):
_fields_ = [("p", POINTER(c_ubyte)),
("left", c_size_t)]
def make_uchar_buf(s):
buf = (c_ubyte * len(s))()
buf[:] = s[:]
return buf
class CParseLineException(Exception):
pass
def parse_line(line):
buf = make_uchar_buf(line)
rd = CReader(buf, len(line))
ins = CIns()
ret = dll.creole_parse_line(byref(ins), byref(rd))
if ret != 0:
raise CParseLineException(ret)
return (ins.opcode, list(zip(ins.w_flags, ins.w)))
|