EkBass commited on
Commit
5ac9a6d
·
verified ·
1 Parent(s): 949ab82

Update SKILL.md

Browse files
Files changed (1) hide show
  1. SKILL.md +131 -33
SKILL.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- name: bazzbasic-code
3
- description: "BazzBasic BASIC interpreter language reference. Use when writing, debugging, or explaining BazzBasic code (.bas files). Triggers on BazzBasic syntax, $ and # variable suffixes, SDL2 graphics in BASIC, SCREEN/DRAWSHAPE/LOADIMAGE commands, DEF FN functions, BazzBasic file I/O, sound commands, or any question about BazzBasic features. Always use this skill before writing any BazzBasic code. "
4
  ---
5
 
6
  # BazzBasic Language Reference
7
- **Version:** 1.6 | **Author:** Kristian Virtanen (EkBass) | **Platform:** Windows x64 only (SDL2 via P/Invoke — no Linux, macOS, browser or Android)
8
  **Homepage:** https://ekbass.github.io/BazzBasic/
9
  **GitHub:** https://github.com/EkBass/BazzBasic
10
- **Manual:** https://ekbass.github.io/BazzBasic/manual
11
  **Examples:** https://github.com/EkBass/BazzBasic/tree/main/Examples
12
  **Rosetta Code:** https://rosettacode.org/wiki/Category:BazzBasic
13
  **Rosetta Code solutions:** https://github.com/EkBass/BazzBasic/tree/main/Examples/rosetta-code
