ReviewOS

home-lang/home

Parser: Zig-style space-separated return type 'fn(args) Ret { ... }' fails when params present (10+ kernel files)

#64
Closed glennmichael123 opened this 24 days ago · 0 comments
24 days ago

Problem

The parser accepts the Zig-style fn name() Ret { ... } return-type form when there are NO parameters, but fails with error[EP0001]: Expected '{' when parameters are present:

fn foo() ?u32 { return null }                          // OK — no params
fn foo(name: []const u8) ?[]const u8 { return null }   // FAIL — same shape, with param
fn foo(name: []const u8): ?[]const u8 { return null }  // OK — adding `:` works

The parser treats the ?[]const u8 as a continuation of the parameter list rather than as a return type.

Background

Home accepts both:

  • TS/Rust-style: fn foo(): Ret { ... } (: separator)
  • Zig-style: fn foo() Ret { ... } (space separator)

The Zig-style works for empty parameter lists but breaks for non-empty ones — clearly a parser inconsistency.

Affected kernel files (10+)

Many files in kernel/src/iot/, kernel/src/security/, kernel/src/drivers/ use the Zig-style with params and currently fail. Examples:

  • kernel/src/iot/voice_routines.home:337 (pub fn get_param(self: *const Action, name: []const u8) ?[]const u8 {)
  • kernel/src/iot/voice_languages.home:436 (similar)
  • kernel/src/iot/audio_processing.home (similar)
  • kernel/src/security/dm_verity.home, secure_boot.home, fscrypt.home, etc.

Expected behavior

The parser should accept the Zig-style space-separated return type irrespective of parameter presence. After the closing ) of the parameter list:

  • If next token is :, consume : and parse the return type (existing TS/Rust path).
  • If next token starts a type expression (?, *, &, [, Identifier, etc.) AND parameters were non-empty, parse it as a return type (new — fix the gap).
  • If next token is {, the function returns void (existing).

Implementation hint

Find the function-decl parameter-list closing parser. After ) is consumed, the return-type detection probably has two paths:

  1. : → parse return type
  2. else → assume void

The fix: extend path 2 to peek for a type-starter and dispatch into the return-type parser when present. The same logic already works for empty parameter lists; just remove or fix the gating that excludes non-empty param lists.

Tests

Positive (must parse):

  • fn foo(x: u32) ?u32 { return null }
  • fn foo(x: u32, y: u32) []const u8 { ... }
  • fn foo(x: *const T) Result { ... }
  • All fn(args) RetType {} forms with arbitrary RetType

Regression:

  • fn foo() Ret { ... } (empty params) keeps working
  • fn foo(): Ret { ... } (TS/Rust-style with colon) keeps working
  • fn foo() { ... } (no return type) keeps working

Acceptance

  1. Many kernel files clear home-debug ast past their fn-signature line.
  2. Existing test suite green.

Sign in to comment on this issue.