def test_ptrtoptrdecl(): testcode = """ int16_t (*motion_val[2])[2]; """ state = helpers_test.parse(testcode) v = state.vars["motion_val"] pprint((v, v.type))
def test_funcptrdecl(): testcode = """ int16_t (*f)(); int16_t (*g)(char a, void*); int (*h); // ISO/IEC 9899:TC3 : C99 standard int fx(void), *fip(), (*pfi)(); // example 1, page 120 int (*apfi[3])(int *x, int *y); // example 2, page 120 //int (*fpfi(int (*)(long), int))(int, ...); // example 3, page 120 """ state = helpers_test.parse(testcode) f = state.vars["f"] g = state.vars["g"] assert f.name == "f" assert isinstance(f.type, CFuncPointerDecl) assert f.type.type == CStdIntType("int16_t") assert f.type.args == [] assert isinstance(g.type, CFuncPointerDecl) gargs = g.type.args assert isinstance(gargs, list) assert len(gargs) == 2 assert isinstance(gargs[0], CFuncArgDecl) assert gargs[0].name == "a" assert gargs[0].type == CBuiltinType(("char", )) assert gargs[1].name is None assert gargs[1].type == CBuiltinType(("void", "*")) h = state.vars["h"] #assert h.type == CPointerType(CBuiltinType(("int",))) # TODO? # TODO? #fx = state.funcs["fx"] # fx is a function `int (void)` #assert fx.type == CBuiltinType(("int",)) #assert fx.args == [] # TODO? #fip = state.funcs["fip"] # fip is a function `int* (void)` #assert fip.type == CPointerType(CBuiltinType(("int",))) #assert fip.args == [] pfi = state.vars["pfi"] # pfi is a function-ptr to `int ()` assert isinstance(pfi.type, CFuncPointerDecl) assert pfi.type.type == CBuiltinType(("int", )) assert pfi.type.args == [] apfi = state.vars[ "apfi"] # apfi is an array of three function-ptrs `int (int*,int*)`
def test_funcptrdecl(): testcode = """ int16_t (*f)(); int16_t (*g)(char a, void*); int (*h); // ISO/IEC 9899:TC3 : C99 standard int fx(void), *fip(), (*pfi)(); // example 1, page 120 int (*apfi[3])(int *x, int *y); // example 2, page 120 //int (*fpfi(int (*)(long), int))(int, ...); // example 3, page 120 """ state = helpers_test.parse(testcode) f = state.vars["f"] g = state.vars["g"] assert f.name == "f" assert isinstance(f.type, CFuncPointerDecl) assert f.type.type == CStdIntType("int16_t") assert f.type.args == [] assert isinstance(g.type, CFuncPointerDecl) gargs = g.type.args assert isinstance(gargs, list) assert len(gargs) == 2 assert isinstance(gargs[0], CFuncArgDecl) assert gargs[0].name == "a" assert gargs[0].type == CBuiltinType(("char",)) assert gargs[1].name is None assert gargs[1].type == CBuiltinType(("void","*")) h = state.vars["h"] #assert h.type == CPointerType(CBuiltinType(("int",))) # TODO? # TODO? #fx = state.funcs["fx"] # fx is a function `int (void)` #assert fx.type == CBuiltinType(("int",)) #assert fx.args == [] # TODO? #fip = state.funcs["fip"] # fip is a function `int* (void)` #assert fip.type == CPointerType(CBuiltinType(("int",))) #assert fip.args == [] pfi = state.vars["pfi"] # pfi is a function-ptr to `int ()` assert isinstance(pfi.type, CFuncPointerDecl) assert pfi.type.type == CBuiltinType(("int",)) assert pfi.type.args == [] apfi = state.vars["apfi"] # apfi is an array of three function-ptrs `int (int*,int*)`
def test_simplevardecl(): testcode = """ int16_t a; int b = 42; void* c = &b; int* d = &b; char e, *f = "abc", g, **h = &f; """ state = helpers_test.parse(testcode) a = state.vars["a"] b = state.vars["b"] c = state.vars["c"] d = state.vars["d"] e = state.vars["e"] f = state.vars["f"] g = state.vars["g"] h = state.vars["h"] for v in "abcdefgh": var = locals()[v] assert state.vars[v] is var assert var.name == v assert a.type == CStdIntType("int16_t") assert a.body is None assert b.type == CBuiltinType(("int",)) assert b.body is not None assert b.body.getConstValue(state) == 42 assert c.type == CBuiltinType(("void","*")) #pprint(c.body) TODO: check <CStatement <COp '&'> <CStatement <CVarDecl 'b' ... assert d.type == CPointerType(CBuiltinType(("int",))) assert e.type == CBuiltinType(("char",)) assert f.type == CPointerType(e.type) assert h.type == CPointerType(f.type) assert f.body.getConstValue(state) == "abc"
def test_simplevardecl(): testcode = """ int16_t a; int b = 42; void* c = &b; int* d = &b; char e, *f = "abc", g, **h = &f; """ state = helpers_test.parse(testcode) a = state.vars["a"] b = state.vars["b"] c = state.vars["c"] d = state.vars["d"] e = state.vars["e"] f = state.vars["f"] g = state.vars["g"] h = state.vars["h"] for v in "abcdefgh": var = locals()[v] assert state.vars[v] is var assert var.name == v assert a.type == CStdIntType("int16_t") assert a.body is None assert b.type == CBuiltinType(("int", )) assert b.body is not None assert b.body.getConstValue(state) == 42 assert c.type == CBuiltinType(("void", "*")) #pprint(c.body) TODO: check <CStatement <COp '&'> <CStatement <CVarDecl 'b' ... assert d.type == CPointerType(CBuiltinType(("int", ))) assert e.type == CBuiltinType(("char", )) assert f.type == CPointerType(e.type) assert h.type == CPointerType(f.type) assert f.body.getConstValue(state) == "abc"
def test_interpreter_helloworld(): testcode = """ #include <stdio.h> int main(int argc, char** argv) { printf("Hello %s\n", "world"); printf("args: %i\n", argc); int i; for(i = 0; i < argc; ++i) printf("%s\n", argv[i]); fflush(stdout); } """ state = helpers_test.parse(testcode, withGlobalIncludeWrappers=True) from cparser import interpreter interp = interpreter.Interpreter() interp.register(state) def dump(): for f in state.contentlist: if not isinstance(f, cparser.CFunc): continue if not f.body: continue print() print("parsed content of " + str(f) + ":") for c in f.body.contentlist: print(c) print() print("PyAST of main:") interp.dumpFunc("main") #interpreter.runFunc("main", len(sys.argv), sys.argv + [None]) import os # os.pipe() returns pipein,pipeout pipes = os.pipe(), os.pipe() # for stdin/stdout+stderr if hasattr(os, "set_inheritable"): # Python 3 by default will close all fds in subprocesses. This will avoid that. os.set_inheritable(pipes[0][0], True) os.set_inheritable(pipes[0][1], True) os.set_inheritable(pipes[1][0], True) os.set_inheritable(pipes[1][1], True) pid = os.fork() if pid == 0: # child os.close(pipes[0][1]) os.close(pipes[1][0]) os.dup2(pipes[0][0], sys.__stdin__.fileno()) os.dup2(pipes[1][1], sys.__stdout__.fileno()) os.dup2(pipes[1][1], sys.__stderr__.fileno()) try: interp.runFunc("main", 2, ["./test", "abc", None]) except SystemExit: raise except BaseException: better_exchook.better_exchook(*sys.exc_info()) print("Normal exit.") os._exit(0) return # parent os.close(pipes[0][0]) os.close(pipes[1][1]) child_stdout = os.fdopen(pipes[1][0], "rb", 0) child_stdout = child_stdout.readlines() if PY3: child_stdout = [l.decode("utf8") for l in child_stdout] expected_out = [ "Hello world\n", "args: 2\n", "./test\n", "abc\n", "Normal exit.\n", ] if expected_out != child_stdout: print("Got output:") print("".join(child_stdout)) dump() print("run directly here now:") interp.runFunc("main", 2, ["./test", "abc", None]) raise Exception("child stdout %r" % (child_stdout,))
def test_parse2(): helpers_test.parse("int16_t (*g)(char a, void*);")
def test_parse1(): helpers_test.parse("int16_t (*f)();")
def test_parse5(): helpers_test.parse("int (*apfi[3])(int *x, int *y);")
def test_parse4(): helpers_test.parse("int fx(void), *fip(), (*pfi)();")
def test_parse3(): helpers_test.parse("int (*h);")
def test_interpreter_helloworld(): testcode = """ #include <stdio.h> int main(int argc, char** argv) { printf("Hello %s\n", "world"); printf("args: %i\n", argc); int i; for(i = 0; i < argc; ++i) printf("%s\n", argv[i]); } """ state = helpers_test.parse(testcode, withGlobalIncludeWrappers=True) import interpreter interp = interpreter.Interpreter() interp.register(state) def dump(): for f in state.contentlist: if not isinstance(f, cparser.CFunc): continue if not f.body: continue print print "parsed content of " + str(f) + ":" for c in f.body.contentlist: print c print print "PyAST of main:" interp.dumpFunc("main") #interpreter.runFunc("main", len(sys.argv), sys.argv + [None]) import os # os.pipe() returns pipein,pipeout pipes = os.pipe(), os.pipe() # for stdin/stdout+stderr if os.fork() == 0: # child os.close(pipes[0][1]) os.close(pipes[1][0]) os.dup2(pipes[0][0], sys.__stdin__.fileno()) os.dup2(pipes[1][1], sys.__stdout__.fileno()) os.dup2(pipes[1][1], sys.__stderr__.fileno()) try: interp.runFunc("main", 2, ["./test", "abc", None]) except BaseException: better_exchook.better_exchook(*sys.exc_info()) os._exit(0) return # parent os.close(pipes[0][0]) os.close(pipes[1][1]) child_stdout = os.fdopen(pipes[1][0]) child_stdout = child_stdout.readlines() expected_out = [ "Hello world\n", "args: 2\n", "./test\n", "abc\n", ] if expected_out != child_stdout: print "Got output:" print "".join(child_stdout) dump() assert False