Skip to content

feat: implement pipes, variable expansion and declare/export/unset builtins - #8

Merged
podlLev merged 14 commits into
mainfrom
feature/pipes-and-variables
Jul 7, 2026
Merged

podlLev merged 14 commits into
mainfrom
feature/pipes-and-variables

Conversation

@podlLev

@podlLev podlLev commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

Implements pipeline execution with | operator, variable expansion, and variable management builtins (declare, export, unset).

Changes

Pipeline Execution

  • Add PipelineResult record to carry pipeline stages and background flag
  • Add ProcessBuilderFactory to centralize process building and redirect configuration, eliminating duplication between ExternalCommand and Pipeline
  • Add Pipeline executor supporting single and multi-stage pipelines
  • Support builtin commands in pipeline via System.out/in capture
  • Support external commands in pipeline via ProcessBuilder PIPE redirects
  • Feed previous stage output into next via daemon feeder thread
  • Update Tokenizer to split input by | respecting quotes
  • Return PipelineResult from tokenizer.tokenize() instead of ParseResult

Variable Expansion

  • Add variable storage to Environment with setVariable, getVariable, unsetVariable methods
  • Add lastExitCode and lastBackgroundPid tracking to Environment
  • Add expandVariables() in Tokenizer supporting:
    • $VAR and ${VAR} syntax
    • $? last exit code
    • $$ current shell PID
    • $! last background job PID
    • No expansion inside single quotes
    • Unexpanded variables resolve to empty string
  • Handle VAR=value assignment syntax in Shell.handleInput
  • Update ExternalCommand to track exit code and background PID

New Builtins

  • declare — set and list variables
  • export — export variables to environment
  • unset — remove variables

Testing

Manually verified:

  • echo hello | cat pipes output correctly
  • dir | findstr ".java" filters output on Windows
  • echo hello | cat | cat chains multiple pipes
  • MY_VAR=hello && echo $MY_VAR expands variables
  • echo $PATH expands system environment variables
  • echo $? shows last exit code
  • echo $$ shows shell PID
  • declare MY_VAR=hello sets variable
  • export MY_VAR=hello exports variable
  • unset MY_VAR removes variable
  • Variable expansion works inside pipelines and redirects

podlLev added 14 commits July 7, 2026 09:00
…g logic

- Add ProcessBuilderFactory with build(), configureRedirect(), configureOutput()
- Update ExternalCommand to use ProcessBuilderFactory
- Remove duplicate buildProcessArgs and configureRedirect from ExternalCommand
- Rename tokenize() logic to tokenizeStage() for per-stage parsing
- Add splitByPipe() to split input by | respecting single and double quotes
- Move background & detection from extractRedirect to tokenize()
- Remove background flag from ParseResult, now carried by PipelineResult
- Return PipelineResult from tokenize() containing all stages and background flag
- Execute single stage directly via CommandRegistry
- Chain external commands via ProcessBuilder PIPE redirects
- Support builtin commands in pipeline via System.out/in capture
- Feed previous stage output into next via daemon feeder thread
- Support background pipeline execution
- Add variables map to Environment for local variable storage
- Add setVariable(), getVariable(), unsetVariable() methods
- getVariable() checks local variables first, then system env
- Return unmodifiable view of variables map via getVariables()
- Add lastExitCode field to track exit code of last command
- Add lastBackgroundPid field to track PID of last background job
- Expose via getters and setters for use in variable expansion
- Add expandVariables() called before tokenizing each stage
- Support $VAR and ${VAR} syntax expanding from Environment
- Support $? expanding to last command exit code
- Support $$ expanding to current shell PID
- Support $! expanding to last background job PID
- Skip expansion inside single quotes
- Unexpanded variables resolve to empty string
- Pass Environment into Tokenizer constructor
- Add DeclareBuiltin to set variables via declare VAR=value
- List all variables with declare output format
- Print single variable with declare VAR
- Register as environment-dependent builtin in BuiltinFactory
- Add ExportBuiltin to set variables via export VAR=value
- List all exported variables with export output format
- Register as environment-dependent builtin in BuiltinFactory
- Add UnsetBuiltin to remove variables from Environment
- Support multiple arguments to unset several variables at once
- Register as environment-dependent builtin in BuiltinFactory
- Detect VAR=value pattern at start of handleInput
- Set variable in Environment without executing as command
- Support standard shell assignment syntax
- Capture process exit code and store in Environment.lastExitCode
- Store background process PID in Environment.lastBackgroundPid
- Makes $? and $! expansion work correctly after command execution
@podlLev podlLev self-assigned this Jul 7, 2026
@podlLev podlLev added the enhancement New feature or request label Jul 7, 2026
@podlLev
podlLev merged commit 6984b14 into main Jul 7, 2026
@podlLev
podlLev deleted the feature/pipes-and-variables branch July 7, 2026 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant