coding-nim

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

coding-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
    static
    blocks.
  • 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
importc
pragma for C functions. For memory management, specify
--gc:arc
or
--gc:orc
flags during compilation. Always test code with
nim check
before full builds to catch errors early. Integrate templates for generic functions to reduce boilerplate.
完成任务时使用模块组织Nim代码,并通过编译器进行构建。对于Macros,需在单独的过程中定义并在编译时调用。编写FFI代码时,对C函数使用
importc
编译指示。内存管理可在编译时指定
--gc:arc
--gc:orc
参数。完整构建前始终使用
nim check
测试代码,提前发现错误。为泛型函数集成Templates以减少样板代码。

Common Commands/API

常用命令/API

Use the Nim compiler (
nim
) for core operations. Compile a file:
nim c --verbosity:0 -r main.nim
(flags:
-r
for run,
--verbosity:0
for minimal output). For Nimble, install packages:
nimble install somepkg
. Define a macro:
nim
macro doubleIt(x: expr): stmt =  
  result = quote do: `x` * 2
Call it as
echo doubleIt(5)
. For FFI, import C:
proc printf(format: cstring; args: varargs[pointer]) {.importc: "printf", header: "<stdio.h>".}
. Config format: Use
nim.cfg
for settings, e.g.,
gcc.exe = "gcc"
to specify compiler. Environment variables: Set
$NIMBLE_DIR
for package cache.
使用Nim编译器(
nim
)执行核心操作。编译文件命令:
nim c --verbosity:0 -r main.nim
(参数说明:
-r
表示编译后直接运行,
--verbosity:0
表示仅输出最低限度信息)。Nimble安装包命令:
nimble install somepkg
。定义Macro示例:
nim
macro doubleIt(x: expr): stmt =  
  result = quote do: `x` * 2
调用方式为
echo doubleIt(5)
。FFI导入C函数示例:
proc printf(format: cstring; args: varargs[pointer]) {.importc: "printf", header: "<stdio.h>".}
。配置格式:使用
nim.cfg
存放配置,例如
gcc.exe = "gcc"
用于指定C编译器。环境变量:设置
$NIMBLE_DIR
指定包缓存路径。

Integration Notes

集成说明

Integrate Nim with other languages via FFI; for C++, use
importcpp
. To embed in a project, compile Nim code to a shared library:
nim c --app:lib -d:release mylib.nim
. For CI/CD, use GitHub Actions with:
run: nim c --opt:speed file.nim
. If auth is needed (e.g., for Nimble registry), set env vars like
$NIMBLE_TOKEN
for private repos. Link against external libs: Add
--passL:-lssl
for OpenSSL. Ensure path configurations match, e.g., add
path = "/path/to/headers"
in
nim.cfg
.
通过FFI将Nim与其他语言集成;对接C++可使用
importcpp
。要嵌入到其他项目中,可将Nim代码编译为共享库:
nim c --app:lib -d:release mylib.nim
。CI/CD场景下可在GitHub Actions中使用:
run: nim c --opt:speed file.nim
。如果需要身份验证(例如访问私有Nimble注册表),可设置
$NIMBLE_TOKEN
等环境变量访问私有仓库。链接外部库:添加
--passL:-lssl
参数链接OpenSSL。确保路径配置匹配,例如在
nim.cfg
中添加
path = "/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
nim c --verbosity:2 file.nim
. Check for memory issues by switching to ORC:
nim c --gc:orc file.nim
. Parse compiler output for specifics; common flags include
--warnings:on
to catch undeclared vars. Log errors with
echo
or a logging library like
chronicles
.
Nim中使用try-except块处理运行时错误:
nim
try:  
  raise newException(ValueError, "Invalid input")  
except ValueError:  
  echo "Handled error: ", getCurrentExceptionMsg()
编译时错误可使用
nim c --verbosity:2 file.nim
启用详细输出排查。切换到ORC检查内存问题:
nim c --gc:orc file.nim
。解析编译器输出获取具体错误信息;常用参数包括
--warnings:on
以捕获未声明变量等问题。使用
echo
chronicles
等日志库记录错误。

Concrete Usage Examples

具体使用示例

  1. 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:
forEach(@[1, 2, 3], echo item)
. This generates efficient loops at compile-time.
  1. 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
nim c --passC:-I/usr/include file.nim
to link C headers, enabling seamless integration.
  1. 定义并使用Macro实现代码生成:要创建循环Macro,编写如下代码:
nim
macro forEach(items: seq, body: stmt): stmt =  
  result = quote do: for item in `items`: `body`
调用方式:
forEach(@[1, 2, 3], echo item)
。该代码会在编译期生成高效的循环逻辑。
  1. 使用FFI调用C函数:导入并调用C的printf函数:
nim
proc cPrintf(format: cstring) {.importc: "printf", header: "<stdio.h>".}  
cPrintf("%s\n", "Hello from Nim")
使用
nim c --passC:-I/usr/include file.nim
编译以链接C头文件,实现无缝集成。

Graph 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)