Writing and Running Grapa Scripts
Syntax Reminders
- Every statement and every block (including after closing braces) must end with a semicolon (
;).
- Use block comments (
/* ... */) or line comments (// ...). # comments are not supported.
- To append to arrays, use the
+= operator (not .push() or .append()).
- See Syntax Quick Reference for more.
Writing .grc Files
- Use
.grc files for Grapa scripts and tests.
- End every statement and block with a semicolon.
- Use block comments for documentation and explanations.
- Use
.echo() for output.
- Use
while loops (not for).
- Access arrays/lists with
[index], objects with .get("key").
- See Examples for idiomatic code patterns.
Example .grc File
/* Example Grapa script */
input = "Hello world\nGoodbye world\nHello again";
matches = input.grep("Hello");
matches.echo();
Running .grc Files
- Use the
-f option to run a Grapa script file:
- Windows:
.\grapa.exe -q -f "my_script.grc"
- Linux/Mac:
./grapa -q -f "my_script.grc"
- Use
-c for short, inline commands (not for files).
- Always use
-f for multi-line scripts and tests.
Creating and Using .grz Files
.grz files are compiled Grapa scripts for faster execution.
- To create a
.grz file, use the Grapa compiler or the $sys().compile() function:
compiled = $sys().compile("my_script.grc");
$file().set("my_script.grz", compiled);
- To run a
.grz file:
- Windows:
.\grapa.exe -q -f "my_script.grz"
- Linux/Mac:
./grapa -q -f "my_script.grz"
.grz files are portable and can be distributed for faster loading and execution.
- See GRZ Format Specification for technical details.
Best Practices
- Validate your
.grc scripts with known-good examples before running in production.
- Use
.grz files for deployment or performance-critical scenarios.
- Use Testing to ensure your scripts behave as expected.
See also