Skip to content

Repository files navigation

picoposh

A deliberately stripped-down PowerShell script interpreter, written in strict C89 for low-powered and vintage systems. It is not intended to be used as a feature-complete PowerShell runtime.

Quick start

cmake -S . -B build/cmake
cmake --build build/cmake
ctest --test-dir build/cmake --output-on-failure

Run a script or an inline command:

./build/cmake/picoposh script.ps1
./build/cmake/picoposh -c 'Write-Output "hello"'
./build/cmake/picoposh --version

A taste of the supported subset:

$src = "data.txt"
if (Test-Path $src) {
    Get-ChildItem "." | Write-Output
    Copy-Item $src "backup.txt"
    Write-Output "done"
}

Use as a library

picoposh is also a small static library. Include one header and run scripts from your own application, capturing or redirecting their output:

#include "picoposh.h"

char *out = NULL;
int code = pico_run_capture("Write-Output (2 + 3)\n", "job", &out, NULL);
puts(out);              /* -> 5 */
pico_string_free(out);

Link with CMake (find_package(picoposh)picoposh::picoposh) or compile the sources directly. See docs/embedding.md and examples/embed.

Currently implemented

  • Language: string/integer literals, $var assignment (from an expression or a pipeline), "$var" and "$( … )" string expansion, bare expression statements, Verb-Noun commands with positional/named/switch parameters, | pipelines, if/elseif/else, foreach ($x in …), while, # comments.
  • Control flow: if/elseif/else, foreach, while, for, break, continue.
  • User-defined functions with positional and named params, param(...) blocks, typed ([int]$n) and default-valued ($n = 5) parameters, pipeline input (begin/process/end blocks with $_, or $input), $args, return, recursion, and local scope.
  • switch statements and try/catch/finally + throw error handling.
  • Expressions: integer (incl. hex 0xFF) and floating-point arithmetic + - * / %, compound assignment += -= *= /= and ++/--, comparison -eq -ne -lt -le -gt -ge -like -match (regex), membership/type -contains -in -is -as, bitwise -band -bor -bxor -bnot -shl -shr, logical -and -or -not/!, $( … ) subexpressions, member access $obj.Prop, .Count/.Length and string methods (.ToLower(), .Substring(), .Replace(), …), array literals 1,2,3 / @( … ) with $a[0] indexing, hashtable literals @{ k = v }, and { … } script blocks ($_ inside), ranges 1..10, and -join/-split.
  • Cmdlets: Get-ChildItem, Copy-Item, Move-Item, Remove-Item, New-Item, Test-Path, Get-Item, Rename-Item, Get-Location, Set-Location, Push-Location, Pop-Location, Join-Path, Split-Path, Resolve-Path, Get-Content, Set-Content, Add-Content, Clear-Content, Out-File, Write-Output, Write-Error, Write-Host, Write-Warning, Out-Null, Out-String, Where-Object, ForEach-Object, Sort-Object, Measure-Object, Select-Object, Format-Table, Format-List, Group-Object, Compare-Object, Get-Unique, Tee-Object, New-Object, Get-Member, Get-Variable, Set-Variable, New-Variable, Select-String, Get-Date, ConvertTo-Json, ConvertFrom-Json, ConvertTo-Csv, ConvertFrom-Csv, Import-Csv, Export-Csv, Invoke-WebRequest, Get-Command, Get-Help, Import-Module.
  • Script modules via Import-Module.
  • Everything outside the subset parses to a clear "not supported yet" error rather than misbehaving.

About

A stripped-down PowerShell script interpreter for low-powered and vintage systems

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages