def example_c(): body = c_block() body.add_statement(pp_include("stdio.h")) main_body = c_block() main = c_function_def(c_func_type(c_int("main")), main_body) main_body.add_statement(c_stmt(c_function_call("printf", c_string("Hello, World\\n")))) main_body.add_statement(c_return(c_num(0))) body.add_statement(main) f = open("hello_c.c", "w") f.write(body.to_string()) f.close()
def example_c(): body = c_block() body.add_statement(pp_include('stdio.h')) main_body = c_block() main = c_function_def(c_func_type(c_int('main')), main_body) main_body.add_statement( c_stmt(c_function_call("printf", c_string("Hello, World\\n")))) main_body.add_statement(c_return(c_num(0))) body.add_statement(main) f = open('hello_c.c', 'w') f.write(body.to_string()) f.close()
from derivation_modeling.codegen import sympy_to_c from derivation_modeling.codegen import lang_c import sympy e = trap.final() idx = sympy.Symbol("a") + sympy.Symbol("i") * sympy.Symbol("h") index_trans = {"x": (sympy.Symbol("i"), idx)} print e.rhs f_h = open("ctrap_gen.h", "w") f_cpp = open("ctrap_gen.cpp", "w") inc_f = lang_c.pp_include("ctrap_gen.h", lang_c.pp_include.INCLUDE_TYPE_LOCAL) f_cpp.write(inc_f.to_string()) for narg in range(6): extra_args = ["e" + str(i) for i in range(narg)] (ep, inc) = sympy_to_c.convert( e.rhs, definitions=[definition_of_h], func_name="trap" + str(narg), extra_args=extra_args, index_trans=index_trans, func_type_name="FUNC" + str(narg), ) f_cpp.write(ep.to_string()) f_cpp.write("\n") f_h.write(inc.to_string())
from derivation_modeling.quadrature.trapezoid import trap, definition_of_h from derivation_modeling.codegen import sympy_to_c from derivation_modeling.codegen import lang_c import sympy e = trap.final() idx = sympy.Symbol('a') + sympy.Symbol('i') * sympy.Symbol('h') index_trans = {"x": (sympy.Symbol('i'), idx)} print e.rhs f_h = open('ctrap_gen.h', 'w') f_cpp = open('ctrap_gen.cpp', 'w') inc_f = lang_c.pp_include('ctrap_gen.h', lang_c.pp_include.INCLUDE_TYPE_LOCAL) f_cpp.write(inc_f.to_string()) for narg in range(6): extra_args = ['e' + str(i) for i in range(narg)] (ep, inc) = sympy_to_c.convert(e.rhs, definitions=[definition_of_h], func_name='trap' + str(narg), extra_args=extra_args, index_trans=index_trans, func_type_name='FUNC' + str(narg)) f_cpp.write(ep.to_string()) f_cpp.write('\n') f_h.write(inc.to_string()) f_h.write('\n') f_cpp.close() f_h.close()