NYC
skills/letta-ai/skills/polyglot-c-py

polyglot-c-py

SKILL.md

Polyglot C-Python File Creation

This skill provides guidance for creating single source files that function correctly as both Python and C programs.

When to Use

This skill applies when:

  • Creating a file that must be valid syntax for both Python and C
  • The file needs to execute correctly under both python filename and gcc filename && ./output
  • Building polyglot programs that produce equivalent output in both languages

Polyglot Structure Approach

Core Technique

Use preprocessor directives and string literals to hide language-specific code:

  1. Hide C code from Python: Wrap C-specific code in triple-quoted strings that Python ignores
  2. Hide Python code from C: Use #if 0...#endif preprocessor blocks that C ignores but Python sees as comments

Typical File Structure

[Shared/polyglot preamble]
#if 0
[Python code block - C preprocessor skips this]
#endif
"""
[C code block - Python treats as string literal]
"""

Key Syntax Considerations

  • C-style // comments cause Python syntax errors - avoid in shared sections
  • Use #if 0 instead of /* */ comments for hiding Python code from C
  • Triple-quoted strings (""") effectively hide C code from Python
  • The file extension (e.g., .py.c) should indicate dual-language nature

Verification Strategy

Functional Testing

  1. Test Python execution: python <filename>
  2. Test C compilation and execution: gcc <filename> -o <output> && ./<output>
  3. Verify both produce identical or equivalent output
  4. Test multiple input cases including edge cases (e.g., boundary values, zero, negative inputs if applicable)

Environmental Verification (Critical)

Before declaring the task complete, verify the final directory state:

  1. List all files in the target directory: Ensure only the required source file(s) exist
  2. Check for leftover artifacts: Compiled binaries, object files (.o), temporary files
  3. Compare against requirements: Match the exact expected file structure

Common Pitfalls

Artifact Cleanup (Most Critical)

Problem: Leaving compiled binaries or test artifacts in the working directory.

Prevention:

  • Compile test binaries to /tmp or a separate test directory: gcc file.py.c -o /tmp/test_binary
  • If compiling in the working directory, delete binaries after testing: rm <binary_name>
  • Never leave .o files, executables, or other build artifacts in the final directory

Incomplete Task Analysis

Problem: Focusing only on functional correctness while ignoring implicit requirements like directory cleanliness.

Prevention:

  • Before starting, explicitly identify all success criteria:
    • What files should exist when done?
    • What files should NOT exist?
    • What state should the environment be in?
  • After completing functional work, verify the end state matches all requirements

Syntax Conflicts

Problem: Using syntax valid in one language but not the other in shared sections.

Prevention:

  • Avoid C-style // comments in any section Python will parse
  • Test both interpreters after each significant change
  • Keep shared sections minimal

Untested Edge Cases

Problem: Only testing happy-path inputs.

Prevention:

  • Test boundary values (0, 1, max values)
  • Test error conditions if the program should handle them
  • Compare outputs between Python and C for all test cases

Verification Checklist

Before declaring the task complete:

  1. Python execution produces correct output
  2. C compilation succeeds without warnings
  3. C execution produces correct output
  4. Multiple test inputs verified
  5. Target directory contains ONLY the required file(s)
  6. No compiled binaries left behind
  7. No temporary or intermediate files present
  8. File naming matches requirements exactly
Weekly Installs
18
Repository
letta-ai/skills
First Seen
Jan 24, 2026
Installed on
claude-code13
gemini-cli12
codex12
opencode12
antigravity11
windsurf9