Skip to content

Add HTTP fetch support with libcurl integration#1

Open
TomasPalsson wants to merge 1 commit into
masterfrom
claude/add-http-methods-mGrLD
Open

Add HTTP fetch support with libcurl integration#1
TomasPalsson wants to merge 1 commit into
masterfrom
claude/add-http-methods-mGrLD

Conversation

@TomasPalsson

Copy link
Copy Markdown
Owner

Summary

This PR adds HTTP fetch capabilities to the Bonk language, enabling HTTP requests (GET, POST, DELETE, etc.) with optional request bodies. The feature is implemented as both an expression and statement, with responses returned as strings.

Key Changes

  • New fetch keyword and syntax: Supports 1-3 argument forms:

    • fetch URL — implicit GET request
    • fetch METHOD URL — custom HTTP method, no body
    • fetch METHOD URL BODY — custom method with request body
  • C runtime library (runtime/http.c):

    • Implements bonk_http_fetch() function using libcurl
    • Handles response buffering and memory management
    • Linked into final binary alongside libc
  • Parser enhancements (src/parser.rs):

    • Added Token::Fetch lexing
    • Implemented parse_fetch_statement() and parse_fetch_args() for flexible argument parsing
    • Supports fetch as both expression and statement
  • AST extensions (src/ast.rs):

    • Added Statement::Fetch variant
    • Added Expression::Fetch variant with method, url, and optional body
  • Compiler codegen (src/compiler.rs):

    • Implemented compile_fetch() to emit x86-64 calling convention setup (rdi/rsi/rdx)
    • Added string_vars tracking to distinguish string-valued variables for proper printf formatting
    • Updated compile_print() to use correct format string for fetch results and string variables
    • Declared external _bonk_http_fetch symbol
  • Build system (Makefile):

    • Added runtime compilation step for http.c
    • Links against libcurl (-lcurl)
    • Combines object files before final linking
  • Documentation (CLAUDE.md):

    • Added HTTP fetch syntax examples
    • Updated platform requirements to include libcurl
  • Example (examples/http.bonk):

    • Demonstrates GET and POST requests with httpbin.org

Implementation Details

  • Fetch arguments are evaluated left-to-right and passed via x86-64 calling convention (rdi=method, rsi=url, rdx=body)
  • Response strings are dynamically allocated by libcurl wrapper and returned in rax
  • String variable tracking ensures proper printf formatting (uses fmt_str format for string values)
  • Supports fire-and-forget usage (fetch as statement without assignment)

https://claude.ai/code/session_01M76SM5XsfmERRhkueq1d4W

Add `fetch` keyword for making HTTP requests directly from Bonk code.
Supports GET/POST/PUT/DELETE via a libcurl-based C runtime.

Syntax:
  fetch "url"                      -- GET request
  fetch "POST" "url" "body"        -- method + URL + body
  result = fetch "url";            -- capture response as string

https://claude.ai/code/session_01M76SM5XsfmERRhkueq1d4W
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.

2 participants