@@ -24,19 +24,22 @@ BazzBasic is a BASIC meant for beginners and for small, fun experiments (simple
24
  |------|--------|
25
  | Variables end with `$` | `name$`, `score$`, `x$` — the suffix does NOT mean string type |
26
  | Constants end with `#` | `MAX#`, `PI#`, `TITLE#` — `#` only means immutable |
27
- | Arrays declared with `DIM`, end with `$` | `DIM items$` |
 
28
  | First use of variable requires `LET` | `LET x$ = 0` — after that `x$ = x$ + 1` |
29
  | FOR and INPUT auto-declare, no LET needed | `FOR i$ = 1 TO 10` |
30
- | Case-insensitive | Keywords, variables and labels. Only string contents are case-sensitive |
 
31
  | Functions defined **before** they are called | Put at top or INCLUDE — forward references don't resolve |
32
  | Function name ends with `$`, called with `FN` | `FN MyFunc$(a$, b$)` |
 
33
  | Function return value **must** be used | `PRINT FN f$()` or `LET v$ = FN f$()` |
34
  | `DEF FN` scope is fully isolated | Only global `#` constants are visible. No `$` variables, no global arrays. Arrays cannot be passed as parameters — see *Passing Arrays to Functions* |
35
  | `+` does both add and concatenate | If either side is a string, the other is auto-converted: `"Score: " + 5` → `"Score: 5"`, no `STR()` needed |
36
  | Division always returns float | `10 / 3` → `3.333...` |
37
- | Division by zero returns `0` silently | Guard with `IF b$ = 0 THEN ...` if correctness matters |
38
- | No integer-division operator | Use `INT(a / b)`, `FLOOR(a / b)`, or `CINT(a / b)` |
39
- | Errors halt the program | No `TRY`/`CATCH` or `ON ERROR` — line-numbered message printed |
40
  | No line-continuation char | Leave an operator at line end or open a `(` — see *Line Continuation* |
41
  | No line numbers | Never supported. Use `[labels]` |
42
  | `:` separates statements on one line | `FOR i$ = 1 TO 3 : PRINT i$ : NEXT`. A `:` inside a label like `[sub:init]` is part of the label name, not a separator |
@@ -64,6 +67,8 @@ ISSET(arr$(0)) ' expecting error ' returns 0 silently — ISSET only se
64
  LET x$ = a$ + _ ' VBA continuation LET x$ = a$ + ' just leave the operator
65
  LET x$ = a$ + \ ' Python continuation LET x$ = a$ + ' or open a paren
66
  IF NOT a$ = b$ THEN ' QBASIC habit IF NOT (a$ = b$) THEN
 
 
67
  ```
68
 
69
  ---
@@ -72,7 +77,7 @@ IF NOT a$ = b$ THEN ' QBASIC habit IF NOT (a$ = b$) THEN
72
 
73
  - Prefer `FSTRING` over long `+` concatenation chains for multi-variable strings
74
  - Place `DEF FN` definitions at the top of the file or via `INCLUDE`
75
- - Store every `LOADIMAGE` / `LOADSOUND` / `LOADSHAPE` handle as a `#` constant. They return stable integer handles owned by SDL2 that never change. For many handles, an array is fine: `sprites$("player") = LOADIMAGE(...)`
76
  - Declare all variables and constants in `[inits]`, not inside loops — this avoids repeated existence checks
77
  - Precompute colors: `LET WHITE# = RGB(255, 255, 255)` in `[inits]`, never `RGB()` inside a game loop
78
  - In graphics mode: clear with `LINE (0,0)-(W,H), 0, BF` (much faster than `CLS`), draw text with `DRAWSTRING` (`PRINT` flickers), and wrap each frame in `SCREENLOCK ON` / `SCREENLOCK OFF`
@@ -146,7 +151,7 @@ LET e$, f$ = "foo", g$ = 10 ' e$ stays empty, f$ and g$ get values
146
  LET name$ = "Alice" ' String
147
  LET score$ = 0 ' Number
148
 
149
- LET PI# = 3.14159 ' Constant (immutable)
150
  LET TITLE# = "My Game" ' String constant
151
  ```
152
 
@@ -167,11 +172,12 @@ s$ += " World" ' concatenation
167
  **Comparison:** `"123" = 123` is TRUE (cross-type), but keep types consistent for speed.
168
 
169
  ### Built-in Constants
170
- - **Boolean:** `TRUE` (`1`), `FALSE` (`0`) — plain numbers. `IF x$ = 1` and `IF x$ = TRUE` are identical.
171
  - **Math:** `PI#`, `HPI#` (π/2 = 90°), `QPI#` (π/4 = 45°), `TAU#` (2π = 360°), `EULER#` (e)
 
172
  - **System:** `PRG_ROOT#` (program base directory path, ends with a separator)
173
- - **Version:** `BBVER#` (current BazzBasic version as a string, e.g. `"1.6"`)
174
- - **Keyboard:** `KEY_ESC#`, `KEY_ENTER#`, `KEY_SPACE#`, `KEY_UP#`, `KEY_DOWN#`, `KEY_LEFT#`, `KEY_RIGHT#`, `KEY_F1#`…`KEY_F12#`, `KEY_A#`…`KEY_Z#`, `KEY_0#`…`KEY_9#`, `KEY_NUMPAD0#`…`KEY_NUMPAD9#`, `KEY_NUMPAD_PLUS#`, `_MINUS#`, `_MUL#`, `_DIV#`, `_DOT#`, `KEY_LSHIFT#`, `KEY_LCTRL#`, `KEY_RSHIFT#`, `KEY_RCTRL#`, `KEY_LALT#`, `KEY_RALT#`, `KEY_LWIN#`/`KEY_RWIN#`, `KEY_TAB#`, `KEY_BACKSPACE#`, `KEY_INSERT#`, `KEY_DELETE#`, `KEY_HOME#`, `KEY_END#`, `KEY_PGUP#`, `KEY_PGDN#` — see *Keyboard Input*
175
  - **Gamepad:** see *Gamepad*
176
 
177
  ---
@@ -185,6 +191,7 @@ DIM scores$ ' Declare (required before use)
185
  DIM a$, b$, c$ ' Multiple
186
  scores$(0) = 95 ' Numeric indices are 0-based
187
  scores$("name") = "Alice" ' String keys: associative, insertion-ordered
 
188
  matrix$(0, 1) = "A2" ' Multi-dimensional
189
 
190
  DIM sounds$
@@ -215,8 +222,12 @@ NEXT i$
215
  | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
216
  | `JOIN dest$, src1$, src2$` | Merge two arrays; matching `src2$` keys overwrite `src1$` at the same level. Use an empty `src1$` to copy an array |
217
 
 
 
218
  > `DIM arr$ = value` stores the value at `arr$(0)` — NOT as a scalar. If a scalar is what you want, use `LET`.
219
 
 
 
220
  ---
221
 
222
  ## Control Flow
@@ -239,11 +250,30 @@ IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [play]
239
  FOR i$ = 1 TO 10 STEP 2 : PRINT i$ : NEXT
240
  FOR i$ = 10 TO 1 STEP -1 : PRINT i$ : NEXT
241
 
 
 
 
 
 
 
 
 
 
 
242
  ' WHILE
243
  WHILE x$ < 100
244
  x$ = x$ * 2
245
  WEND
246
 
 
 
 
 
 
 
 
 
 
247
  ' Labels, GOTO, GOSUB
248
  [start]
249
  GOSUB [sub:init]
@@ -263,6 +293,14 @@ END ' Terminate program
263
 
264
  Labels may contain almost any printable character. Whitespace is trimmed; everything between `[` and `]` is the label name.
265
 
 
 
 
 
 
 
 
 
266
  ---
267
 
268
  ## I/O
@@ -303,6 +341,7 @@ Labels may contain almost any printable character. Whitespace is trimmed; everyt
303
  - Numpad Enter = `KEY_ENTER#`.
304
  - `INKEY` consumes the key: read it once per loop pass (`LET k$ = INKEY`) and compare the variable. A second `INKEY` in the same pass returns 0.
305
  - Console mode: `INKEY`/`WAITKEY` return the typed character (except letters and special keys); numpad returns the typed digit, not `KEY_NUMPAD*#`.
 
306
 
307
  ### Mouse (graphics mode only)
308
  `MOUSEX`, `MOUSEY` — cursor position
@@ -368,20 +407,22 @@ FN Clamp$(5, 1, 10) ' ✗ ERROR — return value unused
368
  | Function | Description |
369
  |----------|-------------|
370
  | `ASC(s$)` | ASCII code of first char |
 
371
  | `CHR(n)` | Character from ASCII code |
372
- | `INSTR(s$, search$)` | Position (1-based), 0=not found; case-sensitive |
373
  | `INSTR(s$, search$, mode)` | mode: 0=case-insensitive, 1=case-sensitive |
374
  | `INVERT(s$)` | Reverse string |
375
  | `LCASE(s$)` / `UCASE(s$)` | Lower / upper case |
376
- | `LEFT(s$, n)` / `RIGHT(s$, n)` | First/last n chars |
377
  | `LEN(s$)` | String length |
378
  | `LTRIM(s$)` / `RTRIM(s$)` / `TRIM(s$)` | Strip whitespace |
379
- | `MID(s$, start)` | Substring from start (1-based) |
380
- | `MID(s$, start, len)` | Substring with length |
381
- | `REPEAT(s$, n)` | Repeat string n times |
382
  | `REPLACE(s$, a$, b$)` | Replace a$ with b$ in s$ |
 
383
  | `SPLIT(arr$, s$, sep$)` | Split into array (keys `0`..`count-1`), returns count. Array is emptied first. `arr$()` also accepted |
384
- | `SRAND(n)` | Random alphanumeric string of length n. `n = 0` → `""`; **negative n crashes** with an overflow error — never pass an unvalidated length |
385
  | `STR(n)` | Number to string |
386
  | `VAL(s$)` | String to number |
387
  | `SHA256(s$)` | SHA256 hash (64-char hex) |
@@ -481,7 +522,7 @@ END
481
  | `LERP(start, end, t)` | Linear interpolation (t: 0.0–1.0) |
482
  | `LOG(n)` | Natural logarithm |
483
  | `MAX(a, b)` / `MIN(a, b)` | Larger / smaller of two |
484
- | `MOD(a, b)` / `a % b` | Remainder (function and operator forms are equivalent) |
485
  | `POW(base, exp)` | Power |
486
  | `RND(n)` | Random integer 0 to n-1 (n > 0) |
487
  | `RND(0)` | Random float 0.0–1.0 |
@@ -542,6 +583,7 @@ CIRCLE (cx,cy), radius, color ' Circle outline
542
  CIRCLE (cx,cy), radius, color, 1 ' Circle filled
543
  PAINT (x, y), fillColor, borderColor ' Flood fill
544
  LET c$ = POINT(x, y) ' Read pixel color
 
545
  LET COL# = RGB(r, g, b) ' Create color (0–255 each)
546
  ```
547
 
@@ -628,7 +670,7 @@ LET API_KEY# = env$("OPENAI_API_KEY")
628
 
629
  **FILELIST:** files only (no folders, not recursive), names only (no paths), sorted alphabetically ignoring case, hidden/system files skipped. The array is cleared first. Pattern supports `*` and `?`, default all files. Missing folder → returns `0`. To open a listed file, prepend the folder: `FILEREAD("levels/" + files$(i$))`.
630
 
631
- **Paths:** Use `/` or `\\` — never a single `\` (escape char). Relative paths are from `PRG_ROOT#`, which already ends with a separator: `PRG_ROOT# + "data.txt"`.
632
 
633
  ---
634
 
@@ -649,20 +691,38 @@ LET res$ = HTTPPOST("https://api.example.com/data", body$, headers$)
649
  LET res$ = HTTPPOST("http://localhost:11434/v1/chat/completions", body$, headers$, 120)
650
  ```
651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652
  ### Local HTTP server (LISTEN)
653
 
654
- Local-only server, typically glue between a static HTML page and a BazzBasic script on the same machine.
655
 
656
  ```basic
657
  STARTLISTEN 8080 ' Bind 127.0.0.1:8080, no admin needed
658
- STARTLISTEN 8080, 5000 ' Optional timeout in ms
 
659
  LET body$ = GETREQUEST() ' POST/PUT/PATCH body or GET query string ("" on timeout)
660
  SENDRESPONSE "{""status"":""ok""}" ' Must be called or the browser hangs
661
  STOPLISTEN
662
  ```
663
 
664
  - **Always pair `GETREQUEST()` with `SENDRESPONSE`.**
665
- - **Bound to `127.0.0.1` only.** Do not generate code that tries to expose it on a LAN or the internet.
 
666
  - **Body or query string only** (no `?`). Headers, method and URL path are not exposed. Parse JSON bodies with `ASARRAY`.
667
  - **OPTIONS preflight and CORS are automatic.** Don't handle `OPTIONS`; every response carries `Access-Control-Allow-Origin: *`.
668
  - **One listener at a time.** Call `STOPLISTEN` before another `STARTLISTEN`.
@@ -684,7 +744,7 @@ END
684
 
685
  ---
686
 
687
- ## Arrays & JSON
688
 
689
  Nested JSON maps to comma-separated keys: `data$("player,name")`, `data$("skills,0")`.
690
 
@@ -696,6 +756,48 @@ LOADJSON arr$, "file.json" ' Load JSON file
696
  SAVEJSON arr$, "file.json" ' Save JSON file
697
  ```
698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
  ### Passing Arrays to Functions
700
 
701
  Serialize with `ASJSON`, pass the string, rebuild inside with `ASARRAY`. The function gets an independent copy — changes inside do not affect the original. Nested keys work normally.
@@ -764,6 +866,7 @@ PRINT ARGS(1) ' "arg2"
764
  ```
765
 
766
  0-based; does not include the interpreter or script name.
 
767
 
768
  ---
769
 
@@ -784,6 +887,7 @@ PRINT FN MATHLIB_add$(5, 3)
784
  - **No extension fallback:** `INCLUDE "helpers"` does NOT try `helpers.bas`.
785
  - **Path resolution:** relative to the including file's directory first, then the program's base path.
786
  - **Circular includes** halt with `Circular INCLUDE detected: <file>`.
 
787
 
788
  ---
789
 
@@ -862,10 +966,4 @@ From version 1.6, the IDE (`BazzBasic-IDE.exe`) and interpreter (`BazzBasic.exe`
862
  ```basic
863
  ' BazzBasic version {ver_num}
864
  ' https://ekbass.github.io/BazzBasic/
865
- ```
866
-
867
- ## Examples
868
-
869
- Example programs can be found in the `Examples/` directory (relative to the directory for this skill).
870
- - Before writing BazzBasic code, review the examples most relevant to the task and follow the practices demonstrated in them (naming conventions, structure, typical idioms).
871
- - The examples are organized into folders, such as "graphics", "console", "API", "GamePad", etc.
 
1
  ---
2
+ name: "bazzbasic-code"
3
+ description: "BazzBasic BASIC interpreter language reference. Use when writing, debugging, or explaining BazzBasic code (.bas files). Triggers on BazzBasic syntax, $ and # variable suffixes, SDL2 graphics in BASIC, SCREEN/DRAWSHAPE/LOADIMAGE commands, DEF FN functions, BazzBasic file I/O, sound commands, or any question about BazzBasic features. Always use this skill before writing any BazzBasic code. For changing the interpreter or IDE source code use bazzbasic-interpreter-dev or bazzbasic-ide-dev."
4
  ---
5
 
6
  # BazzBasic Language Reference
7
+ **Version:** 1.7 | **Author:** Kristian Virtanen (EkBass) | **Platform:** Windows x64 only (SDL2 via P/Invoke — no Linux, macOS, browser or Android)
8
  **Homepage:** https://ekbass.github.io/BazzBasic/
9
  **GitHub:** https://github.com/EkBass/BazzBasic
10
+ **Manual:** https://ekbass.github.io/BazzBasic/manual/#/
11
  **Examples:** https://github.com/EkBass/BazzBasic/tree/main/Examples
12
  **Rosetta Code:** https://rosettacode.org/wiki/Category:BazzBasic
13
  **Rosetta Code solutions:** https://github.com/EkBass/BazzBasic/tree/main/Examples/rosetta-code
 
24
  |------|--------|
25
  | Variables end with `$` | `name$`, `score$`, `x$` — the suffix does NOT mean string type |
26
  | Constants end with `#` | `MAX#`, `PI#`, `TITLE#` — `#` only means immutable |
27
+ | Built-in constants cannot be redefined | `LET PI# = 3.14` is an error (`Cannot redefine constant 'PI#'`) — use your own name: `LET MY_PI# = 3.14` |
28
+ | Arrays declared with `DIM`, end with `$` | `DIM items$`. A non-`$` name (`DIM MAX#`) is an error. `DIM` ends at `:`, so `DIM a$ : a$(0) = 1` works |
29
  | First use of variable requires `LET` | `LET x$ = 0` — after that `x$ = x$ + 1` |
30
  | FOR and INPUT auto-declare, no LET needed | `FOR i$ = 1 TO 10` |
31
+ | Case-insensitive | Keywords, variables, constants, labels and array names. For text values see the next row |
32
+ | Case rules differ for text | `=` `<>` `<` `>` `<=` `>=` ignore case: `"abc" = "ABC"` → `1`. Array string keys are case-sensitive (`arr$("name")` and `arr$("NAME")` are different elements), and so is `INSTR` by default. JSON object keys are too, in both directions: `ASJSON`/`SAVEJSON` write them verbatim, `ASARRAY`/`LOADJSON` read them verbatim. Exact (case-sensitive) compare: `IF SAME(pass$, input$) THEN`. Before 1.7 (no `SAME`): `LEN(a$) = LEN(b$) AND INSTR(a$ + "~", b$ + "~") = 1` — the `+ "~"` makes two empty strings match |
33
  | Functions defined **before** they are called | Put at top or INCLUDE — forward references don't resolve |
34
  | Function name ends with `$`, called with `FN` | `FN MyFunc$(a$, b$)` |
35
+ | Parameters end with `$`; pass exactly as many arguments as defined | `DEF FN F$(a$, B#)` is an error (`#` = constant). `FN Add$(1)` for `Add$(a$, b$)` stops with `FN ADD$: expected 2 arguments, got 1` |
36
  | Function return value **must** be used | `PRINT FN f$()` or `LET v$ = FN f$()` |
37
  | `DEF FN` scope is fully isolated | Only global `#` constants are visible. No `$` variables, no global arrays. Arrays cannot be passed as parameters — see *Passing Arrays to Functions* |
38
  | `+` does both add and concatenate | If either side is a string, the other is auto-converted: `"Score: " + 5` → `"Score: 5"`, no `STR()` needed |
39
  | Division always returns float | `10 / 3` → `3.333...` |
40
+ | Division by zero returns `0` silently | Same for `/`, `/=`, `%` and `MOD(a, 0)`. Guard with `IF b$ = 0 THEN ...` if correctness matters |
41
+ | No `\` or `^` operator | Both are an error before the program runs (`Unexpected character '\'`). Integer division: `INT(a / b)`, `FLOOR(a / b)` or `CINT(a / b)`; power: `POW(a, b)` |
42
+ | Errors halt the program | No `TRY`/`CATCH` or `ON ERROR` — line-numbered message printed. Exception: a failed `HTTPGET`/`HTTPPOST` returns `""` and sets `HTTPERROR` (see *Network*) |
43
  | No line-continuation char | Leave an operator at line end or open a `(` — see *Line Continuation* |
44
  | No line numbers | Never supported. Use `[labels]` |
45
  | `:` separates statements on one line | `FOR i$ = 1 TO 3 : PRINT i$ : NEXT`. A `:` inside a label like `[sub:init]` is part of the label name, not a separator |
 
67
  LET x$ = a$ + _ ' VBA continuation LET x$ = a$ + ' just leave the operator
68
  LET x$ = a$ + \ ' Python continuation LET x$ = a$ + ' or open a paren
69
  IF NOT a$ = b$ THEN ' QBASIC habit IF NOT (a$ = b$) THEN
70
+ IF done$ THEN BREAK ' C/Python habit IF done$ THEN EXIT WHILE ' or EXIT FOR
71
+ IF done$ THEN GOTO [after] ' out of loop IF done$ THEN EXIT FOR ' GOTO leaks loop state
72
  ```
73
 
74
  ---
 
77
 
78
  - Prefer `FSTRING` over long `+` concatenation chains for multi-variable strings
79
  - Place `DEF FN` definitions at the top of the file or via `INCLUDE`
80
+ - Store every `LOADIMAGE` / `LOADSOUND` / `LOADSHAPE` handle as a `#` constant. They return string handles that never change while the program runs — images/shapes (also each `LOADSHEET` cell) `"SHAPE1"`, `"SHAPE2"`…, sounds a GUID-style string like `"57212810-1166-4bcf-8ccd-bdfc6607046f"`. Treat them as opaque IDs, not numbers. For many handles, an array is fine: `sprites$("player") = LOADIMAGE(...)`
81
  - Declare all variables and constants in `[inits]`, not inside loops — this avoids repeated existence checks
82
  - Precompute colors: `LET WHITE# = RGB(255, 255, 255)` in `[inits]`, never `RGB()` inside a game loop
83
  - In graphics mode: clear with `LINE (0,0)-(W,H), 0, BF` (much faster than `CLS`), draw text with `DRAWSTRING` (`PRINT` flickers), and wrap each frame in `SCREENLOCK ON` / `SCREENLOCK OFF`
 
151
  LET name$ = "Alice" ' String
152
  LET score$ = 0 ' Number
153
 
154
+ LET MAX_LIVES# = 3 ' Constant (immutable)
155
  LET TITLE# = "My Game" ' String constant
156
  ```
157
 
 
172
  **Comparison:** `"123" = 123` is TRUE (cross-type), but keep types consistent for speed.
173
 
174
  ### Built-in Constants
175
+ - **Boolean:** `TRUE` & `ON` (`1`), `FALSE` & `OFF` (`0`) — plain numbers. `IF x$ = 1` and `IF x$ = TRUE` are identical.
176
  - **Math:** `PI#`, `HPI#` (π/2 = 90°), `QPI#` (π/4 = 45°), `TAU#` (2π = 360°), `EULER#` (e)
177
+ - **Astronomy:** `AU#` = 149597870700 — astronomical unit in metres (exact, IAU 2012)
178
  - **System:** `PRG_ROOT#` (program base directory path, ends with a separator)
179
+ - **Version:** `BBVER#` (current BazzBasic version as a string, e.g. `"1.7"`), `BBBUILD#` (interpreter build time as a string `"dd-mm-yyyy hh:mm:ss"`, e.g. `"06-11-2026 20:47:16"`; an `-exe` program reports the build time of the interpreter it was made with, since 1.7)
180
+ - **Keyboard:** `KEY_ESC#`, `KEY_ENTER#`, `KEY_SPACE#`, `KEY_UP#`, `KEY_DOWN#`, `KEY_LEFT#`, `KEY_RIGHT#`, `KEY_F1#`…`KEY_F12#`, `KEY_A#`…`KEY_Z#`, `KEY_0#`…`KEY_9#`, `KEY_NUMPAD0#`…`KEY_NUMPAD9#`, `KEY_NUMPAD_PLUS#`, `_MINUS#`, `_MUL#`, `_DIV#`, `_DOT#`, `KEY_LSHIFT#`, `KEY_LCTRL#`, `KEY_RSHIFT#`, `KEY_RCTRL#`, `KEY_LALT#`, `KEY_RALT#`, `KEY_LWIN#`/`KEY_RWIN#`, `KEY_TAB#`, `KEY_BACKSPACE#`, `KEY_INSERT#`, `KEY_DELETE#`, `KEY_HOME#`, `KEY_END#`, `KEY_PGUP#`, `KEY_PGDN#`, punctuation (layout-dependent) `KEY_COMMA#` `KEY_DOT#` `KEY_MINUS#` `KEY_EQUALS#` `KEY_SLASH#` `KEY_BACKSLASH#` `KEY_SEP#` (`;`) `KEY_GRAVE#` `KEY_LBRACKET#` `KEY_RBRACKET#` — see *Keyboard Input*
181
  - **Gamepad:** see *Gamepad*
182
 
183
  ---
 
191
  DIM a$, b$, c$ ' Multiple
192
  scores$(0) = 95 ' Numeric indices are 0-based
193
  scores$("name") = "Alice" ' String keys: associative, insertion-ordered
194
+ PRINT HASKEY(scores$("NAME")) ' 0 - string keys are case-sensitive; use UCASE(key$) to normalize
195
  matrix$(0, 1) = "A2" ' Multi-dimensional
196
 
197
  DIM sounds$
 
222
  | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
223
  | `JOIN dest$, src1$, src2$` | Merge two arrays; matching `src2$` keys overwrite `src1$` at the same level. Use an empty `src1$` to copy an array |
224
 
225
+ > Re-DIM of an existing global array is a no-op (contents kept) — only `DELARRAY` removes it. Inside `DEF FN`, `DIM` always creates a fresh local array.
226
+
227
  > `DIM arr$ = value` stores the value at `arr$(0)` — NOT as a scalar. If a scalar is what you want, use `LET`.
228
 
229
+ > `DIM arr$(10)` is accepted but the size is ignored — it declares the same empty dynamic array as `DIM arr$`; nothing inside the parentheses is read. Write plain `DIM arr$`. Size and value cannot be combined: `DIM arr$(10) = 5` is an error.
230
+
231
  ---
232
 
233
  ## Control Flow
 
250
  FOR i$ = 1 TO 10 STEP 2 : PRINT i$ : NEXT
251
  FOR i$ = 10 TO 1 STEP -1 : PRINT i$ : NEXT
252
 
253
+ ' End value of counter Comparison. Wont pass ending point like in MS-BASIC style dialects
254
+ FOR i$ = 1 TO 10 : NEXT : PRINT i$ ' 10
255
+ FOR i$ = 1 TO 10 STEP 4 : NEXT : PRINT i$ ' 9
256
+
257
+ ' EXIT FOR — leave innermost FOR, continue after its NEXT (loop var keeps its value)
258
+ FOR i$ = 1 TO 100
259
+ IF i$ * i$ > 50 THEN EXIT FOR
260
+ NEXT
261
+ PRINT "First square over 50: "; i$ ' 8
262
+
263
  ' WHILE
264
  WHILE x$ < 100
265
  x$ = x$ * 2
266
  WEND
267
 
268
+ ' EXIT WHILE — leave innermost WHILE, continue after its WEND
269
+ LET key$
270
+ WHILE 1
271
+ key$ = INKEY
272
+ IF key$ = KEY_ESC# THEN EXIT WHILE
273
+ ' Game logic here
274
+ WEND
275
+ PRINT "Goodbye"
276
+
277
  ' Labels, GOTO, GOSUB
278
  [start]
279
  GOSUB [sub:init]
 
293
 
294
  Labels may contain almost any printable character. Whitespace is trimmed; everything between `[` and `]` is the label name.
295
 
296
+ ### EXIT FOR / EXIT WHILE (since 1.5)
297
+
298
+ - Work anywhere inside the loop: one-line IF, inside an `IF...END IF` block, or inside an inner loop of the *other* kind. `EXIT WHILE` inside a FOR that sits in a WHILE leaves both loops.
299
+ - Only the innermost loop of the named kind is left. To leave several nested loops of the same kind at once, use a flag variable.
300
+ - Inside `DEF FN`, EXIT can only leave loops that are inside the function.
301
+ - `EXIT` without a loop of that kind is an error: `EXIT WHILE without WHILE`.
302
+ - Prefer EXIT over `GOTO` out of a loop. A GOTO leaves the loop's bookkeeping behind; doing it repeatedly (e.g. once per frame) slowly fills memory and can confuse later `NEXT` statements.
303
+
304
  ---
305
 
306
  ## I/O
 
341
  - Numpad Enter = `KEY_ENTER#`.
342
  - `INKEY` consumes the key: read it once per loop pass (`LET k$ = INKEY`) and compare the variable. A second `INKEY` in the same pass returns 0.
343
  - Console mode: `INKEY`/`WAITKEY` return the typed character (except letters and special keys); numpad returns the typed digit, not `KEY_NUMPAD*#`.
344
+ - No real console (redirected stdin/stdout): console `INKEY`/`WAITKEY`/`SLEEP`/`CLS`/`CURPOS` stop with a line-numbered `Error at line N: ...` and exit code 1; piped `INPUT`/`LINE INPUT` still works.
345
 
346
  ### Mouse (graphics mode only)
347
  `MOUSEX`, `MOUSEY` — cursor position
 
407
  | Function | Description |
408
  |----------|-------------|
409
  | `ASC(s$)` | ASCII code of first char |
410
+ | `ASC(s$, pos)` | ASCII code of char at 1-based `pos`; out of range (`pos < 1` or `pos > LEN(s$)`) → `0`. Prefer over `ASC(MID(s$, pos, 1))` in loops (no temp substring, ~35 % faster) |
411
  | `CHR(n)` | Character from ASCII code |
412
+ | `INSTR(s$, search$)` | Position (1-based), 0=not found; case-sensitive. Start form `INSTR(start, s$, search$)`: `start` < 1 or > `LEN(s$)` → `0` |
413
  | `INSTR(s$, search$, mode)` | mode: 0=case-insensitive, 1=case-sensitive |
414
  | `INVERT(s$)` | Reverse string |
415
  | `LCASE(s$)` / `UCASE(s$)` | Lower / upper case |
416
+ | `LEFT(s$, n)` / `RIGHT(s$, n)` | First/last n chars; `n <= 0` → `""`, `n >= LEN(s$)` → whole string |
417
  | `LEN(s$)` | String length |
418
  | `LTRIM(s$)` / `RTRIM(s$)` / `TRIM(s$)` | Strip whitespace |
419
+ | `MID(s$, start)` | Substring from start (1-based); `start` < 1 or > `LEN(s$)` → `""` |
420
+ | `MID(s$, start, len)` | Substring with length; `len` < 0 → runtime error (`MID: length must be 0 or more, got -1`); `len` past the end clamps to the tail |
421
+ | `REPEAT(s$, n)` | Repeat n times; `n <= 0` or empty string → `""`; result > 2147483647 chars → runtime error (`REPEAT: result too large (N characters)`) |
422
  | `REPLACE(s$, a$, b$)` | Replace a$ with b$ in s$ |
423
+ | `SAME(a$, b$)` | 1 if identical, case-sensitive (`SAME("A", "a")` → `0`); numbers compare as text (`SAME("1.0", 1)` → `0`) — use `=` for numbers. Since 1.7 |
424
  | `SPLIT(arr$, s$, sep$)` | Split into array (keys `0`..`count-1`), returns count. Array is emptied first. `arr$()` also accepted |
425
+ | `SRAND(n)` | Random alphanumeric string of length n (decimals dropped). `n = 0` → `""`; a negative n is a runtime error (`SRAND: length must be 0 or more, got -1`) |
426
  | `STR(n)` | Number to string |
427
  | `VAL(s$)` | String to number |
428
  | `SHA256(s$)` | SHA256 hash (64-char hex) |
 
522
  | `LERP(start, end, t)` | Linear interpolation (t: 0.0–1.0) |
523
  | `LOG(n)` | Natural logarithm |
524
  | `MAX(a, b)` / `MIN(a, b)` | Larger / smaller of two |
525
+ | `MOD(a, b)` / `a % b` | Remainder (function and operator forms are equivalent). `b = 0` → `0` |
526
  | `POW(base, exp)` | Power |
527
  | `RND(n)` | Random integer 0 to n-1 (n > 0) |
528
  | `RND(0)` | Random float 0.0–1.0 |
 
583
  CIRCLE (cx,cy), radius, color, 1 ' Circle filled
584
  PAINT (x, y), fillColor, borderColor ' Flood fill
585
  LET c$ = POINT(x, y) ' Read pixel color
586
+ SAVESCREEN "shot.png" ' Save screen as PNG (only .png; relative to program folder)
587
  LET COL# = RGB(r, g, b) ' Create color (0–255 each)
588
  ```
589
 
 
670
 
671
  **FILELIST:** files only (no folders, not recursive), names only (no paths), sorted alphabetically ignoring case, hidden/system files skipped. The array is cleared first. Pattern supports `*` and `?`, default all files. Missing folder → returns `0`. To open a listed file, prepend the folder: `FILEREAD("levels/" + files$(i$))`.
672
 
673
+ **Paths:** Use `/` or `\\` — never a single `\` (escape char). Relative paths are from `PRG_ROOT#`, which already ends with a separator: `PRG_ROOT# + "data.txt"`. This also covers asset loads: `LOADIMAGE`, `LOADSHEET`, `LOADSOUND`, `LOADFONT` and `SAVESCREEN` resolve relative paths from the program folder too.
674
 
675
  ---
676
 
 
691
  LET res$ = HTTPPOST("http://localhost:11434/v1/chat/completions", body$, headers$, 120)
692
  ```
693
 
694
+ **Failed requests do not stop the program.** On connection error, unknown host or timeout, `HTTPGET`/`HTTPPOST` return `""` and `HTTPERROR` holds the reason (`""` after a successful call; parentheses optional). Check it right after the call:
695
+
696
+ ```basic
697
+ LET res$ = HTTPGET(url$)
698
+ IF HTTPERROR <> "" THEN
699
+ PRINT "Source is down: "; HTTPERROR
700
+ ELSE
701
+ ' use res$
702
+ END IF
703
+ ```
704
+
705
+ - `HTTPERROR` has no `$` — it is a built-in function like `TICKS`, not a variable.
706
+ - An invalid URL (not `http://`/`https://`) is still a program-stopping error.
707
+ - HTTP error statuses (404, 500…) are **not** errors: the body (often an HTML error page) is returned and `HTTPERROR` is `""`.
708
+ - `""` alone does not prove failure — a server may answer with an empty body. Use `HTTPERROR` when it matters.
709
+
710
  ### Local HTTP server (LISTEN)
711
 
712
+ Small HTTP server, typically glue between a static HTML page and a BazzBasic script on the same machine.
713
 
714
  ```basic
715
  STARTLISTEN 8080 ' Bind 127.0.0.1:8080, no admin needed
716
+ STARTLISTEN 8080, 5000 ' Optional timeout in ms (0 = wait forever)
717
+ STARTLISTEN 8080, 0, "*" ' Optional host: "*" = all network interfaces
718
  LET body$ = GETREQUEST() ' POST/PUT/PATCH body or GET query string ("" on timeout)
719
  SENDRESPONSE "{""status"":""ok""}" ' Must be called or the browser hangs
720
  STOPLISTEN
721
  ```
722
 
723
  - **Always pair `GETREQUEST()` with `SENDRESPONSE`.**
724
+ - **Default is `127.0.0.1`.** Use a host (`"*"` or an address) only when the user wants other computers to connect. Then tell them: Windows needs a one-time `netsh http add urlacl url=http://+:PORT/ user=Everyone` as administrator (STARTLISTEN's error prints the exact command), the firewall must allow the port, and the server is plain HTTP with no authentication or body size limit.
725
+ - **Host is positional:** it is the third argument, so pass a timeout first (`0` for none).
726
  - **Body or query string only** (no `?`). Headers, method and URL path are not exposed. Parse JSON bodies with `ASARRAY`.
727
  - **OPTIONS preflight and CORS are automatic.** Don't handle `OPTIONS`; every response carries `Access-Control-Allow-Origin: *`.
728
  - **One listener at a time.** Call `STOPLISTEN` before another `STARTLISTEN`.
 
744
 
745
  ---
746
 
747
+ ## Arrays & JSON (and XML)
748
 
749
  Nested JSON maps to comma-separated keys: `data$("player,name")`, `data$("skills,0")`.
750
 
 
756
  SAVEJSON arr$, "file.json" ' Save JSON file
757
  ```
758
 
759
+ `ASARRAY` on an empty string is an error. A failed `HTTPGET`/`HTTPPOST` returns `""`, so check `HTTPERROR` before passing the response to `ASARRAY`.
760
+
761
+ `ASJSON`/`SAVEJSON` emit a JSON **array** at a level only when all its keys are canonical non-negative ints (`"0"`, `"1"`, ... — no sign, leading zeros or spaces) and the max index is <= 1,000,000 or the level is dense (max < 4*count+64); otherwise a JSON **object** with the original keys. A huge key (`arr$("2147483647")`) returns instantly as an object instead of freezing (before 1.7); `"-5"`/`"007"` keys are kept as properties, not dropped. `ASARRAY`/`LOADJSON` read both shapes back to the same keys (arrays → `"0".."n-1"`, objects → verbatim names; `null` holes skipped).
762
+
763
+ Keys keep their case in both directions (D12): `arr$("key")` and `arr$("KEY")` stay two keys — `ASJSON` gives `{"key":"lower","KEY":"upper"}`, at every nesting level. Before 1.7 the write path collapsed case-differing keys and lost a value.
764
+
765
+ ### XML (light model)
766
+
767
+ `ASARRAY` also reads XML. It picks the format from the first character: `{` or `[` = JSON, `<` = XML (BOM and whitespace skipped). **Prefer JSON.** Use XML only when the task requires it — an RSS/Atom feed, or an API or file that only offers XML. JSON keys are shorter, numbers stay numbers, and it maps to arrays cleanly.
768
+
769
+ ```basic
770
+ LET n$ = ASARRAY(feed$, xml$) ' XML string → array
771
+ LET xml$ = ASXML(arr$) ' Array → XML string
772
+ LOADXML arr$, "file.xml" ' Load XML file
773
+ SAVEXML arr$, "file.xml" ' Save XML file (indented, with <?xml ...?>)
774
+ ```
775
+
776
+ - **Every element below the root is indexed, even a single one.** Never drop the `,0`: `feed$("rss,channel,0,item,0,title,0")`, not `feed$("rss,channel,item,title")`.
777
+ - Attributes: `...,guid,0,@isPermaLink`. Text of an element that also has attributes or children: `...,guid,0,#text`.
778
+ - Prefixes kept as written: `...,dc:creator,0`. CDATA is plain text. Comments and DTDs are ignored.
779
+ - All values are strings — use `VAL()` for numbers. Mixed text-and-element order is lost.
780
+ - `ASXML`: a single top-level key becomes the root element, otherwise `<root>`. Keys must be valid XML names (no spaces).
781
+ - XML is not HTML: a web page does not parse as XML.
782
+
783
+ ```basic
784
+ ' RSS reader
785
+ LET xml$ = HTTPGET("https://www.example.com/rss.xml")
786
+ IF HTTPERROR <> "" THEN
787
+ PRINT "Feed is down: "; HTTPERROR
788
+ END
789
+ END IF
790
+ DIM feed$
791
+ LET n$ = ASARRAY(feed$, xml$)
792
+ PRINT feed$("rss,channel,0,title,0")
793
+ LET i$ = 0
794
+ WHILE HASKEY(feed$("rss,channel,0,item," + i$ + ",title,0"))
795
+ PRINT "- "; feed$("rss,channel,0,item," + i$ + ",title,0")
796
+ i$ += 1
797
+ WEND
798
+ END
799
+ ```
800
+
801
  ### Passing Arrays to Functions
802
 
803
  Serialize with `ASJSON`, pass the string, rebuild inside with `ASARRAY`. The function gets an independent copy — changes inside do not affect the original. Nested keys work normally.
 
866
  ```
867
 
868
  0-based; does not include the interpreter or script name.
869
+ Packaged `-exe` programs receive arguments identically (`prog.exe arg1 arg2`); inside a packaged exe, flags like `-v` are not special — they arrive as ordinary ARGS() strings.
870
 
871
  ---
872
 
 
887
  - **No extension fallback:** `INCLUDE "helpers"` does NOT try `helpers.bas`.
888
  - **Path resolution:** relative to the including file's directory first, then the program's base path.
889
  - **Circular includes** halt with `Circular INCLUDE detected: <file>`.
890
+ - A .bb library is never embedded into an -exe package - ship it next to the exe (only .bas INCLUDEs are embedded).
891
 
892
  ---
893
 
 
966
  ```basic
967
  ' BazzBasic version {ver_num}
968
  ' https://ekbass.github.io/BazzBasic/
969
+ ```