Skip to content

parse_nat should return Result<Nat, String> per spec #174

@aallan

Description

@aallan

The parse_nat built-in (added in PR #173) currently returns bare Nat. Per spec section 9, it should return Result<Nat, String> to handle invalid input gracefully.

Current behaviour

parse_nat("42")    -- returns Nat (42)
parse_nat("abc")   -- returns garbage (no validation)

Expected behaviour

parse_nat("42")    -- returns Ok(42)
parse_nat("abc")   -- returns Err("invalid digit")

Why this is non-trivial

No built-in function currently returns an ADT value. The codegen only knows ADT layouts from DataDecl AST nodes (codegen/registration.py:_register_data). Making a built-in construct heap-allocated tagged unions requires:

  1. New codegen infrastructure for built-in functions that return ADT values (allocate Result, store tag, box payload)
  2. Digit validation in the WASM parse loop (currently byte - 48 on all non-space bytes, no check for 0-9 range)
  3. Error string construction for the Err path
  4. Test rewrites -- all 4 codegen tests need data Result<T, E> { Ok(T), Err(E) } declarations and pattern matching
  5. Example update -- examples/string_ops.vera uses bare parse_nat return

Context

Deferred from PR #173 review to avoid blocking the first external contribution. The two simpler spec discrepancies (string_length return type, string_slice param types) were fixed in that PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions