Issue #23 now covers function naming conventions, file organisation, global namespace cleanup options, and a concrete implementation plan.
38 lines
1.1 KiB
Markdown
38 lines
1.1 KiB
Markdown
# 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
|
|
|
|
```lua
|
|
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
|
|
|
|
```lua
|
|
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`
|