Files
lush/issues/05-env-var-access.md
Cormac Shannon 34bfabccbd Expand issue #23 with detailed cleanup analysis; close issues #5 and #24
Issue #23 now covers function naming conventions, file organisation,
global namespace cleanup options, and a concrete implementation plan.
2026-03-12 19:26:05 +00:00

1.1 KiB

05 — Implement environment variable access (read/write)

Status: done Blocked by: #01

Add syntax for reading and writing environment variables directly from Lua.

Option 1 — export keyword

local var = "my data"
export var                    -- exports existing local to env
export other_var = "my data"  -- creates + exports
  • New reserved word export in lexer
  • export var looks up the local's value by name and calls setenv()
  • export x = val combines declaration with setenv()

Option 2 — $ sigil

local dir = $PWD              -- getenv("PWD")
$PWD = "/"                    -- setenv("PWD", "/")
$MY_ENV_VAR = "data"          -- setenv("MY_ENV_VAR", "data")
  • $NAME as expression → getenv("NAME"), returns string or nil
  • $NAME = expr as statement → setenv("NAME", tostring(expr))
  • New token type in lexer for $ + identifier

Implementation touches

  • llex.c / llex.h — new token(s) or reserved word
  • lparser.c — new expression/statement rules
  • Runtime calls getenv() / setenv() — standard C, no shell needed
  • Non-existent env var → return nil