Skip to content

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Jan 1, 2026

This PR introduces the input() statement to support interactive console applications and performs a significant refactor of the expression code generation logic. By separating L-value (address) generation from R-value (value) generation, the compiler now supports more complex assignment targets and memory-modifying operations.

Key Changes

1. Interactive input() Statement

  • AST/Parser: Added StatementNode::Input and implemented parse_input to support input("format", targets...) syntax.
  • Format Validation: The parser now ensures the number of placeholders in the format string matches the number of provided arguments.
  • LLVM Implementation:
    • Added wave_format_to_scanf to bridge Wave format strings with C-style scanf.
    • Generated IR to call scanf, passing the memory addresses of target variables.
    • Error Handling: Added runtime checks to ensure scanf successfully reads the expected number of arguments; otherwise, the program exits with code 1.

2. L-Value Generation & Codegen Refactor

  • Architectural Reorganization: Split the monolithic expression.rs into a module containing mod.rs, rvalue.rs, and lvalue.rs.
  • L-Value System (generate_lvalue_ir): Implemented dedicated logic to retrieve the memory address of an expression. This supports:
    • Variables and pointer dereferences.
    • Nested Accessors: Complex paths like arr[i], obj.field, and even combined forms like (deref p).field[idx].
    • Ensures type correctness when navigating through pointers and struct offsets.

3. Interactive Runtime Support

  • Process Inheritance: Updated the runner to use Stdio::inherit() for the child process. This allows the compiled Wave program to interact directly with the user's terminal for standard input/output.

4. Testing

  • Integration Test: Updated test74.wave to demonstrate and verify the new input() functionality.

Example Usage

var age: i32;
var name: str;

// Reading basic types
input("Age: {}", &age);

// Reading into complex types (if supported by lvalue)
var scores: array<i32, 3> = [0, 0, 0];
input("Score: {}", &scores[0]);

Add support for reading user input using `scanf` and reorganize
expression handling to support L-value generation.

Changes:
- AST/Parser:
  - Add `StatementNode::Input` variant.
  - Implement `parse_input` to handle `input("fmt", var)` syntax.
  - Enforce `input()` format string validation (placeholder count matches args).
- Expression Codegen Refactor:
  - Split `expression.rs` into `mod.rs`, `rvalue.rs`, and `lvalue.rs`.
  - Move existing R-value generation to `rvalue.rs`.
  - Implement `generate_lvalue_ir` in `lvalue.rs` to support address generation for:
    - Variables, Dereferences, AddressOf (`&`).
    - Array Indexing (`arr[i]`), Struct Field Access (`obj.field`).
    - Handles nested expressions and ensures type correctness.
- LLVM Codegen:
  - Implement `wave_format_to_scanf` to convert Wave format strings to C-style `scanf` formats.
  - Generate IR for `input()`:
    - Calls `scanf` with generated format string and variable addresses.
    - Validates `scanf` return value (matches expected argument count).
    - Exits program with code 1 on input failure.
- Runner:
  - Update `run_wave_file` to use `Stdio::inherit()` for stdin/stdout/stderr.
  - Allows interactive input during execution.
- Tests:
  - Update `test74.wave` to demonstrate `input()` usage.

This enables interactive console applications in Wave by providing
standard input capabilities.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev self-assigned this Jan 1, 2026
@LunaStev LunaStev merged commit cce40f4 into wavefnd:master Jan 1, 2026
2 checks passed
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.

1 participant