coding-nim
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecoding-nim
coding-nim
Purpose
用途
This skill equips the AI to handle Nim 2.x programming tasks, focusing on advanced features like macros, templates, compile-time execution, memory management (ARC/ORC), FFI for interoperability, Nimble for package handling, and systems programming. Use it to generate, debug, and optimize Nim code efficiently.
该技能让AI能够处理Nim 2.x编程任务,重点覆盖宏、模板、编译时执行、内存管理(ARC/ORC)、用于跨语言互操作的FFI、用于包管理的Nimble以及系统编程等高级特性,可高效生成、调试和优化Nim代码。
When to Use
适用场景
Apply this skill for systems-level programming needing low-level control, such as embedded systems, performance-critical apps, or when integrating with C/C++ via FFI. Use it for projects requiring compile-time metaprogramming (e.g., via macros) or automatic memory management with ARC/ORC to avoid manual garbage collection.
适用于需要低级别控制的系统级编程场景,例如嵌入式系统、性能关键型应用,或是需要通过FFI与C/C++集成的场景。也可用于需要编译时元编程(例如通过macros实现)或者使用ARC/ORC自动内存管理以避免手动垃圾回收的项目。
Key Capabilities
核心能力
- Macros for code generation: Define reusable code transformations at compile-time.
- Templates for type-safe string-based metaprogramming.
- Compile-time execution: Run code during compilation using blocks.
static - Memory management: Switch between ARC (automatic reference counting) and ORC (optional reference counting) via compiler flags.
- FFI: Call external libraries (e.g., C functions) without wrappers.
- Nimble: Manage dependencies and build projects like a package manager.
- Systems programming: Direct hardware access, concurrency, and cross-platform compilation.
- 用于代码生成的Macros:在编译期定义可复用的代码转换逻辑。
- 用于类型安全的字符串元编程的Templates。
- 编译时执行:使用块在编译阶段运行代码。
static - 内存管理:可通过编译参数在ARC(自动引用计数)和ORC(可选引用计数)之间切换。
- FFI:无需额外封装即可调用外部库(例如C函数)。
- Nimble:作为包管理器管理依赖并构建项目。
- 系统编程:支持直接硬件访问、并发能力和跨平台编译。
Usage Patterns
使用模式
To accomplish tasks, structure Nim code with modules and use the compiler for builds. For macros, define them in a separate proc and invoke at compile-time. When writing FFI code, use the pragma for C functions. For memory management, specify or flags during compilation. Always test code with before full builds to catch errors early. Integrate templates for generic functions to reduce boilerplate.
importc--gc:arc--gc:orcnim check完成任务时使用模块组织Nim代码,并通过编译器进行构建。对于Macros,需在单独的过程中定义并在编译时调用。编写FFI代码时,对C函数使用编译指示。内存管理可在编译时指定或参数。完整构建前始终使用测试代码,提前发现错误。为泛型函数集成Templates以减少样板代码。
importc--gc:arc--gc:orcnim checkCommon Commands/API
常用命令/API
Use the Nim compiler () for core operations. Compile a file: (flags: for run, for minimal output). For Nimble, install packages: . Define a macro:
nimnim c --verbosity:0 -r main.nim-r--verbosity:0nimble install somepkgnim
macro doubleIt(x: expr): stmt =
result = quote do: `x` * 2Call it as . For FFI, import C: . Config format: Use for settings, e.g., to specify compiler. Environment variables: Set for package cache.
echo doubleIt(5)proc printf(format: cstring; args: varargs[pointer]) {.importc: "printf", header: "<stdio.h>".}nim.cfggcc.exe = "gcc"$NIMBLE_DIR使用Nim编译器()执行核心操作。编译文件命令:(参数说明:表示编译后直接运行,表示仅输出最低限度信息)。Nimble安装包命令:。定义Macro示例:
nimnim c --verbosity:0 -r main.nim-r--verbosity:0nimble install somepkgnim
macro doubleIt(x: expr): stmt =
result = quote do: `x` * 2调用方式为。FFI导入C函数示例:。配置格式:使用存放配置,例如用于指定C编译器。环境变量:设置指定包缓存路径。
echo doubleIt(5)proc printf(format: cstring; args: varargs[pointer]) {.importc: "printf", header: "<stdio.h>".}nim.cfggcc.exe = "gcc"$NIMBLE_DIRIntegration Notes
集成说明
Integrate Nim with other languages via FFI; for C++, use . To embed in a project, compile Nim code to a shared library: . For CI/CD, use GitHub Actions with: . If auth is needed (e.g., for Nimble registry), set env vars like for private repos. Link against external libs: Add for OpenSSL. Ensure path configurations match, e.g., add in .
importcppnim c --app:lib -d:release mylib.nimrun: nim c --opt:speed file.nim$NIMBLE_TOKEN--passL:-lsslpath = "/path/to/headers"nim.cfg通过FFI将Nim与其他语言集成;对接C++可使用。要嵌入到其他项目中,可将Nim代码编译为共享库:。CI/CD场景下可在GitHub Actions中使用:。如果需要身份验证(例如访问私有Nimble注册表),可设置等环境变量访问私有仓库。链接外部库:添加参数链接OpenSSL。确保路径配置匹配,例如在中添加指定头文件路径。
importcppnim c --app:lib -d:release mylib.nimrun: nim c --opt:speed file.nim$NIMBLE_TOKEN--passL:-lsslnim.cfgpath = "/path/to/headers"Error Handling
错误处理
In Nim, use try-except blocks for runtime errors:
nim
try:
raise newException(ValueError, "Invalid input")
except ValueError:
echo "Handled error: ", getCurrentExceptionMsg()For compile-time errors, enable detailed output with . Check for memory issues by switching to ORC: . Parse compiler output for specifics; common flags include to catch undeclared vars. Log errors with or a logging library like .
nim c --verbosity:2 file.nimnim c --gc:orc file.nim--warnings:onechochroniclesNim中使用try-except块处理运行时错误:
nim
try:
raise newException(ValueError, "Invalid input")
except ValueError:
echo "Handled error: ", getCurrentExceptionMsg()编译时错误可使用启用详细输出排查。切换到ORC检查内存问题:。解析编译器输出获取具体错误信息;常用参数包括以捕获未声明变量等问题。使用或等日志库记录错误。
nim c --verbosity:2 file.nimnim c --gc:orc file.nim--warnings:onechochroniclesConcrete Usage Examples
具体使用示例
- Define and use a macro for code generation: To create a loop macro, write:
nim
macro forEach(items: seq, body: stmt): stmt =
result = quote do: for item in `items`: `body`Use it as: . This generates efficient loops at compile-time.
forEach(@[1, 2, 3], echo item)- Use FFI to call a C function: Import and call a C printf:
nim
proc cPrintf(format: cstring) {.importc: "printf", header: "<stdio.h>".}
cPrintf("%s\n", "Hello from Nim")Compile with to link C headers, enabling seamless integration.
nim c --passC:-I/usr/include file.nim- 定义并使用Macro实现代码生成:要创建循环Macro,编写如下代码:
nim
macro forEach(items: seq, body: stmt): stmt =
result = quote do: for item in `items`: `body`调用方式:。该代码会在编译期生成高效的循环逻辑。
forEach(@[1, 2, 3], echo item)- 使用FFI调用C函数:导入并调用C的printf函数:
nim
proc cPrintf(format: cstring) {.importc: "printf", header: "<stdio.h>".}
cPrintf("%s\n", "Hello from Nim")使用编译以链接C头文件,实现无缝集成。
nim c --passC:-I/usr/include file.nimGraph Relationships
关联关系
- Related to cluster: coding
- Connected via tags: nim (direct link to language-specific skills), systems (links to low-level programming tools), coding (broad connections to other coding skills like coding-python or coding-c)
- 关联集群:coding
- 标签关联:nim(直接关联对应语言专属技能)、systems(关联低级编程工具)、coding(广泛关联其他编程技能,例如coding-python或coding-c)