Skip to content

Implement C interop foundation: AST, parser, and type system#4

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/implement-cinterop-module-system
Draft

Implement C interop foundation: AST, parser, and type system#4
Copilot wants to merge 6 commits intomainfrom
copilot/implement-cinterop-module-system

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 16, 2025

Implements the C interoperability foundation per docs/CINTEROP.md, enabling Shortcake to call C functions, declare C structs, and load C libraries.

Changes

AST Layer

  • Added 6 node types: c_load, c_include, c_import, c_from_import, c_struct_decl, c_callback_reg
  • Added corresponding data structures in extra union with helper methods
  • Memory management in deinit() for new node types

Parser Layer (src/parser.zig)

  • parseCStructDecl(): Parse $struct $Name do ... end with field declarations
  • parseCType(): Parse C types ($int, $char*, $int[10])
  • parseCFunctionCall(): Parse C function invocations
  • parseCRegisterDecl(): Parse callback registration
  • Dollar token handling in parseDeclaration() and parsePrimaryExpression()

Type System (src/c_types.zig)

  • CPrimitiveType: 19 C primitive types with Shortcake/MLIR mappings
  • CType: Union type supporting primitive, pointer, array, function pointer, struct, custom
  • CTypeMapping: Parse C type strings, convert to Shortcake/MLIR types, compatibility checking

Codegen (src/codegen.zig)

  • Integrated C interop into generateExpr() switch
  • Placeholder MLIR generation for $load, $struct, C function calls
  • mapCTypeToMLIR(): Type conversion helper

Tests & Examples

  • 10 parser tests covering all C interop syntax
  • 6 type system unit tests
  • Example programs: c_math_example.sho, c_structs_example.sho

Example Usage

$load "libm.so"
$from <math.h> $import sin, cos, sqrt

$struct $Point do
  x: $int
  y: $int
end

fn main() do
  local result = $sqrt(16.0)
  $printf("Result: %f\n", result)
end

Scope

Parser and type system are complete. MLIR codegen and runtime (dlopen/dlsym, symbol registry, callbacks) deferred to future PRs.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • ziglang.org
    • Triggering command: curl -L REDACTED -o /tmp/zig.tar.xz (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Implement CInterop</issue_title>
<issue_description>Implement the module system in Shortcake. Following the plan from docs/CINTEROP.md</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 16, 2025
@igorgue igorgue changed the title [WIP] Implement CInterop module system in Shortcake [WIP] Implement CInterop system in Shortcake Nov 16, 2025
Copilot AI and others added 5 commits November 16, 2025 18:36
- Add c_include, c_import, c_from_import, c_struct_decl, c_callback_reg node types
- Add corresponding data structures to extra union
- Implement helper methods for all new node types
- Update deinit to properly free new node types
- Update toString method for new node types

Co-authored-by: igorgue <7014+igorgue@users.noreply.github.com>
- Implement parseCStructDecl for parsing $struct declarations
- Implement parseCType for parsing C types ($int, $char*, $int[10])
- Implement parseCFunctionCall for parsing C function calls
- Hook up $ token handling in parseDeclaration and parsePrimaryExpression
- Support opaque struct declarations and struct field parsing

Co-authored-by: igorgue <7014+igorgue@users.noreply.github.com>
- Implement parseCRegisterDecl for callback registration
- Add handling for $register in dollar token processing
- Create comprehensive test suite for C interop parser
- Tests cover $load, $include, $struct, C function calls, C types, and $register

Co-authored-by: igorgue <7014+igorgue@users.noreply.github.com>
- Create c_types.zig module for C to Shortcake type mapping
- Implement CPrimitiveType enum with toShortcakeType and toMLIRType methods
- Implement CType union for representing all C types (primitive, pointer, array, etc.)
- Add CTypeMapping for parsing and converting C type strings
- Add basic codegen support for C load, struct declarations, and function calls
- Add mapCTypeToMLIR helper in codegen for type conversion

Co-authored-by: igorgue <7014+igorgue@users.noreply.github.com>
- Add c_math_example.sho demonstrating C math library usage
- Add c_structs_example.sho showing struct declarations and usage
- Create CINTEROP_STATUS.md documenting implementation progress
- Document all completed phases and remaining work
- Include syntax examples and architecture overview

Co-authored-by: igorgue <7014+igorgue@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement CInterop system in Shortcake Implement C interop foundation: AST, parser, and type system Nov 16, 2025
Copilot AI requested a review from igorgue November 16, 2025 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement CInterop

2 participants