text
stringlengths
0
721
**key=value parsing:** When `FileRead` assigns to a `DIM`'d array, lines `key=value` become `arr$("key")`. Lines starting with `#` are comments. Perfect for `.env` files.
```basic
DIM env$
LET env$ = FileRead(".env")
LET API_KEY# = env$("OPENAI_API_KEY")
```
**Paths:** Use `/` or `\\` — never single `\` (it's an escape char). Relative paths are from `PRG_ROOT#`.
**FileWrite with array** saves in key=value format (round-trips with FileRead).
---
## Network
```basic
LET res$ = HTTPGET("https://api.example.com/data")
LET res$ = HTTPPOST("https://api.example.com/submit", "{""key"":""val""}")
' With headers (optional last parameter)
DIM headers$
headers$("Authorization") = "Bearer mytoken"
headers$("Content-Type") = "application/json"
LET res$ = HTTPGET("https://api.example.com/data", headers$)
LET res$ = HTTPPOST("https://api.example.com/data", body$, headers$)
```
### Local HTTP server (LISTEN)
BazzBasic can also receive requests as a local-only HTTP server, typically used as glue between a static HTML page and a BazzBasic script on the same machine.
```basic
STARTLISTEN 8080 ' bind 127.0.0.1:8080, no admin needed
STARTLISTEN 8080, 5000 ' optional timeout in ms
LET body$ = GETREQUEST() ' POST/PUT/PATCH body, or GET query string ("" on timeout)
SENDRESPONSE "{""status"":""ok""}" ' must be called or browser hangs
STOPLISTEN
```
Important rules:
- **Always pair `GETREQUEST()` with `SENDRESPONSE`.** The browser is waiting on the response. Forgetting `SENDRESPONSE` makes the page hang.
- **Bound to `127.0.0.1` only.** This is local glue, not a real web server. Do not generate code that tries to expose this on the LAN or internet.
- **Body or query string, not full request.** `GETREQUEST()` returns just the POST body (or the GET query string without `?`). Headers, method, and URL path are NOT exposed in this version. If you need structured data, parse the body with `ASARRAY(body$)`.
- **OPTIONS preflight is automatic.** Do NOT write code that handles `OPTIONS` — it is consumed silently and never reaches user code.
- **CORS is automatic.** Every response carries `Access-Control-Allow-Origin: *`. No need to set it manually.
- **One listener at a time.** Calling `STARTLISTEN` while one is already active is an error. Call `STOPLISTEN` first.
- **Always 200 OK, always `text/plain`.** Custom status codes and content types are not supported in this version. To return JSON, just put JSON text in the body — browser-side `response.json()` still works.
Typical receive-and-process pattern:
```basic
STARTLISTEN 8080
PRINT "Waiting for browser..."
LET json$ = GETREQUEST()
DIM data$
LET data$ = ASARRAY(json$)
PRINT "Got name: " + data$("name")
SENDRESPONSE "{""status"":""ok""}"
STOPLISTEN
END
```
---
## Arrays & JSON
Nested JSON maps to comma-separated keys: `data$("player,name")`, `data$("skills,0")`
```basic
' Array → JSON string
LET json$ = ASJSON(arr$)
' JSON string → array (returns element count)
DIM data$
LET count$ = ASARRAY(data$, json$)
' Load/save JSON files
LOADJSON arr$, "file.json"
SAVEJSON arr$, "file.json"
```
---
## Fast Trigonometry
~20× faster than `SIN(RAD(x))`, 1-degree precision. Uses ~5.6 KB memory.
```basic
FastTrig(TRUE) ' Enable lookup tables (must call first)
LET x$ = FastCos(45) ' Degrees, auto-normalized 0–359
LET y$ = FastSin(90)
LET r$ = FastRad(180) ' Deg→rad (no FastTrig needed)
FastTrig(FALSE) ' Free memory
```