Beispiel #1
0
	def compile(self, code):
		""""""
		from pytcc import TCCState as TCC
		python_dir = pathlib.Path(sys.exec_prefix)
		comp = TCC()
		comp.add_include_path(str(python_dir / 'include'))
		comp.add_library_path(str(python_dir))
		comp.add_library('python3')
		ret = None
		try:
			#print('Compiling...', id(self))
			comp.compile_string(code)
			#print('Relocating...')
			#comp.relocate()
			self.code_buffer = comp.get_bytes()
			#print('Prototyping...')
			func_proto = ctypes.CFUNCTYPE(
				ctypes.py_object,  # Return type
				ctypes.py_object,  # ARGS
				ctypes.py_object,  # SYMBOL_TABLE
				ctypes.py_object,  # BRANCHES
			)
			#print('Getting Symbol...')
			craft_main = comp.get_symbol('craft_main')
			ret = func_proto(craft_main)
		except:
			traceback.print_exc()
		finally:
			del comp
			return ret, self.code_buffer
Beispiel #2
0
from ctypes import CFUNCTYPE, POINTER, c_int
from pytcc import TCC
comp = TCC()
comp.add_library_path("./")
comp.add_file("factorial.c")
comp.relocate()
factorial = comp.get_symbol("factorial")
factorialsignature = CFUNCTYPE(c_int, c_int)
func = factorialsignature(factorial)
print(func(6))  # Returns the factorial of 6
Beispiel #3
0
int sum(int a, int b)
{
	return a + b;
}

int main(int argc, char **argv)
{
	printf("%s %d\n", argv[0], sum(2, 2));
	return 0;
}
'''


def square(n):
    return n**2


c_square = CFUNCTYPE(c_int, c_int)(square)

python_dir = Path(sys.executable).parent
comp = TCC()
comp.add_library_path(f'{python_dir}')
comp.add_include_path(f'{python_dir / "include"}')
comp.add_library('python37')
comp.add_symbol('square', c_square)
comp.compile_string(source)
comp.relocate()
pop = CFUNCTYPE(py_object)(comp.get_symbol('pop'))

print(pop())
Beispiel #4
0
from pytcc import TCC, FILE
comp = TCC(output_type=FILE)
comp.add_library_path("./")
comp.compile_file("program.c")
comp.output_file("program.exe")
Beispiel #5
0
from pytcc import TCC
comp = TCC()
comp.preprocessor_symbols["DEBUG"] = "1"
comp.add_library_path("./")
comp.add_file("test.c")
comp.compile_string('''
int main(int argc, char **argv)
    {
    printf("%s %d", argv[0], sum(2, 2));
    return 0;
}
''')
comp.run(["test"])
Beispiel #6
0
from pytcc import TCC
import sys
comp = TCC()
comp.add_library_path("./")
comp.compile_string('''
int main(int argc, char **argv)
    {
    int i;
    for (i = 0; i < argc; i++)
        printf("%s", argv[i]);
    return 0;
}
''')
comp.run(sys.argv)
Beispiel #7
0
from ctypes import c_int, CFUNCTYPE, POINTER
from pytcc import TCC
comp = TCC()
comp.preprocessor_symbols["DEBUG"] = "1"
comp.add_library_path("./")


def square(n):
    return n**2


squaresignature = CFUNCTYPE(c_int, c_int)
func = squaresignature(square)

comp.add_symbol("square", func)
comp.compile_string('''
int main(int argc, char **argv)
    {
    printf("%d", square(6));
    return 0;
}
''')
comp.run()
Beispiel #8
0
from pytcc import TCC, FILE
comp = TCC(output_type=FILE)
comp.preprocessor_symbols["DEBUG"] = "1"
comp.add_library_path("./")
comp.add_file("test.c")
comp.compile_string('''
int main()
    {
    printf("%d", sum(2, 2));
    return 0;
}
''')
comp.output_file("b.exe")