Skip to main content

用python写c

Project description

用python写c语言

import l0n0lc as lc
import os
import ctypes

if not os.path.exists('./l0n0lcoutput'):
    os.mkdir('l0n0lcoutput')


fp = open('l0n0lcoutput/helloworld.cpp', 'w')
lc.配置输出函数(fp.write)
lc.c_include('stdio.h')
lc.c_include('stdlib.h')

with lc.如果没有定义('hahaha'):
    lc.c_define('hahaha', '2')


@lc.c_函数(前缀=['extern "C"'])
def test_add(a: lc.c_i32, b: lc.c_i32) -> lc.c_i32:
    with lc.如果(a.大于(100)) as f:
        a(10)
    with lc.否则如果(a.等于(10)):
        a(100)
    with lc.否则():
        a(1000)
    return a + b # type: ignore

@lc.c_函数()
def main(argc: lc.c_i32, argv: lc.c_char_p_p) -> lc.c_i32:
    c =lc.c_i32(test_add(1, 2))
    lc.输出(f'printf("Hello World! c = %d\\n", {c});\n')
    lc.输出('printf("Hello World!\\n");\n')
    with lc.如果定义了('hahaha'):
        lc.输出('printf("哈哈哈!\\n");\n')
    with lc.宏如果('hahaha == 1') as c:
        lc.输出('printf("111!\\n");\n')
        c.否则如果('hahaha == 2')
        lc.输出('printf("否则2!\\n");\n')
        c.否则()
        lc.输出('printf("否则!\\n");\n')
    i = lc.c_i32(0)
    with lc.循环(i.小于(argc)):
        lc.输出(f'printf("i = %d, v = %s\\n", {i}, {argv[i]});\n')
        i += 1
    return 0  # type: ignore


fp.close()

cm = lc.编译()
cm.编译文件('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/helloworld')
os.system('./l0n0lcoutput/helloworld 1 2')

cm.编译为动态库('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/libhelloworld.so')
path = f'{os.getcwd()}/l0n0lcoutput/libhelloworld.so'
print(path)
libhelloworld = ctypes.CDLL(path)
libhelloworld.test_add.argtypes=[ctypes.c_int32, ctypes.c_int32]
libhelloworld.test_add.restype = ctypes.c_int32
a = libhelloworld.test_add(300, 6)
print(a)
a = libhelloworld.test_add(10, 6)
print(a)
a = libhelloworld.test_add(1, 6)
print(a)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

l0n0lc-0.3.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

l0n0lc-0.3.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file l0n0lc-0.3.2.tar.gz.

File metadata

  • Download URL: l0n0lc-0.3.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for l0n0lc-0.3.2.tar.gz
Algorithm Hash digest
SHA256 e8f0b88640ac47043fc59959559ae1def07b033e3c4f548e27f08ec3ced77412
MD5 d0b3bf3ee7e32af83d90b4e9ae0fcf62
BLAKE2b-256 025645f385e51321d96208e104bed5731c2fbfdac974f1401a83ed258984dc3c

See more details on using hashes here.

File details

Details for the file l0n0lc-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: l0n0lc-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for l0n0lc-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4f3901f8fc0a2c4f55004139499725ff61a53c57b00df145ce7a2c8d517da3
MD5 b2bd490388c150541382b28439697ac8
BLAKE2b-256 b9bfe39fb029c09c5fad54c93dddf3fdb28172b14c3a15f668f37d1d1c53947e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page